Jupyter Notebook Setup#
This section explains three typical workflows for using Jupyter in the PyClm101 environment:
Run Jupyter locally
Run Jupyter on a remote server (via SSH)
Convert a Jupyter notebook to a Python script
π₯οΈ Using Jupyter on Your Local Machine#
First, activate your environment:
conda activate geocat
Then start JupyterLab (recommended for local use):
jupyter lab
Or launch the classic Jupyter Notebook interface:
jupyter notebook
Your browser should open automatically. If not, visit the URL printed in the terminal (typically http://localhost:8888
).
π Using Jupyter on a Remote Server#
This is useful when working on a high-performance computing (HPC) cluster or university server.
π‘ Tip: On some remote servers, setting up
Jupyter Notebook
may be easier and more reliable thanJupyterLab
due to browser compatibility and firewall issues.
π Step 1: SSH into the remote server#
ssh your_username@your.server.address.edu
Replace
your_username
andyour.server.address.edu
with your actual credentials.
π Step 2: Launch Jupyter on the remote server#
Activate your conda environment:
conda activate geocat
Option 1: Start Jupyter Notebook (recommended for remote use)#
nohup jupyter notebook --no-browser --port=8888 --ip=0.0.0.0 > jupyter_notebook.log 2>&1 &
Option 2: Start JupyterLab (may not work on all remote setups)#
nohup jupyter lab --no-browser --port=8888 --ip=0.0.0.0 > jupyter_lab.log 2>&1 &
β οΈ Notes:#
Jupyter will automatically generate a token or password the first time you run it, which is required to connect.
If you see an βinvalid tokenβ error, check the log file (
jupyter_notebook.log
orjupyter_lab.log
) to find the correct URL and token.If you see port conflict errors, try using an alternative port, e.g.
--port=8899
.
Example:
nohup jupyter notebook --no-browser --port=8899 --ip=0.0.0.0 > jupyter_notebook.log 2>&1 &
π§© Step 3: Tunnel to the remote Jupyter server from your local machine#
Open a new terminal on your local machine and run:
ssh -L 8888:localhost:8888 your_username@your.server.address.edu
If using a different port (e.g., 8899), adjust the command:
ssh -L 8899:localhost:8899 your_username@your.server.address.edu
Then open your browser and visit:
http://localhost:8888
Or
http://localhost:8899
if you changed the port.
π Getting the token#
To find the token, check the output inside the log file:
cat jupyter_notebook.log
Look for a line like:
http://localhost:8888/?token=xxxxxxxxxxxxxxxx
π Converting Jupyter Notebooks to Python Scripts#
To convert a notebook (.ipynb
) to a Python script:
jupyter nbconvert --to script my_notebook.ipynb
This will generate my_notebook.py
in the same directory β useful for sharing, version control, or running as a batch script.
π¨ Troubleshooting#
π§ Check if the port is already used on the remote server#
Method 1: Using lsof
#
lsof -i :8888
If no output is shown, the port is free. If you see an entry, another process is using it.
Method 2: Using ss
(recommended)#
ss -tuln | grep 8888
Method 3: Using netstat
#
netstat -tuln | grep 8888
π οΈ Common Issues#
Issue |
Possible Cause |
Solution |
---|---|---|
Port already in use |
Another service is using port |
Use a different port (e.g. |
Invalid token |
Wrong token or expired session |
Check |
No browser opening |
Use the URL shown in terminal |
Copy and paste URL with token into your browser |
Canβt connect via SSH |
Wrong address or user |
Double-check your SSH command and credentials |