Unverified Commit a460c148 authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

Merge branch 'master' of gitlab.pasteur.fr:hub-courses/scientific_python

parents 0472968f 59d5382e
%% Cell type:markdown id:expired-highway tags:
# Introduction to JupyterLab
## Aim of this section
- Whet your appetite on notebook technologies.
- Discover, get comfortable with Jupyterlab and create notebooks.
## Install
After creating a folder for the course, use `venv` to create a virtual environment named for example `sp_env`:
```python3 -m venv sp_env```
This will create a folder `sp_env` in your working directory. The corresponding virtual environment can be activated with:
```source sp_env/bin/activate```
You are now in a virtual environment. You can install librairies in it using pip and these will be installed specifically in this environment (and not globally on your machine). For more on virtual environment, [see the documentation](https://docs.python.org/3/library/venv.html).
Once the virtal environment activated, we can start composing this environment, now with jupyterlab
```pip install jupyterlab```
You can now start the jupyter server as follows:
```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 and you will be automatically redirected to this tab.
Once all you work is done, you can exit the virtual environment with:
```deactivate```
You will need to reactivate it (with `source sp_env/bin/activate`) in order to use it again.
For more exhaustive guidelines on JupyterLab installation, you can see [the official Jupyter documentation](https://jupyter.org/install)
## 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|
|-|-|
|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](https://www.markdownguide.org/cheat-sheet)
- **Edit mode** (`Enter/Return`): To edit the active cell. Then to execute the cell you have 2 options:
|keys|effect|
|-|-|
|`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](https://ipython.readthedocs.io/en/stable/interactive/magics.html)
Here are some example of useful magic commands:
- Run cell with bash in subprocess:
%% Cell type:code id:billion-actress tags:
``` python
%%bash
echo "This is a bash script"
for i in {1..3}; do echo $i; done
echo "Over and out"
```
%% Cell type:markdown id:tight-spring 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:helpful-oasis tags:
``` python
! echo "This is executed in a bash subprocess"
```
%% Cell type:markdown id:marked-construction tags:
- `%timeit` can be used to check for execution times:
%% Cell type:code id:photographic-premises tags:
``` python
%timeit for _ in range(1000): True
```
%% Cell type:markdown id:sustained-render 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:posted-pleasure tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:markdown id:insured-entertainment tags:
# Exercices
%% Cell type:markdown id:helpful-telephone 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:congressional-light tags:
``` python
```
%% Cell type:markdown id:little-questionnaire 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:behavioral-ethnic tags:
``` python
```
%% Cell type:markdown id:trained-advantage tags:
## Exercise
In the notebook, create a markdown cell with:
- A Header
- Bold text
- A list
- A link to the jupyter documentation ie https://jupyter.org/documentation
Render (execute) the cell to display the cell with a pretty formatting.
%% Cell type:code id:phantom-register tags:
``` python
```
%% Cell type:markdown id:precise-average 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:solar-auckland tags:
``` python
```
%% Cell type:markdown id:inside-approval 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:worse-husband tags:
``` python
```
%% Cell type:markdown id:dirty-speaker tags:
## Exercise
Using the graphical interface, export your notebook as html file.
%% Cell type:code id:injured-thirty tags:
``` python
```
%% Cell type:markdown id:perceived-michael tags:
# More documentation
JupyterLab: https://jupyterlab.readthedocs.io/en/latest/
## 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.
This diff is collapsed.
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