A Trip to Jupyter Lab

image_5608_2e-Jupiter.jpg This image of Jupiter was taken by NASA’s Juno Orbiter on December 16 2018 and then processed by citizen scientist David Marriott. Image credit: NASA / JPL-Caltech / SwRI / MSSS / David Marriott.

This is the first of 6 post where I share the content of the beginner’s course I held for Federico II University undergraduate students in Neaples (Italy).

The aim of this material is to offer an interactive environment where students can start their journey into this very large subject.

I think that Jupyter lab offers a really conveninent environment, also improving the learning journey with fast visual feedbacks

A Trip to Jupyter Lab

Install and launch jupyter lab

create and activate your environment

if you have a conda installation

conda create -n myenv python=3.12
conda activate myenv

on windows using the py launcher

py -3.12 -m venv myenv
myenv\Scripts\activate

launch installation

with conda

conda install jupyterlab

with pip

pip install jupyterlab

launch application

jupyter lab

create a notebook

when starting application your browser will show the launcher page which includes some icons; clicking the python icon on the top left corner will open a new notebook

Writing and executing code

Each Jupyter notebook is composed of cells; in each cell you can type some python code e.g.

name = "Marco"
f"my name is {name}"

executing a cell

you can either click on the right arrow icon in the top of the notebook or press shift-enter in a cell you are writing into

the last expression in a cell is returned as a result; if any function prints some text this appears as well

print("this is a text before the result of an expression")
# this is the last expression in the cell
2 ** 3 + 2

what does it happen behind the scenes?

a python interpreter is activated per each individual open notebook; each time a cell is executed the text inside the cell is evaluated and the result is returned as well as any output on the stdout or stderr streams

Magic commands

Jupyter extends python with some commands aimed to simplify common tasks; these commands starts with one or two percent symbols; execute the following cell to read an introduction to magic commands and their functionality. Let’s just list a few action you can do

  • change the current process directory
  • time the execution of a command
  • save the content of a cell in a file
  • execute a command in your operating system (an external command)
%magic

Executing external process

Magic commands starting with a ! are equivalent to commands sent to your operating system shell; their standard output and standard error streams contents are collected as the cell output e.g. ! git status will execute git in the current working directory (which is the one where the notebook is created) and the result is returned

Timing a cell

using the magic command %time followed by some python code will measure the execution time of the cell

%timeit will perform multiple executions of the code in order to gather a statistic for execution time

Using python Help

by writing a question mark before the last expression of a cell you can read the python built-in manual

?sum

Load and save files and notebook

A jupyter app is a web application and can run on a remote server; you can download your notebooks as well as other data files available and load other notebooks or files

saving your current notebook

Every now and then the notebook saves itself; you can make sure it is saved by clicking the floppy disk icon in the top bar

Each notebook can be renamed either

  1. from the left pane
    1. selecting the folder tab
    2. and right clicking on the name in the file list on the left
    3. and selecting rename; or
  2. from an open notebook
    1. right clicking on the name in the tab
    2. and selecting rename

upload files and notebook

to load notebooks or any file:

  1. choose the folder tab
  2. click on the up arrow on the left
  3. select your file from your local disk

download a file or a notebook

  1. choose the folder tab
  2. navigate to the directory containing the file(s)
  3. right click on the file name
  4. select download

Adding formatted text

Jupyter allows you to write formatted text using MarkDown.

This allows you to create titles italic text bold text links e.g. cheat sheet

You can create tables as well

species name birth
cat Matisse 2021
dog Nuvola 2010

also formulas can be embedded in text \int_R{e^{-x^2}dx} = \sqrt{\pi}

or as equations

Var = \frac{ \sum_{i \in 1..n}{(\mu - x_i)^2} }{n}

Var = E[(E[X] - X)^2]

Var = E[X]^2 + E[X^2] - 2E[XE[X]]

Var = E[X^2] - E[X]^2

\sigma = \sqrt{Var}

Exercise

  1. save and rename this notebook
  2. complete the following function with an iteration which prints all fibonacci numbers up to the n-th (see the fibonacci definition below)

n \in N

fib(0) = fib(1) = 1

n >= 2 \implies fib(n) = fib(n - 1) + fib(n - 2)

def fibonacci(n):
    # complete the function here

  1. execute the function and compare your results with your friends
fibonacci(5)

  1. download this notebook and exchange with a friend
  2. load your friend’s notebook and upload it here

marco.p.v.vezzoli

Self taught assembler programming at 11 on my C64 (1983). Never stopped since then -- always looking up for curious things in the software development, data science and AI. Linux and FOSS user since 1994. MSc in physics in 1996. Working in large semiconductor companies since 1997 (STM, Micron) developing analytics and full stack web infrastructures, microservices, ML solutions

You may also like...

Leave a Reply