Skip to content

Jupyter Notebook Maintenance

Will Kibbe edited this page Aug 29, 2023 · 9 revisions

Why do the labs contain Jupyter Notebooks?

Jupyter Notebooks allow lab users to interact with Python code through a web browser instead of a terminal. This allows for more flexible output. While the terminal can only display text output, Jupyter Notebooks can create graphs and display pictures, making the labs more engaging and informative.

Which labs contain Jupyter Notebooks?

As of 28 August 2023, the following labs contain Jupyter Notebooks:

  • PyTorch
  • TensorFlow
  • Managing Jupyter Notebooks

Adding Jupyter Notebooks to a lab

Configuring the lab environment

Create a folder for Jupyter Notebooks

Run the following command to make a folder called jupyter_source:

mkdir /root/jupyter_source

Install software using Conda

First, if you haven't done so already install Conda (note that you cannot run exec $SHELL inside a track script):

subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
dnf install conda -y
conda init bash
exec $SHELL

Next, if you don't have a Conda environment already, make one. In this example, we will call it jupyter-env:

conda create --name jupyter-env python=3.9 -y

Next, install JupyterLab and Matplotlib. While Matplotlib is not required, many Jupyter Notebooks require it.

conda run -n jupyter-env pip install jupyter matplotlib

Activating the Notebook

Run the following command to activate the Notebook:

conda run -n jupyter-env jupyter notebook --ip=* --no-browser --allow-root -NotebookApp.password='redhat' -NotebookApp.token='redhat' --notebook-dir="/root/jupyter_source"

If you wish to script the activation of your notebook, run the following command to activate the notebook in the background:

nohup conda run -n jupyter-env jupyter notebook --ip=* --no-browser --allow-root -NotebookApp.password='redhat' -NotebookApp.token='redhat' --notebook-dir="/root/jupyter_source" </dev/null >/dev/null 2>&1 &

Note: In this example, the notebook login token/password will be set to redhat and it will listen on all IP addresses. Don't use this configuration in production.

When you configure the notebook as a tab within Instruqt, do it like this:

- title: Juypter Notebook
  type: service
  hostname: rhel
  port: 8888
  new_window: true

Importing the Notebook into the lab

Fetching from the web

Fetch a notebook like this:

wget --directory-prefix=/root/jupyter_source/ <url-to-raw-notebook>

Hard-coding the Notebook

Will the intern recommends just hard-coding the notebook because it makes life easier. Hardcode the notebook by inserting the following command into an Instruqt setup script:

tee /root/jupyter_source/notebook_name.ipynb << EOF
<< Raw Notebook text >>
EOF

Storing Jupyter Notebooks in the Git Repository

Store Jupyter Notebook files inside the assets folder for the relevant labs. This way, the Notebook can be easily edited. After making edits, copy and paste the raw text from the Notebook file into the setup script.