{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "09c25ebc-e403-4356-9569-0b94d5696fff",
   "metadata": {},
   "source": [
    "# Jupyter Bonus\n",
    "\n",
    "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",
    "- `pyd` (change directory to the py project directory),\n",
    "- `venv` (activate the virtual environment), and\n",
    "- `jn` (launch Jupyter Notebook) like:\n",
    "\n",
    "```powershell\n",
    "PS C:\\Users\\[user]> pyd                           \n",
    "PS C:\\Users\\[user]\\workspace\\py> venv\n",
    "(.venv) PS C:\\Users\\[user]\\workspace\\py> 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",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b3921f1-8d08-415d-bb2e-c41adfe25a13",
   "metadata": {},
   "source": [
    "## Windows\n",
    "\n",
    "To add aliases in Windows, you modify the user profile. To check whether you have a profile defined already, do the following in PowerShell:\n",
    "\n",
    "```powershell\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, and `$PROFILE` is a variable that points to the profile script file that runs every time you open a PowerShell session. It’s like a startup configuration file for customizing the environment, setting aliases, defining functions, and loading modules automatically. \n",
    "\n",
    "You need to edit the profile using Notepad or VS Code. But sometimes the profile .ps1 file and its path do not exist yet, and you will see a \"No such file or directory\" warning that looks like:\n",
    "\n",
    "<!-- <span style=\"background-color:darkblue; color:white\"> -->\n",
    "```powershell\n",
    "PS /> $Profile\n",
    "/home/A1653237104/.config/powershell/Microsoft.PowerShell_profile.ps1\n",
    "PS /> ls /home/A1653237104/.config/powershell/\n",
    "/usr/bin/ls: cannot access '/home/A1653237104/.config/powershell/': No such file or directory\n",
    "```\n",
    "<!-- </span> -->\n",
    "\n",
    "To create the directories and the .ps1 file, you may use `mkdir` and touch, or you may run:\n",
    "\n",
    "```powershell\n",
    "New-Item -Path $PROFILE -Force\n",
    "```\n",
    "\n",
    "The following command will open (or create if not exist) the `*.ps1` file in Notepad (`code $PROFILE` will open it in VS Code):\n",
    "\n",
    "```powershell\n",
    "PS C:\\Users\\[user]]> notepad $PROFILE\n",
    "```\n",
    "\n",
    "You then: \n",
    "1) Copy and paste the **##### venv code #####** code block below into your profile.\n",
    "2) Save the profile file and exit Notepad.\n",
    "3) Close all terminal sessions and exit PowerShell.\n",
    "\n",
    "Now, when you reopen PowerShell, the aliases should work.\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",
    "```powershell\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",
    "```powershell\n",
    "########## venv code ############################################################\n",
    "##### 1. py: change directory to py ######\n",
    "function CDIntroPY {set-location ~\\workspace\\py\\}\t### create the change direcotry function\n",
    "New-Alias \"pyd\" CDIntroPY\t\t\t\t### create the alias py 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",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b32a01b4-4fa1-4a8e-8c23-089b7761de45",
   "metadata": {},
   "source": [
    "## macOS\n",
    "\n",
    "For Mac users, you can add the following lines to your `~/.bashrc` or `~/.zshrc` file, and you should be able to use the `aliases` to launch Jupyter Notebook fast after you source the file or open a new shell. 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 pyd='cd ~/workspace/py'\n",
    "alias venv='source .venv/bin/activate'\n",
    "alias jn='jupyter notebook'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "151dfa51-8cba-486b-aaa5-9d309c5fe840",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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
}
