Beyond the notebook
Our file system
Our notebooks in jupyter do not exist in the void. They are stored in a directory on an online account on the Noteable server. These directories have a structure, the file system, and we can navigate this structure using dedicated commands.
These are not python commands, but are in fact specific to the file system itself, and follow different logic and conventions. For this reason, we prepend them with the ! character. At the moment, we will use one command per cell.
Pathway to the current directory
First, let’s check where we are right now in the file system. To do so, we query the pathway to the working directory with the pwd command.
! pwdThis shows where we currently are, and each of you should have a different path, with a different username.
Listing the content of the current directory
The ls command lists the files and directories in the current directory. The current directory is represented by a . (dot).
! ls Creating a new directory
The mkdir command creates a new directory in the specified path. For example, to create a new directory in the current directory we can write
! mkdir new_directory We can check its existence with ls.
! lsEqually, we can go back to the jupyter interface and see the new directory in the graphical user interface.
Changing directory
It is easy to change the current directory with the cd command. For example, to go to the new_directory with
! cd new_directoryAnd we we can go back to the previous directory with the cd command again using ..
! cd ..Creating an empty file
The touch command creates a new file in the specified path. For example, to create a new file in the current directory we can write
! touch new_directory/new_file.txtWe can check its existence with ls.
! ls new_directoryWe can open this file in the graphical user interface and edit the contents
[follows demonstration in Noteable with dummy text in the file new_file.txt]
Viewing file content
The cat command prints the content of a file. For example, to view the content of new_file.txt we can write
! cat new_directory/new_file.txt Several other commands exist to display parts of files, such as head, tail etc. Try them if you like!
Exercise
- Create a new jupyter notebook using the graphical user interface.
- From the notebook, use the commands above to create:
- a new directory called
src - a new file inside the directory called
naive_script.py - open it with double click for the files tab in
jupyterand write a minimal code
print("Hello, World!")- save the file
- use the
catcommand from the notebook to view the content ofnaive_script.py
- a new directory called
Running scripts: from the notebook
For the entire duration of this course, you have been running python codes by typing them inside the notebook cells and executing the code cells.
The notebooks are complex files, that contain a lot of information beyond your code: the markdown, images, and a lot of extra data (called metadata).
A much more essential way to store python code is to use scripts. These are files with the .py extension, like your naive_script.py file. Inside a script you can only have
pythoncodepythoncomments to the code (i.e. lines prepended by the#)
This means that the scripts are simple, portable pure text files containing instructions in the python language.
Can we execute such instructions? Yes, we run a script in various way. A simple way is to do it directly from a code cell in the notebook. For this we use a special jupyter command called run followed by the path to the script.
[check that you are in the correct path and the script exist]
run src/naive_script.pyAs you can see, we have executed the instructions of the script and its textual output has been printed in the notebook. No variables are created during the process.
The Terminal
All the commands that we have seen above can be used in a pure-text environment called the terminal. The terminal is a text-based interface to an operating system (local or remote).
You do not use the mouse to perform actions. Instead, you write commands. These commands belong to the scripting language called bash (see here for more information). It is different from python, they can interact together in the notebook.
You can launch a terminal directly from Noteable by clicking on the New button, then selecting Terminal.

Exercise
- We try to navigate folders from the terminal and create a file
- Open the terminal and
- change the directory to
src(usingcd) - create a new file inside the directory called
parabola.py(usetouch) - go back to the
Noteablefile tab, refresh the web-page and open theparabola.pyfile - open it and write a minimal code to plot a parabola. For example:
import numpy as np import matplotlib.pyplot as plt a = 1. b =1. c = 2 xlo = 0 xhi = 10 npoints = 100 x = np.linspace(xlo, xhi, npoints) y = a*x**2 + b*x + c plt.plot(x,y) plt.savefig("parabola.png")- save the file and go back to the Terminal tab
- use the
catcommand to check the content
- change the directory to
Exercise: Running scripts from the Terminal
We have seen that we can run the scripts from the notebook using the run command. In a very similar way, we can run scripts from the terminal, just by using the python command followed by the path to the script.
Do the following:
- first run the
parabolascript from the notebook usingrun: you should see the plot appear in the notebook. - then go back to the Terminal and run the
parabola.pyscript using thepythoncommand followed by the path to the script: e.g.python src/parabola.py. Where is the output?
run src/parabola.pyReusing code
The most useful feature of the scripts is that they can store code for future usage in an organized manner. We do not need to have all of our code in a single notebook: we can split the relvant bits, put them into scripts and use them again and again in different projects.
What allows us to do this is the special python keywork import, that we have already used many times.
Let’s move our working directory to src and try importing our parabola.py script
cd srcIn python we can only import .py files, so we drop the .py extension when importing.
import parabolaNow the variables defined in parabola.py are available in our current notebook.
print(parabola.xhi)Congratulations! You have written your first python module!
Exercise
The module above is not very useful. A better way to reuse code is to encapsulate it in a **custom .
Modify your
parabola.pyscript to create a functionplot_parabolathat takesa,b,c,xlo,xhias parameters.Then, import your parabola module in the present notebook using the following line:
import parabola as pbCan you find a way to access your function
plot_parabolafrompb?Improve your function to customize the appearence of the plot (adding custom labels, colors, linestyles) and use the function with such changes. Important you will need to restart the notebook for these to take effect, because a module is loaded only once.
More exercises on the terminal
- Most commands in
bashallow you to acces their documentation with<name_of_the_command> --help. Can you find out what the commandmvdoes?
##YOUR CODE HERE- Find a way to rename your
parabola.pyscript togeometry.py
##YOUR CODE HEREMost commands in bash support options, using the dash - sytax. For example, ls -l will list the files in a long format. Can you use ls --help to find out what the -l option does
ls --help- Can you find an additional option for
ls -lto fils files in reverse time order?
##YOUR CODE HERERiddles
To revise the material from the previous lectures, we can try to solve some riddles.
You can find the exercises here below