{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "09c25ebc-e403-4356-9569-0b94d5696fff",
   "metadata": {},
   "source": [
    "# Launch Jupyter FAST!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "947ec858",
   "metadata": {},
   "source": [
    "To save you some typing, you may create **aliases** (to the long commands you issue) to launch Jupyter Notebook by just typing the aliases:\n",
    "\n",
    "- `dsm` (change directory to the py project directory),\n",
    "- `venv` (activate the virtual environment), and\n",
    "- `jn` (launch Jupyter Notebook) like:\n",
    "\n",
    "After you have the aliases created, you can launch Jupyter Notebook with only several keystrokes in the terminal like below. \n",
    "\n",
    "```powershell\n",
    "PS C:\\Users\\[user]> dsm                           \n",
    "PS C:\\Users\\[user]\\workspace\\dsm> venv\n",
    "(.venv) PS C:\\Users\\[user]\\workspace\\dsm> jn\n",
    "[I 2025-09-07 16:35:42.748 ServerApp] jupyter_lsp | extension was successfully linked.\n",
    "[I 2025-09-07 16:35:42.757 ServerApp] jupyter_server_terminals | extension was successfully linked.\n",
    "...\n",
    "...\n",
    "```\n",
    "\n",
    "These aliases do the following for you: \n",
    "\n",
    "- `dsm` will move your present working directory to your project folder *dsm*.\n",
    "- `venv` will activate your virtual environment.\n",
    "- `jn` will launch your Jupyter Notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b32a01b4-4fa1-4a8e-8c23-089b7761de45",
   "metadata": {},
   "source": [
    "Here are step-by-step instructions of how to set up the aliases. The Windows version of this instruction is longer because we have to modify a few places in Windows.\n",
    "\n",
    "::::{tab-set}\n",
    ":::{tab-item} Windows\n",
    "\n",
    "In Windows, you may add aliases to your user profile to make your life with Jupyter a little easier. You add aliases to your profile. To check whether you have a profile defined already, do the following in PowerShell:\n",
    "\n",
    "```bash\n",
    "PS C:\\Users\\[user]> echo $PROFILE\n",
    "C:\\Users\\[user]\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1\n",
    "```\n",
    "`echo` means print in the terminal. You should see the system-defined file path and the profile file name as above. \n",
    "\n",
    "You need to edit the profile using Notepad or VS Code. If the profile exists already, the following command will open the `*.ps1` file, and you copy and paste the **##### venv code #####** code block below to your profile, exit Notepad to save the profile. You then close all terminal sessions and exit PowerShell, open PowerShell again, and the aliases should work:\n",
    "\n",
    "```bash\n",
    "PS C:\\Users\\[user]]> notepad $PROFILE\n",
    "```\n",
    "\n",
    "Some systems have the `$PROFILE` environment variable defined, but do not have the profile path/folder and the profile file created. In that case, when you do `notepad $PROFILE`, you are trying to open a file that does not exist yet. You may therefore receive an error message or warning saying that the $PROFILE folder (*WindowsPowerShell*) or the profile file (*Microsoft.PowerShell_profile.ps1*) does not exist. If so, create them by doing the following steps: \n",
    "\n",
    "First, check to see if the profile folder exists. If it does, you will be able to change directory into it (the folder is usually *WindowsPowerShell* or *PowerShell*, and it is under the user's *Documents* folder):\n",
    "\n",
    "```bash\n",
    "PS C:\\Users\\[user]> cd $PROFILE\\..                       ### **cd** into the folder\n",
    "PS C:\\Users\\[user]\\Documents\\WindowsPowerShell>          ### ready to edit profile \n",
    "```\n",
    "\n",
    "If the profile folder *WindowsPowerShell* or *PowerShell* does not exist, you need to create it:\n",
    "\n",
    "```bash\n",
    "PS C:\\Users\\[user]> cd .\\Documents\\\n",
    "PS C:\\Users\\[user]\\Documents> mkdir WindowsPowerShell    ### or \"PowerShell\" depends on your system\n",
    "PS C:\\Users\\[user]\\Documents> cd WindowsPowerShell       ### cd into the profile folder\n",
    "PS C:\\Users\\[user]\\Documents\\WindowsPowerShell>          ### ready to edit profile \n",
    "```\n",
    "\n",
    "After you are in the profile folder, use Notepad or VSCode to edit or create the profile. The profile (**`*.ps1`**) name may be different for different versions of Windows. Use the one you have when doing `echo $PROFILE`:\n",
    "\n",
    "```bash\n",
    "PS C:\\Users\\[user]\\Documents\\WindowsPowerShell> notepad Microsoft.PowerShell_profile.ps1\n",
    "```\n",
    "\n",
    "Notepad will pop up, and you then copy and paste the **##### venv code #####** code block below to the profile to create the aliases: \n",
    "\n",
    "\n",
    "```text\n",
    "########## venv code ############################################################\n",
    "##### 1. dsm: change directory to introdsm ######\n",
    "function CDIntroDSM {set-location ~\\workspace\\dsm\\}\t### create the change direcotry function\n",
    "New-Alias \"dsm\" CDIntroDSM\t\t\t\t### create the alias dsm to the function\n",
    "\n",
    "##### 2. venv: activate virtual environment #####\n",
    "New-Alias -Name \"venv\" -Value .\\.venv\\Scripts\\activate\t### activate venv alias\n",
    "\n",
    "##### 3. jn: start Jupyter notebook ##############\n",
    "function JupyterNotebook {                              ### create the \"jupyter notebook\" function\n",
    "  param(\n",
    "    [string]$notebook = \"notebook\"\n",
    "  )\n",
    "jupyter $notebook\n",
    "}\n",
    "New-Alias -Name \"jn\" -Value JupyterNotebook             ### alias to run the function\n",
    "########## end of venv code ######################################################\n",
    "```\n",
    ":::\n",
    "\n",
    "\n",
    ":::{tab-item} macOS\n",
    "\n",
    "For Mac users, you can add the following lines to your `~/.bashrc` or `~/.zshrc` file, \n",
    "\n",
    "and you should be able to use the `aliases` to launch Jupyter Notebook fast. \n",
    "\n",
    "To edit the `.bashrc` file, open the terminal and type `code .bashrc` or `nano .bashrc`. VS Code (or nano) will then open the file for you to edit.\n",
    "\n",
    "```bash\n",
    "alias dsm='cd ~/workspace/dsm'\n",
    "alias venv='source .venv/bin/activate'\n",
    "alias jn='jupyter notebook'\n",
    "```\n",
    "\n",
    " After you have modified your `~/.bashrc` or `~/.zshrc` file, run `source` to the file or open a new shell to use the updated configuration. To run `source`, do:\n",
    "\n",
    "```bash\n",
    " [user]@[host]ː~/$ source `~/.bashrc`\n",
    "```\n",
    "or \n",
    "```bash\n",
    " [user]@[host]ː~/$ source `~/.zrc`\n",
    "```\n",
    "\n",
    ":::\n",
    "::::"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
