jupyter - anjavdl/PHY517_AST443 GitHub Wiki
Jupyter notebooks are a great way to keep code organized and documented. Notebooks have individual cells, which can be in the kernel language (in this case, python), or in markdown (the language also used by github). A single cell can be run by hitting command-enter or shift-enter (the latter automatically advances to the next cell). A cheat-sheet for Markdown syntax can be found here. Note that markdown cells support LaTeX equations. To start jupyter, type
jupyter notebook
on your command line. This will open a browser window, from which you can navigate to the notebook of choice.
To exit jupyter, hit ctrl-c
twice in the terminal from which you started it. Not doing so will slow down the computer you're running on.
Jupyter on the computing lab
On the machines in the computing lab, jupyter only works with chrome, so you need to start it with
jupyter notebook --browser="google-chrome"
Jupyter Notebooks in VS Code (Python)
Prerequisites
Before you begin, ensure you have the following installed on your system:
- Visual Studio Code (VS Code) – Download and install it from here.
- Python – Install the latest version of Python from here.
- pip – Ensure
pip
, Python’s package manager, is installed. It typically comes with Python. To verify Python andpip
installation, open a terminal (Command Prompt, PowerShell, or Terminal on macOS/Linux) and run:
python --version
pip --version
If these commands return version numbers, Python and pip
are installed correctly.
Step 1: Install VS Code Extensions
To work with Jupyter notebooks in VS Code, you need the Python and Jupyter extensions.
Installing Extensions:
- Open VS Code.
- Go to the Extensions marketplace (Ctrl + Shift + X on Windows/Linux or Cmd + Shift + X on macOS).
- Search for Python and install the extension by Microsoft.
- Search for Jupyter and install the extension by Microsoft. After installing these extensions, restart VS Code.
Step 2: Install Jupyter in Your Python Environment
Jupyter must be installed in the Python environment you want to use. To do this:
- Open VS Code’s terminal (Ctrl + ` on Windows/Linux or Cmd + ` on macOS).
- Run the following command to install Jupyter:
pip install jupyter
- Wait for the installation to complete. To verify the installation, run:
jupyter --version
If a version number appears, Jupyter is installed successfully.
Step 3: Create or Open a Jupyter Notebook in VS Code
Option 1: Create a New Jupyter Notebook
- Open VS Code.
- Press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (macOS) to open the Command Palette.
- Type "Jupyter: Create New Blank Notebook" and select it.
- A new
.ipynb
file (Jupyter Notebook) will open.
Option 2: Open an Existing Jupyter Notebook
- Open VS Code.
- Click File > Open Folder... and select the folder containing your notebook.
- Locate your
.ipynb
file in the Explorer panel and click to open it.
Step 4: Select the Python Interpreter for Jupyter
To ensure VS Code runs the correct Python environment:
- Click on the kernel selector in the top-right corner of the notebook.
- Choose the appropriate Python interpreter (the one where Jupyter is installed).
- If the correct environment is missing, select "Select Another Interpreter" and browse to the correct Python installation. To list available Python environments, run:
python -m jupyter --paths
Step 5: Running Code in Jupyter Notebook
Once your notebook is open:
- Type Python code inside a cell.
- Press Shift + Enter or click the Run button to execute the cell.
- The output will appear directly below the cell.
Video Guide
Get started with Jupyter Notebooks in less than 4 minutes
Running jupyter remotely
Running jupyter on uhura remotely from your own laptop can be quite slow. In this case, you can speed things up significantly by running it remotely, but the browser locally (thanks to Rad for figuring this out!):
Requirements
Jupyter notebook installed in both the local_host and remote_host
Procedure
- Login to the remote host (e.g. uhura):
local_host$ ssh -L localhost:8888:localhost:8889 usernm@remote_host
If you get a message such ascannot listen to port: 8888
, you can choose a different port number. cd
to the Directory where you have want to create your .ipynb files and start Jupyter notebook at port 8889 on remote host without a browser:
remote_host$ jupyter notebook --no-browser --port=8889
- Go to a browser in the local machine and in the address bar type:
localhost:8888/
- If a token is requested then copy the token from the URL printed by the Jupyter notebook call in the terminal. You can also use the following command to view the tokens currently open:
In a separate session of ssh type:
remote_host$ jupyter notebook list
This should give an output like:
Currently running servers:
http://localhost:8889/?token=fccb1b2c8b7157de14466993c5d374c2933f6a4a3e284c53
Copy and paste the token. You should be able to run jupyter notebook faster and on the local browser without window forwarding.
Side effects
Once a port is assigned to remote_host it is no longer available to the local_host. So in the above example port 8888 is now binded to the remote host. This means that a local jupyter notebook cannot be started with the same port which also happens to be the default port.
Hence the following command may not work on the local machine:
local_host$ jupyter notebook
The workaround is very similar to the example above i.e. using a different port available.
local_host$ jupyter notebook port=8889
Currently, the only way I know to reset all ports is to reboot the local machine.