Helix/bwVisu/JupyterLab: Difference between revisions

From bwHPC Wiki
< Helix‎ | bwVisu
Jump to navigation Jump to search
(created page)
 
(Restructured page, improved explanation for sharing virtual environments)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[https://jupyter.org/ JupyterLab] is an integrated development environment (IDE) that provides a flexible and scalable interface for the Jupyter Notebook system. It supports interactive data science and scientific computing across over 40 programming languages (including Python, Julia, and R).
[https://jupyter.org/ JupyterLab] is an integrated development environment (IDE) that provides a flexible and scalable interface for the Jupyter Notebook system. It supports interactive data science and scientific computing across over 40 programming languages (including Python, Julia, and R).


== Python version ==
== Change Python Version ==


You can see the default python version by running <code>python --version</code>
The default python version can be seen by running <code>python --version</code> in the terminal.


A different python version can be installed into a new virtual environment and then registered as IPython kernel for the usage in JupyterLab. This is explained in the chapter [[#Add_packages_via_conda_environments | add packages via conda environments]].
<ol style="list-style-type: decimal;">
<li>Activate the Miniconda module</li>
<li>Create a virtual environment with a custom python version via conda:
<syntaxhighlight lang="bash">conda create --name <myenv> python=<python version></syntaxhighlight></li>
<li>Install the ipykernel package:
<syntaxhighlight lang="bash">conda install ipykernel</syntaxhighlight></li>
<li>Register your virtual environment as custom kernel to Jupyter:
<syntaxhighlight lang="bash">python3 -m ipykernel install --user --name=<myKernel></syntaxhighlight></li>
<li>Select your newly created kernel in Jupyter</li></ol>


== Python packages ==
== Install Python Packages ==

Python packages can be added by installing them into a virtual environment and then creating an IPython kernel from the virtual environment. </br>
<u>Kernels can be shared</u>. See the notes below. </br>
If you want to move a virtual environment, it is adivsed to recreate it in the new place. Otherwise, dependencies based on relative paths will break.

# Create a virtual environment with...
#* [[#Add_packages_via_venv_virtual_environments | ...venv]] or
#* [[#Add_packages_via_Conda_virtual_environments | ...conda]] (choose this option if you want to install a different python version) or
#* ...[https://docs.astral.sh/uv/getting-started/ uv] if you want to install a different python version but don't want to use conda.
# [[#Create_an_IPython_kernel | Create an IPython kernel]] from the virtual environment
# Use the kernel within JupyterLab
#* By default new kernels are saved under <code>~/.local/share/jupyter</code> and this location is automatically detected. Therefore, new kernels are directly available.
#* If the kernel is saved somewhere else, the path can be provided in the "Kernel path" field when submitting the job. For a kernel placed under <code>path_to_parent_dir/share/jupyter/kernels/my_kernel</code> the needed path would be <code>path_to_parent_dir</code>.

<u>Notes regarding the sharing of IPython kernels</u>
* The virtual environment and the kernel need to be placed in a shared directory. For example at SDS@hd.
* There could be a subdirectory for the virtual environments and one for the kernels.
* The path to the kernels is saved in the environment variable $JUPYTER_DATA_DIR. Jupyter relevant paths can be seen with <code>jupyter --paths</code>


=== Add packages via ''venv'' virtual environments ===
=== Add packages via ''venv'' virtual environments ===
More information about venv or other python virtual environments can be found at the [[Development/Python | Python]] page.


Steps for creating a '''venv''' virtual environment:
<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
<li>Open a terminal and type:
<li>Open a terminal.</li>
<li>Create a new virtual evironment:
<syntaxhighlight lang="bash">python3 -m venv <myEnv></syntaxhighlight></li>
<syntaxhighlight lang="bash">python3 -m venv <env_parent_dir>/<env_name></syntaxhighlight>
<li>Activate your environment:
* <code>env_parent_dir</code> is the path to the folder where the virtual environment shall be created. Relative paths can be used.
<syntaxhighlight lang="bash">source <myEnv>/bin/activate</syntaxhighlight></li>
* Caution: If you you want to share the environment with others, make sure to already create it in the shared place.
<li>Install your packages:
</li>
<li>Activate the environment:
<syntaxhighlight lang="bash">source <env_parent_dir>/<env_name>/bin/activate</syntaxhighlight></li>
<li>Update pip and install packages:
<syntaxhighlight lang="bash">pip install -U pip
<syntaxhighlight lang="bash">pip install -U pip
pip install <mypackage></syntaxhighlight></li>
pip install <packagename></syntaxhighlight></li>
<li> [[#Create_an_IPython_kernel | Create an IPython kernel]]</li>
<li>Install the ipykernel package:
</ol>
<syntaxhighlight lang="bash">pip install ipykernel</syntaxhighlight></li>
<li>Register your virtual environment as custom kernel to Jupyter:
<syntaxhighlight lang="bash">python3 -m ipykernel install --user --name=<myKernel></syntaxhighlight></li></ol>


=== Add packages via miniconda ===
=== Add packages via Conda virtual environments ===
More information about using conda can be found at the [[Development/Conda | Conda]] page.


<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
<li>Load the miniconda module by clicking first on the blue hexagon icon on the left-hand side of Jupyter's start page and then on the &quot;load&quot; Button right of the entry for miniconda in the software module menu.
<li>Load the miniforge module by clicking first on the blue hexagon icon on the left-hand side of Jupyter's start page and then on the &quot;load&quot; button right of the entry for miniforge in the software module menu.
Open a terminal and complete the miniconda setup with:
<li>Open a terminal.</li>
<li>Create a new virtual environment:
<pre>source $MINICONDA_HOME/etc/profile.d/conda.sh </pre></li>
* If you are the only person using the environment, you can install it in your home directory:
<li>Open a terminal and create a conda environment:
<syntaxhighlight lang="bash">conda create --name <myenv></syntaxhighlight></li>
<syntaxhighlight lang="bash">conda create --name <env_name> python=<python version></syntaxhighlight>
* If you want to install it into a different directory, for example a shared place:
<syntaxhighlight lang="bash">conda create --prefix <path_to_shared_directory>/<env_name> python=<python version>
</syntaxhighlight>
</li>
<li>Activate your environment:
<li>Activate your environment:
<syntaxhighlight lang="bash">conda activate <myenv></syntaxhighlight></li>
<syntaxhighlight lang="bash">conda activate <myenv></syntaxhighlight></li>
<li>Install your packages:
<li>Install your packages:
<syntaxhighlight lang="bash">conda install <mypackage></syntaxhighlight></li>
<syntaxhighlight lang="bash">conda install <mypackage></syntaxhighlight></li>
<li> [[#Create_an_IPython_kernel | Create an IPython kernel]]</li>
</ol>

== Create an IPython Kernel ==

Python kernels are implementations of the Jupyter notebook environment for different languages or virtual environments. You can switch between kernels easily, allowing you to use the best tool for a specific task.
conda_kernels

=== Create a kernel from a virtual environment ===

<ol>
<li>Activate the virtual environment.</li>
<li>Install the ipykernel package:
<li>Install the ipykernel package:
<syntaxhighlight lang="bash">conda install ipykernel</syntaxhighlight></li>
<syntaxhighlight lang="bash">pip install ipykernel</syntaxhighlight></li>
<li>Register your virtual environment as custom kernel to Jupyter:
<li>Register the virtual environment as custom kernel to Jupyter.
* If you are the only person using the environment:
<syntaxhighlight lang="bash">python3 -m ipykernel install --user --name=<myKernel></syntaxhighlight></li></ol>
<syntaxhighlight lang="bash">python3 -m ipykernel install --user --name=<kernel_name></syntaxhighlight>
The kernel can be found under <code>~/.local/share/jupyter/kernels/</code>.
* If you installed the environment in a shared place and want to have the kernel there as well:
<syntaxhighlight lang="bash">python3 -m ipykernel install --prefix <path_to_kernel_folder> --name=<kernel_name></syntaxhighlight>
The kernel can then be found under <code>path_to_kernel_folder/share/jupyter/kernels/<kernel_name></code>. As long as the same <code>path_to_kernel_folder</code> is used, all kernels will be saved next to each other in "kernels".
</li>
</ol>


=== Multi-Language Support ===
=== Multi-Language Support ===


JupyterLab supports over 40 programming languages including Python, R, Julia, and Scala. This is achieved through the use of different kernels, which are implementations of the Jupyter notebook environment for each language. You can switch between kernels easily, allowing you to use the best tool for a specific task.
JupyterLab supports over 40 programming languages including Python, R, Julia, and Scala. This is achieved through the use of different kernels.


==== R Kernel ====
Example of switching kernels in a notebook:


<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
Line 70: Line 108:


<li>Start kernel 'R 4.2' as console or notebook</li>
<li>Start kernel 'R 4.2' as console or notebook</li>
</ol>
</ol>
</ol>


==== Julia Kernel ====


Load the math/julia module. Open the Terminal.
=== Interactive Widgets ===

<pre>
julia
]
add IJulia
</pre>

After that, Julia is available as a kernel.

== Interactive Widgets ==


JupyterLab supports interactive widgets that can create UI controls for interactive data visualization and manipulation within the notebooks. Example of using an interactive widget:
JupyterLab supports interactive widgets that can create UI controls for interactive data visualization and manipulation within the notebooks. Example of using an interactive widget:
Line 84: Line 134:
== FAQ ==
== FAQ ==


<ol><li>'''My conda commands are interrupted with message 'Killed'.'''</br>
<ol style="list-style-type: decimal;">
Request more memory when starting Jupyter.</li>
<li><p>Q: My conda commands are interrupted with message 'Killed'. What can I do?</p>
<li>'''How can I navigate to my SDS@hd folder in the file browser?'''</br>

Open a terminal and set a symbolic link to your SDS@hd folder in your home directory. For example:</li>
<p>A: Request more memory when starting Jupyter. In the job settings open 'Cluster Settings' and look for the option 'Memory / node'.</p></li>
<pre>cd $HOME
<li><p>Q: How can I navigate to my SDS@hd folder in the file browser?</p>
mkdir sds-hd

cd sds-hd
<p>A: Open a terminal and set a symbolic link to your SDS@hd folder in your home directory. For example:</p>
ln -s /mnt/sds-hd/sd16a001 sd16a001</pre>
<pre>$ cd $HOME
<li>'''Jupyterlab doesn't let me in but asks for a password.'''</br>
$ mkdir sds-hd
Try using more memory for the job. If this doesn't help, try using the inkognito mode of your browser as the browser cache might be the problem.</li>
$ cd sds-hd
<li>'''I prefer VSCode over JupyterLab. Can I start a JupyterLab job and then connect with it via VSCode?'''</br>
$ ln -s /mnt/sds-hd/sd16a001 sd16a001</pre></li>
This is not possible. Please start the job directly on Helix instead. You can find the instructions at the [[Development/VS_Code#Connect_to_Remote_Jupyter_Kernel | VSCode page]].</li>
<li><p>Q: Jupyterlab doesn't let me in but asks for a password.
</ol>

A: Try using more memory for the job. If this doesn't help, try using the inkognito mode of your browser as the browser cache might be the problem.</p></li>
<li><p>Q: I prefer VSCode over JupyterLab. Can I start a JupyterLab job and then connect with it via VSCode?

A: This is possible but, if you are a Helix user as well, it is not recommended as it wastes bwVisu resources. Please start the job directly on Helix instead. You can follow these steps:</p>
<ul>
<li>normal Slurm job with a Jupyter instance (jupyter lab --no-browser --ip=0.0.0.0 --port=somenumber) and connect to that? That way, you could also automate most of the process instead of having to log in to a web page.</li></ul>
</li></ol>

Latest revision as of 20:38, 28 August 2025

JupyterLab is an integrated development environment (IDE) that provides a flexible and scalable interface for the Jupyter Notebook system. It supports interactive data science and scientific computing across over 40 programming languages (including Python, Julia, and R).

Change Python Version

The default python version can be seen by running python --version in the terminal.

A different python version can be installed into a new virtual environment and then registered as IPython kernel for the usage in JupyterLab. This is explained in the chapter add packages via conda environments.

Install Python Packages

Python packages can be added by installing them into a virtual environment and then creating an IPython kernel from the virtual environment.
Kernels can be shared. See the notes below.
If you want to move a virtual environment, it is adivsed to recreate it in the new place. Otherwise, dependencies based on relative paths will break.

  1. Create a virtual environment with...
    • ...venv or
    • ...conda (choose this option if you want to install a different python version) or
    • ...uv if you want to install a different python version but don't want to use conda.
  2. Create an IPython kernel from the virtual environment
  3. Use the kernel within JupyterLab
    • By default new kernels are saved under ~/.local/share/jupyter and this location is automatically detected. Therefore, new kernels are directly available.
    • If the kernel is saved somewhere else, the path can be provided in the "Kernel path" field when submitting the job. For a kernel placed under path_to_parent_dir/share/jupyter/kernels/my_kernel the needed path would be path_to_parent_dir.

Notes regarding the sharing of IPython kernels

  • The virtual environment and the kernel need to be placed in a shared directory. For example at SDS@hd.
  • There could be a subdirectory for the virtual environments and one for the kernels.
  • The path to the kernels is saved in the environment variable $JUPYTER_DATA_DIR. Jupyter relevant paths can be seen with jupyter --paths

Add packages via venv virtual environments

More information about venv or other python virtual environments can be found at the Python page.

Steps for creating a venv virtual environment:

  1. Open a terminal.
  2. Create a new virtual evironment:
    python3 -m venv <env_parent_dir>/<env_name>
    
    • env_parent_dir is the path to the folder where the virtual environment shall be created. Relative paths can be used.
    • Caution: If you you want to share the environment with others, make sure to already create it in the shared place.
  3. Activate the environment:
    source <env_parent_dir>/<env_name>/bin/activate
    
  4. Update pip and install packages:
    pip install -U pip
    pip install <packagename>
    
  5. Create an IPython kernel

Add packages via Conda virtual environments

More information about using conda can be found at the Conda page.

  1. Load the miniforge module by clicking first on the blue hexagon icon on the left-hand side of Jupyter's start page and then on the "load" button right of the entry for miniforge in the software module menu.
  2. Open a terminal.
  3. Create a new virtual environment:
    • If you are the only person using the environment, you can install it in your home directory:
    conda create --name <env_name> python=<python version>
    
    • If you want to install it into a different directory, for example a shared place:
    conda create --prefix <path_to_shared_directory>/<env_name> python=<python version>
    
  4. Activate your environment:
    conda activate <myenv>
    
  5. Install your packages:
    conda install <mypackage>
    
  6. Create an IPython kernel

Create an IPython Kernel

Python kernels are implementations of the Jupyter notebook environment for different languages or virtual environments. You can switch between kernels easily, allowing you to use the best tool for a specific task. conda_kernels

Create a kernel from a virtual environment

  1. Activate the virtual environment.
  2. Install the ipykernel package:
    pip install ipykernel
    
  3. Register the virtual environment as custom kernel to Jupyter.
    • If you are the only person using the environment:
    python3 -m ipykernel install --user --name=<kernel_name>
    

    The kernel can be found under ~/.local/share/jupyter/kernels/.

    • If you installed the environment in a shared place and want to have the kernel there as well:
    python3 -m ipykernel install --prefix <path_to_kernel_folder> --name=<kernel_name>
    

    The kernel can then be found under path_to_kernel_folder/share/jupyter/kernels/<kernel_name>. As long as the same path_to_kernel_folder is used, all kernels will be saved next to each other in "kernels".

Multi-Language Support

JupyterLab supports over 40 programming languages including Python, R, Julia, and Scala. This is achieved through the use of different kernels.

R Kernel

  1. On the cluster:
    $ module load math/R
    $ R
    > install.packages('IRkernel')
  2. On bwVisu:
    1. Start Jupyter App
    2. In left menu: load math/R
    3. Open Console:
    4. $ R
      > IRkernel::installspec(displayname = 'R 4.2')
    5. Start kernel 'R 4.2' as console or notebook

Julia Kernel

Load the math/julia module. Open the Terminal.

julia
]
add IJulia

After that, Julia is available as a kernel.

Interactive Widgets

JupyterLab supports interactive widgets that can create UI controls for interactive data visualization and manipulation within the notebooks. Example of using an interactive widget:

from ipywidgets import IntSlider
slider = IntSlider()
display(slider)

These widgets can be sliders, dropdowns, buttons, etc., which can be connected to Python code running in the backend.

FAQ

  1. My conda commands are interrupted with message 'Killed'.
    Request more memory when starting Jupyter.
  2. How can I navigate to my SDS@hd folder in the file browser?
    Open a terminal and set a symbolic link to your SDS@hd folder in your home directory. For example:
  3. cd $HOME
    mkdir sds-hd
    cd sds-hd
    ln -s /mnt/sds-hd/sd16a001 sd16a001
  4. Jupyterlab doesn't let me in but asks for a password.
    Try using more memory for the job. If this doesn't help, try using the inkognito mode of your browser as the browser cache might be the problem.
  5. I prefer VSCode over JupyterLab. Can I start a JupyterLab job and then connect with it via VSCode?
    This is not possible. Please start the job directly on Helix instead. You can find the instructions at the VSCode page.