Tips and Tricks for Python

Tips and trick for python

Image credit: [GPT-4 Generated]

Tips and Trick for Python

Using conda environment inside Jupyter Lab

Step 1: Install nb_conda_kernel in base environment

conda install -c conda-forge nb_conda_kernels

Step 2: Install ipykernel in your environment

conda activate my-env
conda install ipykernel

Step 3: Run jupyter lab from your base environment

conda deactivate
jupyter lab

SSH Tunnel Jupyter lab and notebook from remote cluster

First of all you should be able to log in to the cluster using ssh.

In your remote cluster, start the jupyter lab or notebook using the following options. In place of port number, you can give any four digit number. --no-browser tells the system not to start a browser for the jupyter lab or notebook session.

jupyter notebook --no-browser --port 1234

or

jupyter lab --no-browser --port 1234

If you don’t want to occupy the terminal while using the jupyter lab/notebook, use nohup infront of the above command and --ip=0.0.0.0 & at the end.

nohup jupyter notebook --no-browser --port 1234 --ip=0.0.0.0 &

or

nohup jupyter lab --no-browser --port 1234 --ip=0.0.0.0 &

After that go to your local system, and create the ssh tunneling with the same port number. userid@ipaddress is your login credential for remote system. You will be asked to provide password if you have not set up passwordless login.

ssh -NL 1234:localhost:1234 userid@ipaddress

Now you can copy and pase anyone of the following link from your remote cluster to the browser in your local system to run jupyter.

http://127.0.0.1:1234/lab?token=<some_token>

or

http://localhost:1234/lab?token=<some_token>
Shibu Meher
Shibu Meher
PhD Scholar in Materials Research

My research interests include Material Physics, Computational Sciences, Artificial Intelligence and Quantum Computing.