From 5017ad660d1e37d8089fdd5659d110d2e984352a Mon Sep 17 00:00:00 2001 From: Etienne Kornobis <ekornobis@gmail.com> Date: Fri, 24 Sep 2021 10:53:35 +0200 Subject: [PATCH] Adding jupyterlab handson --- notebooks/jupyter_practice.ipynb | 378 +++++++++++++++++++++++++++++++ 1 file changed, 378 insertions(+) create mode 100644 notebooks/jupyter_practice.ipynb diff --git a/notebooks/jupyter_practice.ipynb b/notebooks/jupyter_practice.ipynb new file mode 100644 index 0000000..66c78e6 --- /dev/null +++ b/notebooks/jupyter_practice.ipynb @@ -0,0 +1,378 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "systematic-springer", + "metadata": {}, + "source": [ + "# Introduction to JupyterLab\n", + "\n", + "## Aim of this section\n", + "\n", + "- Whet your appetite on notebook technologies.\n", + "- Discover, get comfortable with Jupyterlab and create notebooks.\n", + "\n", + "## Install\n", + "\n", + "For more exhaustive guidelines, you can see [the official Jupyter documentation](https://jupyter.org/install)\n", + "\n", + "Using conda, create a ``jupyter`` environment with jupyter lab::\n", + "\n", + " conda create -n jupyter jupyterlab\n", + "\n", + "Once installed, you can then activate the ``jupyter`` environment and start the jupyter server as follows::\n", + "\n", + " conda activate jupyter\n", + " jupyter lab\n", + "\n", + "And open the specified URL in your internet browser (Chrome or Firefox are\n", + "better supported). By default, the address will be http://localhost:8888\n", + "\n", + "## Basic functioning of Jupyter notebooks\n", + "\n", + "### Using the interface\n", + "\n", + "Jupyter notebooks are organized in **cells** which are executed\n", + "separately. Therefore the code execution is not necessarily sequential as\n", + "in classical scripts. Cell execution order can be witnessed in the value in\n", + "between squared brackets `[]` on the left of the corresponding cell. **You\n", + "should be careful with cell execution order in jupyter**.\n", + "\n", + "Jupyter cells can be of various **types**:\n", + "\n", + " - **Code**: actual code blocks of your notebooks (which will be interpreted).\n", + " - **Markdown**: To integrate explanations within your notebooks.\n", + " - **Raw**: Raw it is...\n", + " - And more...\n", + "\n", + "Jupyter use **2 editing modes**:\n", + "\n", + "- **Command mode** (``Esc``): To organize cells and browse the notebook.\n", + "\n", + " Using keystrokes, you can:\n", + "\n", + "| key | effect |\n", + "|-|-|\n", + "| ↑, ↓ | Move up and down in cells |\n", + "| a | Add cell above |\n", + "| b | Add cell below |\n", + "| dd | Delete cell |\n", + "\n", + " You can as well use drag and drop with your mouse to move cells or groups of cells around.\n", + " Cell type can be changed in command mode using the graphical interface or shortcuts:\n", + "\n", + "|key|Switch cell to this mode|\n", + "|-|-|\n", + "|y|Code|\n", + "|m|Markdown|\n", + "|r|Raw|\n", + "\n", + "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)\n", + "\n", + "\n", + "- **Edit mode** (`Enter/Return`): To edit the active cell. Then to execute the cell you have 2 options:\n", + "\n", + "|keys|effect|\n", + "|-|-|\n", + "|`Ctrl + Enter`|Execute current cell|\n", + "|`Shift + Enter`|Execute current cell and move to the next cell|\n", + "\n", + "\n", + "### Jupyter magic commands\n", + "\n", + "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)\n", + "\n", + "Here are some example of useful magic commands:\n", + "\n", + "- Run cell with bash in subprocess:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "wanted-ethiopia", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a bash script\n", + "1\n", + "2\n", + "3\n", + "Over and out\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "echo \"This is a bash script\"\n", + "for i in {1..3}; do echo $i; done\n", + "echo \"Over and out\"" + ] + }, + { + "cell_type": "markdown", + "id": "verbal-intake", + "metadata": {}, + "source": [ + "- The exclamation mark character ``!`` can be used as well to execute the following line in a bash subprocess. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "naval-intensity", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is executed in a bash subprocess\n" + ] + } + ], + "source": [ + "! echo \"This is executed in a bash subprocess\"" + ] + }, + { + "cell_type": "markdown", + "id": "dynamic-heaven", + "metadata": {}, + "source": [ + "- `%timeit` can be used to check for execution times:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "above-recognition", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14.1 µs ± 436 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" + ] + } + ], + "source": [ + "%timeit for _ in range(1000): True" + ] + }, + { + "cell_type": "markdown", + "id": "simple-breathing", + "metadata": {}, + "source": [ + "- 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", + "execution_count": 22, + "id": "grave-defense", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n" + ] + } + ], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "markdown", + "id": "suitable-paint", + "metadata": {}, + "source": [ + "# Exercices" + ] + }, + { + "cell_type": "markdown", + "id": "bright-original", + "metadata": {}, + "source": [ + "The aim here is to get comfortable in Jupyterlab.\n", + "\n", + "## Exercise\n", + "\n", + "- Start a Jupyterlab server.\n", + "- Create a new notebook with a python3 kernel.\n", + "- Create, delete and move cells around using shortcuts and graphical interface.\n", + "\n", + "NB: A kernel provides a programming language support in Jupyter. Kernels are available for Python, R, Julia, and many more." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "beneficial-ownership", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "frozen-mediterranean", + "metadata": {}, + "source": [ + "## Exercise\n", + "\n", + "In the notebook, create a code cell with simple python code inside with a\n", + "``print`` statement, execute the cell and witness its output.\n", + "\n", + "For example::\n", + "\n", + " print(\"Hello World !\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "adjustable-scheme", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "owned-rings", + "metadata": {}, + "source": [ + "## Exercise\n", + "\n", + "In the notebook, create a markdown cell with:\n", + "\n", + "- A Header\n", + "- Bold text\n", + "- A list\n", + "- A link to the jupyter documentation ie https://jupyter.org/documentation\n", + "\n", + "Render (execute) the cell to display the cell with a pretty formatting." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "assumed-platinum", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "reported-planning", + "metadata": {}, + "source": [ + "## Exercise\n", + "\n", + "Grasp the concept of cell execution by creating three cells:\n", + "\n", + "- 1 cell defining a variable with a simple value. (e.g. `myvar=12`)\n", + "- 1 cell defining the same variable with a different value from the previous cell (e.g. `myvar=42`)\n", + "- 1 cell printing the value of the variable (`print(myvar)`).\n", + "\n", + "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", + "execution_count": null, + "id": "residential-furniture", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "surprising-interaction", + "metadata": {}, + "source": [ + "## Exercise\n", + "\n", + "Using a Jupyter magic command, create a cell listing the files in the current directory using a bash subprocess." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "informed-parish", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "removed-devon", + "metadata": {}, + "source": [ + "## Exercise\n", + "\n", + "Using the graphical interface, export your notebook as html file." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "absolute-indication", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "thermal-karma", + "metadata": {}, + "source": [ + "# More documentation\n", + "\n", + "JupyterLab: https://jupyterlab.readthedocs.io/en/latest/\n", + "\n", + "## A note about Extensions\n", + "\n", + "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:\n", + "\n", + "- Visualize and fold your code according to the table of content of your notebook: toc\n", + "- Import/Export your notebook as simple script/markdown files: jupytext.\n", + "- Deal with your conda environments in JupyterLab: nbconda\n", + "\n", + "You can discover and install much more extensions using the Extension Manager in the JupyterLab interface." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:dev]", + "language": "python", + "name": "conda-env-dev-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- GitLab