Introduction to Jupyter notebooks and Jupyter Lab ¶
Date: May 12th, 2022
Author: Loïc Maurin (loic.maurin@universite-paris-saclay.fr)
Context¶
Project Jupyter is a non-profit, open-source project, born out of the IPython Project in 2014 as it evolved to support interactive data science and scientific computing across all programming languages. Jupyter will always be 100% open-source software, free for all to use and released under the liberal terms of the modified BSD license.
Jupyter website
Jupyter documentation
Jupyter Github
Jupyter is a large project with many tools. In this introduction, we will focus on Jupyter notebooks and Jupyter Lab. Jupyter stands for Julia, Python and R. In our case, we are mostly interested in Python functionnalities, but the framework is common for the three languages (actually other languages are also included now). Historically, Jupyter notebooks could be seen as an extension to the IPython functionalities and usage. Later on, an interface around Jupyter notebooks was developped to provide some IDE like functionalities, this is Jupyter Lab. Jupyter Lab is now the recommended tool to work on your Jupyter notebooks.
Sources¶
Many great tutorials about Jupyter notebooks already exists. We will use some of them. Others are listed if you want to explore them.
Use cases¶
From the Code Refinery tutorial.
from IPython.display import IFrame
IFrame(src="https://coderefinery.github.io/jupyter/motivation/", width='100%', height='700px')
- Gravitational waves: a quick look at short segments of data from the Gravitational Wave Open Science Center.
- Activity Inequality Project: notebooks to reproduce plots from the paper.
What is it good at?
- Quick linear analysis: read data, do some processing, make some plots.
- Testing libraries.
- Sharing and explaining your analysis (mixing code, text and images).
- Teaching.
- Provide codes to reproduce your papers plots.
What is it not meant to do?
- Develop long programs or complex codes.
[WARNING] A notebook is not a library, it is a very convenient tool but it will not replace your code editor!
How to install and use it?¶
Jupyter tools can be installed with your favorite Python package installer, like pip or conda. As always in the Code Club, we recommend to do it inside a virtual environnement:
- Create a directory for this tutorial and move into it.
- If you prefer virtualenv + pip:
python -m venv .venv
source .venv/bin/activate
pip install jupyter-lab
- If you prefer conda:
conda create -n Jupyter
conda activate Jupyter
conda install jupyterlab
Then just run jupyter-lab
command in your terminal, your browser should open (maybe you need to click on the link in the terminal) and you will reach the Jupyter Lab interface.
First notebook: calculating pi using Monte Carlo methods¶
From the Code Refinery tutorial.
from IPython.display import IFrame
IFrame(src="https://coderefinery.github.io/jupyter/first-notebook/", width='100%', height='500px')
# importing modules that we will need
!pip install numpy matplotlib
import random
import matplotlib.pyplot as plt
import numpy as np
Requirement already satisfied: numpy in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (1.22.3) Requirement already satisfied: matplotlib in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (3.4.1) Requirement already satisfied: cycler>=0.10 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from matplotlib) (0.10.0) Requirement already satisfied: kiwisolver>=1.0.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from matplotlib) (1.3.1) Requirement already satisfied: python-dateutil>=2.7 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from matplotlib) (2.8.2) Requirement already satisfied: pillow>=6.2.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from matplotlib) (8.2.0) Requirement already satisfied: pyparsing>=2.2.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from matplotlib) (2.4.7) Requirement already satisfied: six in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from cycler>=0.10->matplotlib) (1.15.0)
# initializing the number of "throws"
num_points = 1000
# here we "throw darts" and count the number of hits
points = []
hits = 0
for _ in range(num_points):
x, y = random.random(), random.random()
if x*x + y*y < 1.0:
hits += 1
points.append((x, y, "red"))
else:
points.append((x, y, "blue"))
# unzip points into 3 lists
x, y, colors = zip(*points)
# define figure dimensions
fig, ax = plt.subplots()
fig.set_size_inches(6.0, 6.0)
# plot results
ax.scatter(x, y, c=colors)
<matplotlib.collections.PathCollection at 0x7f658ac6e3a0>
# compute and print the estimate
fraction = hits / num_points
4 * fraction
3.068
Ipywidgets: interactive plots¶
IFrame(src="https://ipywidgets.readthedocs.io/en/stable/", width='100%', height='500px')
interact
function¶
You can interact with your code with the interact
function from ipywidgets
. Let's have a first look at it.
!pip install ipywidgets
from ipywidgets import interact
Requirement already satisfied: ipywidgets in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (7.7.0) Requirement already satisfied: nbformat>=4.2.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (5.4.0) Requirement already satisfied: ipykernel>=4.5.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (6.13.0) Requirement already satisfied: ipython>=4.0.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (8.3.0) Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (1.1.0) Requirement already satisfied: widgetsnbextension~=3.6.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (3.6.0) Requirement already satisfied: ipython-genutils~=0.2.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (0.2.0) Requirement already satisfied: traitlets>=4.3.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipywidgets) (5.2.0) Requirement already satisfied: tornado>=6.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (6.1) Requirement already satisfied: matplotlib-inline>=0.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (0.1.2) Requirement already satisfied: packaging in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (21.0) Requirement already satisfied: debugpy>=1.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (1.6.0) Requirement already satisfied: psutil in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (5.9.0) Requirement already satisfied: nest-asyncio in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (1.5.5) Requirement already satisfied: jupyter-client>=6.1.12 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (7.3.1) Requirement already satisfied: pickleshare in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.7.5) Requirement already satisfied: jedi>=0.16 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.18.0) Requirement already satisfied: stack-data in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0) Requirement already satisfied: decorator in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (5.0.7) Requirement already satisfied: setuptools>=18.5 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (47.1.0) Requirement already satisfied: pygments>=2.4.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (2.8.1) Requirement already satisfied: pexpect>4.3 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (4.8.0) Requirement already satisfied: backcall in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0) Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (3.0.18) Requirement already satisfied: jsonschema>=2.6 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets) (4.5.1) Requirement already satisfied: fastjsonschema in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets) (2.15.3) Requirement already satisfied: jupyter-core in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets) (4.10.0) Requirement already satisfied: notebook>=4.4.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from widgetsnbextension~=3.6.0->ipywidgets) (6.4.11) Requirement already satisfied: parso<0.9.0,>=0.8.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets) (0.8.2) Requirement already satisfied: attrs>=17.4.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (21.2.0) Requirement already satisfied: importlib-resources>=1.4.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (5.7.1) Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (0.18.1) Requirement already satisfied: entrypoints in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets) (0.4) Requirement already satisfied: pyzmq>=22.3 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets) (22.3.0) Requirement already satisfied: python-dateutil>=2.8.2 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets) (2.8.2) Requirement already satisfied: argon2-cffi in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (21.3.0) Requirement already satisfied: terminado>=0.8.3 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.13.3) Requirement already satisfied: nbconvert>=5 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (6.5.0) Requirement already satisfied: Send2Trash>=1.8.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.8.0) Requirement already satisfied: jinja2 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (3.1.2) Requirement already satisfied: prometheus-client in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.14.1) Requirement already satisfied: ptyprocess>=0.5 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets) (0.7.0) Requirement already satisfied: wcwidth in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets) (0.2.5) Requirement already satisfied: pyparsing>=2.0.2 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from packaging->ipykernel>=4.5.1->ipywidgets) (2.4.7) Requirement already satisfied: executing in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from stack-data->ipython>=4.0.0->ipywidgets) (0.8.3) Requirement already satisfied: asttokens in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from stack-data->ipython>=4.0.0->ipywidgets) (2.0.5) Requirement already satisfied: pure-eval in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from stack-data->ipython>=4.0.0->ipywidgets) (0.2.2) Requirement already satisfied: zipp>=3.1.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from importlib-resources>=1.4.0->jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (3.8.0) Requirement already satisfied: jupyterlab-pygments in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.2.2) Requirement already satisfied: mistune<2,>=0.8.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.8.4) Requirement already satisfied: bleach in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (5.0.0) Requirement already satisfied: nbclient>=0.5.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.6.3) Requirement already satisfied: pandocfilters>=1.4.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.5.0) Requirement already satisfied: beautifulsoup4 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (4.10.0) Requirement already satisfied: MarkupSafe>=2.0 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.1.1) Requirement already satisfied: tinycss2 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.1.1) Requirement already satisfied: defusedxml in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.7.1) Requirement already satisfied: six>=1.5 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from python-dateutil>=2.8.2->jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets) (1.15.0) Requirement already satisfied: argon2-cffi-bindings in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (21.2.0) Requirement already satisfied: cffi>=1.0.1 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (1.15.0) Requirement already satisfied: soupsieve>1.2 in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from beautifulsoup4->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.3.1) Requirement already satisfied: webencodings in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from bleach->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (0.5.1) Requirement already satisfied: pycparser in /home/lmaurin/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets) (2.21)
def f(x):
print(x)
interact(f, x=10)
interactive(children=(IntSlider(value=10, description='x', max=30, min=-10), Output()), _dom_classes=('widget-…
<function __main__.f(x)>
interact(f, x=False)
interactive(children=(Checkbox(value=False, description='x'), Output()), _dom_classes=('widget-interact',))
<function __main__.f(x)>
interact(f, x="Hello!")
interactive(children=(Text(value='Hello!', description='x'), Output()), _dom_classes=('widget-interact',))
<function __main__.f(x)>
interact(f, x=range(10))
interactive(children=(Dropdown(description='x', options=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), value=0), Output()), _…
<function __main__.f(x)>
interact
as a decorator¶
Maybe a better way to use interact
is to use it as a decorator.
(You might have seen a thing with an @ before the definition of a function. This is called a decorator.).
@interact
def f(x=10):
print(x)
interactive(children=(IntSlider(value=10, description='x', max=30, min=-10), Output()), _dom_classes=('widget-…
@interact
def f(x=(0,10,0.1)):
print(x)
interactive(children=(FloatSlider(value=5.0, description='x', max=10.0), Output()), _dom_classes=('widget-inte…
@interact
def f(x=range(10)):
print(x)
interactive(children=(Dropdown(description='x', options=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), value=0), Output()), _…
Example for plots¶
How should you use it in your notebooks? A very good way to use it is to vary parameters in your plots:
def gaussian(x, a, b, c):
return a * np.exp(-b * (x-c)**2)
@interact
def plot_gaussian(a=(0,10,0.1), b=(1,10,0.1), c=(-5,5,0.1)):
x = np.linspace(-10, 10, 10000)
plt.plot(x, gaussian(x, a, b, c))
plt.ylim(0,10)
interactive(children=(FloatSlider(value=5.0, description='a', max=10.0), FloatSlider(value=5.0, description='b…
You could use it to visually inspect the fit of a model to you data. For example, let's create a noisy gaussian and try to fit it with a polynomial. We will vary the order of the polynomial.
def noisy_gaussian():
# gaussian array y in interval -5 <= x <= 5
nx = 100
x = np.linspace(-5.0, 5.0, nx)
y = gaussian(x, a=2.0, b=0.5, c=1.5)
noise = np.random.normal(0.0, 0.2, nx)
y += noise
return x, y
def fit(x, y, n):
pfit = np.polyfit(x, y, n)
yfit = np.polyval(pfit, x)
return yfit
def plot(x, y, yfit):
plt.plot(x, y, "r", label="Data")
plt.plot(x, yfit, "b", label="Fit")
plt.legend()
plt.ylim(-0.5, 2.5)
plt.show()
x, y = noisy_gaussian()
@interact
def slider(n=(3, 30)):
yfit = fit(x, y, n)
plot(x, y, yfit)
interactive(children=(IntSlider(value=16, description='n', max=30, min=3), Output()), _dom_classes=('widget-in…
Or maybe we want to have the possibility to modify the colorbar of an image to inspect the full dynamic range.
data = np.random.randn(100).reshape(10,10)
@interact
def slider(vmin=(-10,0), vmax=(0,10)):
plt.imshow(data, vmin=vmin, vmax=vmax)
plt.colorbar()
interactive(children=(IntSlider(value=-5, description='vmin', max=0, min=-10), IntSlider(value=5, description=…
Widgets¶
If you want to add more complex behaviours, a large variety of widgets is available for that. To use them, you need to create a widget object that will hold a value that you can set interactively, and then recover its value. Let's see an example:
from ipywidgets import IntSlider
int_slider = IntSlider(
value=7,
min=0,
max=10,
step=1,
description='Test:',
orientation='horizontal',
)
int_slider
IntSlider(value=7, description='Test:', max=10)
print(int_slider.value)
7
For example, you might want to change the color of your plot:
from ipywidgets import ColorPicker
color_picker = ColorPicker(
concise=False,
description='Pick a color',
value='blue',
disabled=False
)
color_picker
ColorPicker(value='blue', description='Pick a color')
plt.plot(np.random.randn(100), color=color_picker.value)
[<matplotlib.lines.Line2D at 0x7f65b16c8100>]
Or select a date from a calendar:
from ipywidgets import DatePicker
date_picker = DatePicker(
description='Pick a Date',
disabled=False
)
date_picker
DatePicker(value=datetime.date(2022, 5, 25), description='Pick a Date')
print(date_picker.value)
print(date_picker.value.strftime("%d %b, %Y"))
2022-05-25 25 May, 2022
A complete list of available widgets is available here.
IFrame(src="https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20List.html", width='100%', height='500px')
How to show your notebook?¶
One goal of a notebook is to let you present your work in a nice and clear format. The good thing about a notebook is that you don't necessarily need to execute the code all the time. Once your notebook is ready, you can share it or show it with all the computation done and look at the plots and texts. There are several ways to render a notebook without starting a Jupyter Lab instance. It means that the person you want to show your results to will not need to have a Python installatio at all to look at your work.
nbconvert
¶
nbconvert
is a command line tool that allows you to convert a Jupyter notebooks to many other formats, like html, pdf, latex, asciidoc, markdown or python.
For example, try jupyter-nbconvert Introduction\ to\ Jupyter\ Lab.ipynb --to html
and open the result in your browser.
Gitlab, Github¶
Gitlab and Github render the Jupyter notebooks directly. So if you store your notebooks in the IAS Gitlab (and you should), sharing them is very easy.
Nbviewer¶
Nbviewer lets you render any Github/Gitlab hosted notebook. Just type the URL of the notebook and it will display it.
IFrame(src="https://nbviewer.org/", width='100%', height='500px')
What if I don't have a Python installation on my computer?¶
There are several options to run a Jupyter notebook remotely.
Binder¶
Binder lets you run a Jupyter Lab instance remotely from any Github/Gitlab public project. Just type the name of your project and it will open a Jupyter Lab instance. Even better, if you project has an environement file (requirements.txt
or environment.yml
), it will automatically create the environment from it, so all your libraries will be available. The only problem with Binder is that it can get saturated sometimes or very slow.
IFrame(src="https://mybinder.org/", width='100%', height='700px')
Jupyter Hub Paris Saclay¶
A Jupyter Hub is hosted at IJC Lab and accessible to all University members: JupyterHub@Paris-Saclay. A JupyterHub lets you leaunch JupyterLab instances and gives you some space to store your notebooks.
IAS machines¶
If you have access to an interactive IAS machine, you can install and run a Jupyter Lab instance there. You will thus have access to all your data on the cluster and the more computing power. It is slightly more complicated than running it on your local computer since you will need to access it remotely. Once you have installed jupyterlab in you default environment (otherwise, you need to source the desired environment before launching jupyterlab), you can user the following command:
ssh <remote-server> 'jupyter-lab --no-browser --ip=0.0.0.0 --notebook-dir=</path/to/your/notebooks> --ServerApp.shutdown_no_activity_timeout 3600 --MappingKernelManager.cull_idle_timeout 3600'
--no-browser
prevents Jupyter from opening a browser on the server.--ip=0.0.0.0
serves the Jupyterlab to the public interface.--notebook-dir
brings you to the desired folder.--ServerApp.shutdown_no_activity_timeout
kills the server after a specified time (in seconds) if no kernel is running.--MappingKernelManager.cull_idle_timeout
kills the kernels after a specified time (in seconds) if the kernels are idle.
In the terminal, you will see a link to open the Jupyterlab in your local browser. It will look like http://server-name:8889/lab?token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
.