In this article you will see how to easily manage Jupyter Notebook and JupyterLab by using the Systemd tooling. This is useful when you want to have an instance running local or on your server that you can manage and monitor.
What is Systemd and why do we need it? Systemd is an init system in Linux used for system intialization and service management. This allows services configured for Systemd to manage and monitor them. This way, you can check if the service is still running, you can set it to automatically restart, you monitor the outputs of the service, and much more. To add Jupyter as a service we have to create a unit file. Let’s have a look how that works.
Systemd uses unit files as its primary way to manage and configure system resources. These files are in the INI file format and are stored in /etc/systemd/system
.
Jupyter Notebook Cheatsheet. Tidy Display of Pandas Dataframes and Inline Images: from IPython.display import Image as IM from IPython.display import display (Other types: file links, audio files, code, and more. See here) Creating and Displaying Interactive Plots: import matplotlib.pyplot as plt%matplotlib inline. Home Instant Answers Jupyter Notebook Cheat Sheet Next Steps. This is the home page for your Instant Answer and can be. JUPYTER NOTEBOOK CHEAT SHEET Learn PYTHON from experts at Keyboard Shortcuts Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. It is used for data cleaning and transformation, numerical simulation, statistical.
But before we start, we have to configure a password for Jupyter. Typically when running Jupyter, it would open a window in your browser with a generated token. In this case it will run in the background as a service and you would need to use a password. This can be done by generating hashed password and salt for use in notebook configuration with:
Which should give you a hash like this one:
For more information on security, have a read on Security in the Jupyter notebook server. Great, now let’s continue to create a file with the name jupyter.service
with the following contents:
Each file has a few sections and each section like [Unit]
, [Service]
, and [Install]
have various directives that are used to configure a service. Most of them should already give a hint on what they are used for, but we will quickly go through some of them. To read more into detail how this works have a look at the tutorial Understanding Systemd Units and Unit Files.
The most important one here is the ExecStart=
directive, which specifies the command to be run. It is important to note, that only absolute paths should be used and that the ExecStart=
directive has to be written in one line. In this case the unit files uses JupyterLab that is installed in the Anaconda base environment. If you use virtualenv instead, change the directive for ExecStart=
to:
The PIDFile=
directive specifies where the process identification number is stored. Make sure to have your user and group in the User=
and Group=
directives. The Restart=
directive specifies if the service should be restarted when the service exits, is killed or a timeout is reached. RestartSec=
specifies how long it should wait until the service will be then restarted. You can read more about the directives in the documentation.
If you use more configuration and want to have it in a bash script instead, you can create a script like the following:
Then, replace the previous ExecStart
with ExecStart=/path/to/script.sh
in the unit file. Also, make sure to make it executable with chmod +x /path/to/script.sh
.
Finally, instead of using the argument --notebook-dir
, you can specify the working directory by using the WorkingDirectory=
directive.
First, move the unit file to /etc/systemd/system/
with:
Then, reload the systemd manager configuration with:
Next, enable the service to start at boot with:
To start the Jupyter service, you need to type:
To check its status, you can type:
Finally, you can monitor the outputs of the service with sudo journalctl -u jupyter -f
. To show the log messages since the last boot (-b
) and without additional fields like timestamp and hostname (-o cat
), type:
This should show you a similar output to the following output:
This really useful if you need to have a look at what Jupyter logs out since some errors and logs are not visible within the notebooks. For more useful commands and arguments have a look at this Systemd cheatsheet.
Now, you should be able to manage and monitor your Jupyter service with Systemd. To learn how to install and configure Jupyter on a server including SSL/TLS, have a look at my previous guide on Installing and Running Jupyter Notebooks on a Server Here are a few further resources when working with systemd:
Image from Wikimedia CommonsPlease enable JavaScript to view the comments powered by Disqus.Related Posts
Intro to Jupyter Notebooks
Vicky Steeves and Nicholas Wolf
Vicky's ORCID: 0000-0003-4298-168X | Nick's ORCID: 0000-0001-5512-6151
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Jupyter Notebooks Overview
- In-browser editing for code with auto-syntax highlighting, indentation, tab completion/introspection
- In-browser code execution, with results attached to the code that generated them
- Easily include math markdown using LaTeX
- Display results of computation in rich media (LaTeX, HTML, SVG, etc.)
Jupyter Notebooks Overview
- A complete computation record of a session, interleaving executable code with text, maths, and rich representations of objects
- Can export to LaTeX, PDF, slideshows, etc. or available from a public URL that renders it as a static webpage
Examples
- Analyzing Whale Tracks, by Roberto De Almeida
- A Reconstruction of 538 2012 Election Model, by Skipper Seabold
Installation
- Install Jupyter Notebooks on Desktop Quickly Using Anaconda: https://www.anaconda.com/download/
- Today’s Example Notebook Workspace: index.ipynb
Jupyter Notebook Cheat Sheet
Jupyter Notebook Cheatsheet
Launch Notebook in a Directory Other than Home:jupyter notebook --notebook-dir=PATH-TO-FOLDER
Code Cells:
- Notebook will evaluate final statement in cell
- Use
shift + enter
to run an active cell - Use
esc
in highlighted cell to toggle command options:esc + L
= show line numbersesc + M
= format cell as Markdown cellesc + a
= insert cell above current cellesc + b
= insert cell below current cell
- Check all current variables: run
%whos
Jupyter Notebook Cheatsheet
from IPython.display import Image as IM
from IPython.display import display
(Other types: file links, audio files, code, and more. See here)
Creating and Displaying Interactive Plots:
import matplotlib.pyplot as plt
%matplotlib inline
Jupyter Notebook Cheatsheet
display(dataframe)
display(image)
Make a Basic Scatter Plot:
x = list of values
y = list of values
plt.scatter(x, y)
plt.xlabel('X Axis')
plt.xlabel('Y Axis')
Jupyter Notebook Cheatsheet
Markdown Cells:- When run, a markdown cell will display markdown or html that you enter (that means all sort of rich content, including images)
- Essential markdown summary: https://daringfireball.net/projects/markdown/syntax
Moving a Notebook to the Web
Using NBViewer: https://nbviewer.jupyter.org/:- Put notebook (.ipynb) file on the web (e.g. Github, Gitlab...somewhere so that URL is http://NAME-OF-NOTEBOOK.ipynb
- Enter the URL into NBViewer.
- Click Go! (check out example here)
Moving a Notebook to the Web
Using NBConvert: https://github.com/jupyter/nbconvert:- In directory of your notebook, enter:
jupyter nbconvert --output-dir='./docs' --to html *.ipynb
- This gives you a directory called “docs” which has HTML files. If you put these files on GitHub or GitLab, you can render them as webpages.
Example: https://vickysteeves.gitlab.io/Stories-Code-Data-Py/
More on Jupyter Notebooks
http://bebi103.caltech.edu.s3-website-us-east-1.amazonaws.com/2015/tutorials/t0b_intro_to_jupyter_notebooks.html#Best-practices-for-code-cells
Jupyter.org Documentation for Jupyter Notebooks:
https://jupyter-notebook.readthedocs.io/en/stable/
Jupyter Notebook Tutorial Pdf
Questions?
Jupiter Notebook Markdown Guide
Email us: vicky.steeves@nyu.edu & nicholas.wolf@nyu.edu
Jupyter Cheat Sheet Pdf
Learn more about RDM: guides.nyu.edu/data_management
Jupyter Notebook Markdown Cheatsheet Pdf
Get this presentation: guides.nyu.edu/data_management/resources
Jupyter Notebook Markdown Cheat Sheet
Make an appointment: guides.nyu.edu/appointment
Vicky's ORCID: 0000-0003-4298-168X | Nick's ORCID: 0000-0001-5512-6151
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.