Jupyter Notebooks¶
Sometimes, there is a need to prototype something, and while the Python console can be used for this, it is usually pretty inconvenient. How about a solution making use of a web browser, allows for simple plotting, and comes with many features, including auto-completion and the possibility to sensibly annotate your code? Jupyter notebooks come to save you!
Jupyter notebooks are very useful to prototype, quickcheck or generally to have very fast response cycles. If you ever worked with Mathematica notebooks or something similar, you will feel familiar immediately. Furthermore, Jupyter notebooks are great for (interactive) teaching. After all, there is a reason why Jupyter notebooks became the de-facto standard in many (physical) sciences (at least for demonstrating and teaching).
Warning: Never (mis-)use Jupyter notebooks for programming¶
However, do not use Jupyter notebooks for serious programming, as they basically lack all relevant features modern editors/IDEs provide you with, such as refactoring, code style hints, et cetera. Furthermore, while Jupyter notebooks are great for rapid prototyping, they are simply not designed for programming. Taken together, using Jupyter notebooks out of convenience for programming tasks usually leads to bad overall code quality. In a scientific context, you eventually loose what science is actually all about: reproducibility. Remember: scripting (i.e., prototyping) is fundamentally different from programming. Most tasks we are actually interested in when doing science are by far too complex for scripting and require actual programming.
Note
The reason not to use Jupyter notebooks in this course in favour of an IDE (PyCharm) is simply that this course focuses on programming with a tendency towards software engineering rather than prototyping. Hence, using an IDE is pretty much a requirement.
Installing Jupyter¶
As usual with Python projects, Jupyter should be used with its own virtual environment.
pip install jupyter
Open Jupyter notebook¶
file browser
jupyter notebook
open specific file:
jupyter notebook myfile.ipynb
Interactive elements¶
Slider as decorator for a function. The function does not need to be called explicitly!
import ipywidgets as widgets
@interact(parameter=widgets.IntSlider(min=1, max=10, step=1, value=5))
def myfunction(parameter):
print(f'My parameter is {parameter}')