The Jupyter Notebook
Easy speaking, a Jupyter Notebook is “Python in a web-browser”. This is of course a too simplified description, a Jupyter Notebook is and can do much more (read more about it on jupyter.org and wikipedia ). But for the moment, this is enough for our course.
To work with a Jupyter Notebook, 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
The browser shows the Jupyter file tree, which is nothing else than showing all the files and folders in you home folder. Please navigate to your desired destination for our course files, and create a new folder for it (if not done yet). Once done, please create a new Python notebook:
The newly created blank notebook should look similar to this:
The notebook comes already equipped with on 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:
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:
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 any text editor as pure Python files (“.py”), as it contains additional commands for running the Jupyter notebook. But if necessary, you can export any Jupyter notebook as Python file via “Download as” sub-menu in the “Files” menu:
Clear all cell outputs
Sometimes it is neccessary to clean-up the output of all your so-far execute cell. You can do this clean-up via the menu “Cell” -> “All Output” -> “Clear”:
Useful keyboard shortcuts
Creating new cells
|B||creates a new Python code cell|
|X||deletes activated cell|
|M||converts a Python code cell into a Makrdown cell|
|Ctrl/CMD+Return||executes currently activated cell (both, Python code and Markdown cells)|
|Cursor Arrows UP/DOWN||navigate cells up and down|
Using 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! To follow this guideline is fully up to you, I will neither control nor judge on this. It is simply in your own interest to learn something from the exercises by solving them by yourself.
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.
vs. and (!) pure Python script
So, now 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?
Frankly speaking, for the moment I’m not able to answer these questions. Instead, I rather 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 would 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.