Commit 5017ad66 authored by Etienne Kornobis's avatar Etienne Kornobis
Browse files

Adding jupyterlab handson

parent 6ce0b8cc
%% Cell type:markdown id:systematic-springer tags:
# Introduction to JupyterLab
## Aim of this section
- Whet your appetite on notebook technologies.
- Discover, get comfortable with Jupyterlab and create notebooks.
## 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
## Basic functioning of Jupyter notebooks
### 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:
| key | effect |
| ↑, ↓ | Move 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:
|key|Switch cell to this mode|
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 the next cell|
### 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:
%% Cell type:code id:wanted-ethiopia tags:
``` python
echo "This is a bash script"
for i in {1..3}; do echo $i; done
echo "Over and out"
%% Cell type:markdown id:verbal-intake tags:
- The exclamation mark character ``!`` can be used as well to execute the following line in a bash subprocess. For example:
%% Cell type:code id:naval-intensity tags:
``` python
! echo "This is executed in a bash subprocess"
%% Cell type:markdown id:dynamic-heaven tags:
- `%timeit` can be used to check for execution times:
%% Cell type:code id:above-recognition tags:
``` python
%timeit for _ in range(1000): True
%% Cell type:markdown id:simple-breathing tags:
- Load more extension for the notebook, for example `autoreload` is useful extension to automatically reload a module imported in a Jupyter notebook if the module has changed locally:
%% Cell type:code id:grave-defense tags:
``` python
%load_ext autoreload
%autoreload 2
%% Cell type:markdown id:suitable-paint tags:
# Exercices
%% Cell type:markdown id:bright-original tags:
The aim here is to get comfortable in Jupyterlab.
## Exercise
- Start a Jupyterlab server.
- Create a new notebook with a python3 kernel.
- Create, delete and move cells around using shortcuts and graphical interface.
NB: A kernel provides a programming language support in Jupyter. Kernels are available for Python, R, Julia, and many more.
%% Cell type:code id:beneficial-ownership tags:
``` python
%% Cell type:markdown id:frozen-mediterranean tags:
## 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 !")
%% Cell type:code id:adjustable-scheme tags:
``` python
%% Cell type:markdown id:owned-rings tags:
## Exercise
In the notebook, create a markdown cell with:
- A Header
- Bold text
- A list
- A link to the jupyter documentation ie
Render (execute) the cell to display the cell with a pretty formatting.
%% Cell type:code id:assumed-platinum tags:
``` python
%% Cell type:markdown id:reported-planning tags:
## 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. This is potentially dangerous when using notebooks and has to be kept in mind when coded and used.
%% Cell type:code id:residential-furniture tags:
``` python
%% Cell type:markdown id:surprising-interaction tags:
## Exercise
Using a Jupyter magic command, create a cell listing the files in the current directory using a bash subprocess.
%% Cell type:code id:informed-parish tags:
``` python
%% Cell type:markdown id:removed-devon tags:
## Exercise
Using the graphical interface, export your notebook as html file.
%% Cell type:code id:absolute-indication tags:
``` python
%% Cell type:markdown id:thermal-karma tags:
# More documentation
## A note about Extensions
JupyterLab 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 JupyterLab: nbconda
You can discover and install much more extensions using the Extension Manager in the JupyterLab interface.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment