5.1. Introduction to notebooks with Jupyter Lab

5.1.1. Aim of this section

  • Whet your appetite on notebook technologies.

  • Discover and get comfortable with Jupyter notebooks in Jupyterlab.

  • Create your first notebook

5.1.2. Install

For more exhaustive guidelines, you can see the official Jupyter documentation.

Using conda, create a jupyter environment with jupyter lab:

conda create -n jupyter jupyterlab

Once installed, you can then activate the jupyter environment and start the jupyter server as follows:

conda activate jupyter
jupyter lab

And open the specified URL in your internet browser (Chrome or Firefox are better supported). By default, the address will be http://localhost:8888

5.1.3. Basic functioning of Jupyter notebooks

5.1.3.1. Using the interface

Jupyter notebooks are organized in cells which are executed separately. Therefore the code execution is not necessarily sequential as in classical scripts. Cell execution order can be witnessed in the value in between squared brackets [] on the left of the corresponding cell. You should be careful with cell execution order in jupyter.

Jupyter cells can be of various types:

  • Code: actual code blocks of your notebooks (which will be interpreted).

  • Markdown: To integrate explanations within your notebooks.

  • Raw: Raw it is...

  • And more...

Jupyter use 2 editing modes:

  • Command mode (Esc): To organize cells and browse the notebook.

    Using keystrokes, you can:

    ↑, ↓

    Move around up and down in cells

    a

    Add cell above

    b

    Add cell below

    dd

    Delete cell

    You can as well use drag and drop with your mouse to move cells or groups of cells around. Cell type can be changed in command mode using the graphical interface or shortcuts:

    y

    Code

    m

    Markdown

    r

    Raw

Markdown is a lightweight markup language with plain text formatting syntax. To help you remember the markdown syntax and format your markdown cells, here is a cheatsheet.

  • Edit mode (Enter/Return): To edit the active cell. Then to execute the cell you have 2 options:

Ctrl + Enter

Execute current cell

Shift + Enter

Execute current cell and move to next cell

5.1.3.2. Jupyter magic commands

Jupyter provides some functionalities which can be added at the beginning of a code cell called magic commands. Here is an exhaustive list of Jupyter magic commands.

Here are some example of useful magic commands:

  • Run cell with bash in subprocess:

    %%bash
    

    The exclamation mark character ! can be used as well to execute the following line in a bash subprocess. For example:

    ! ls
    
  • Compute execution time:

    %%timeit
    
  • Load more extension for the notebook:

    %load_ext autoreload
    
  • Automatically reload a module imported in a Jupyter notebook if the module has changed locally:

    %autoreload 2
    

5.1.4. Exercises

The aim here is to get comfortable in Jupyterlab.

5.1.4.1. Exercise

  • Start a Jupyterlab server.

  • Create a new notebook with a python3 kernel.

  • Create, delete and move cells around using shortcuts and graphical interface.

Note

A kernel provides a programming language support in Jupyter. Kernels are available for Python, R, Julia, and many more.

5.1.4.2. Exercise

In the notebook, create a code cell with simple python code inside with a print statement, execute the cell and witness its output.

For example:

print("Hello World !")

5.1.4.3. Exercise

In the notebook, create a markdown cell with:

Render (execute) the cell to display the cell with a pretty formatting.

5.1.4.4. Exercise

Grasp the concept of cell execution by creating three cells:

  • 1 cell defining a variable with a simple value. (e.g. myvar=12)

  • 1 cell defining the same variable with a different value from the previous cell (e.g. myvar=42)

  • 1 cell printing the value of the variable (print(myvar)).

Witness how execution order of your cells can affect the result of the cell printing the output.

5.1.4.5. Exercise

Using a Jupyter magic command, create a cell listing the files in the current directory using a bash subprocess.

5.1.4.6. Exercise

Using the graphical interface, export your notebook as html file.

5.1.5. More documentation

Jupyter lab: https://jupyterlab.readthedocs.io/en/latest/

Jupyter notebook: https://jupyter-notebook.readthedocs.io/en/stable/

5.1.5.1. A note about Extensions

Jupyter lab is highly extensible. A lot of extensions are developed by the Jupyter community and can allow you to tune your Jupyter configuration. Here is a couple of examples:

  • Visualize and fold your code according to the table of content of your notebook: toc

  • Import/Export your notebook as simple script/markdown files: jupytext.

  • Deal with your conda environments in Jupyter lab: nbconda

You can discover and install much more extensions using the Extension Manager in the Jupyter lab interface.