A Trip to Jupyter Lab
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
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
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
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
- from the left pane
- selecting the folder tab
- and right clicking on the name in the file list on the left
- and selecting rename; or
- from an open notebook
- right clicking on the name in the tab
- and selecting rename
upload files and notebook
to load notebooks or any file:
- choose the folder tab
- click on the up arrow on the left
- select your file from your local disk
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
or as equations
Exercise
- save and rename this notebook
- complete the following function with an iteration which prints all fibonacci numbers up to the n-th (see the fibonacci definition below)
def fibonacci(n): # complete the function here
- execute the function and compare your results with your friends
fibonacci(5)
- download this notebook and exchange with a friend
- load your friend’s notebook and upload it here