Chapter 3: Jupyter Notebooks
The Jupyter Notebook
Easy speaking, a Jupyter Notebook is “Python in a web-browser”. This is a very simplified description (read more about it on jupyter.org ꜛ and wikipedia ꜛ), but for our course this is fairly enough.
To work with Jupyter Notebooks, we first have to start a new Jupyter (local) server. With Anaconda, this is easily done by clicking on the Launch Jupyter button:
After launching a new server, immediately a web-browser window will show up:
Creating a new notebook
By default, the new browser window shows the files and folders in your home folder. Please navigate to your desired destination for our course files, and create a new folder for it. Once done, please create a new Python notebook:
The newly created blank notebook should look similar to this:
Code cells
A new notebook contains already a first code cell. Within this cell, you can write any Python code that you would use in a pure Python script, e.g., our first lines of code used in the Anaconda/Spyder chapter. Just enter your code and hit the “Run” button in the top menu-bar, and the code of your currently selected cell (indicated by the blue vertical bar on the left of the cell) will be executed. Its output will then be show right below the cell.
Jupyter code cells are comparable to the code cells also introduced in the Anaconda/Spyder chapter. To add a new cell, just insert it via the Insert menu:
Markdown cells
Besides code cells, there is another type of cell, called Markdown cells. To add a Markdown cell, let’s convert our previously added new code cell into a Markdown cell by using the drop-down menu in the top menu bar:
Markdown cells contain just text and are not executed as Python commands. This text serves to describe your code. But unlike simple comments, you can add formatting to your text by using Markdown syntax:
Markdown: You can find an overview of the Markdown syntax in the Markdown guide in the General Teaching Materials.
After you are done with the syntax, just hit the “Run” button and your text gets formatted:
To edit the text and syntax, just double-click on the corresponding cell.
Converting a Jupyter notebook into a Python script
A Jupyter notebook (“.ipynb”) is not a simple text-file anymore, that can be easily read by a text editor as pure Python files (“.py”), as it contains additional (HTML) commands for running the Jupyter notebook. But if necessary, you can export any Jupyter notebook as a Python file via “Download as” sub-menu in the “Files” menu:
Clear all cell outputs
Sometimes it is necessary to clean-up the output of all your so-far executed cells. You can do this via the menu “Cell” -> “All Output” -> “Clear”:
Useful keyboard shortcuts
Creating new cells
Key/Shortcut | Description |
---|---|
B | creates a new Python code cell |
X | deletes activated cell |
M | converts a Python code cell into a Markdown cell |
Executing cells
Key/Shortcut | Description |
---|---|
Ctrl/CMD+Return | executes currently activated cell (both, Python code and Markdown cells) |
Cursor Arrows UP/DOWN | navigate cells up and down |
How to use the provided course notebooks
I will provide all course notebooks to you. Just download the GitHub repository linked on the course description page (if not done yet). The notebooks contain from time to time exercises, which you will do by yourself during the course or as a homework. Always attached to the exercise, you can find the corresponding solution. These solutions should be shown folded by default and you can unfold them by clicking on “Toggle solution”. Please, unfold these solution not before we have discussed the corresponding exercise in the plenum.
After we have discussed a solution, you could, e.g., copy the unfolded solution and paste it into the “Your solution here” cell, if your own solution is not working correctly.
Info: Viewing the Jupyter notebooks on GitHub will not show the “Toggle solution” button and, hence, the solution can not be seen. On your own local Jupyter server and in Google Colab as well as in Binder, the “Toggle solution” button is displayed correctly and the solutions can be toggled.
Jupyter notebook extensions expand the functionality of your Jupyter environment with many useful additional functions. To enable them, follow the instructions in this blog ost, which also shows you, how to enable a Variable Explorer.
Install packages from within a Jupyter notebook: You can also install additional packages from within a Jupyter notebook. Just execute the following commands in a code cell once,
import sys
!{sys.executable} -m pip install <package>
and delete or comment them after their execution.
Jupyter vs. and (!) pure Python script
So, we have pure Python scripts using Spyder, and we have Jupyter notebooks, to do the same tasks just in a browser. Which solution should we use now? Which one is the better one?
I think that it’s not a question of choosing between both, but to use both complementary. E.g., the entire course will be held via Jupyter notebooks. You can download the notebooks and write your own comments into it as well as your own solutions. From time to time I will ask you to put your solutions into a pure Python script in order to become more familiar with it. But in general, you can just work with Jupyter notebooks throughout this course and beyond, when you start to solve your own problems.
Nevertheless, you can do all the same also with pure Python scripts. May be not now, but later you will reach a point, where you might think that a pure Python script is more flexible regarding editing, executing and exchanging. Especially when you design huge processing pipelines, I recommend to use pure Python scripts rather than Jupyter notebooks. But this is fully up to you. There is also no restriction to use both at the same time, e.g., write more complex code in pure Python scripts, but run and control your pipeline via a Jupyter notebook.
Feel free to find your own coding workflow and use what suits you best and you feel comfortable with.