• Webanwendung, zum Erstellen und für die gemeinsame Nutzung von Dokumenten, die sowohl Live-Code, Gleichungen, Visualisierungen, Links als auch formatierten Text enthalten
  • ideales Werkzeuge, um Programme, ihre Ergebnisse und ihre Beschreibung und Dokumentation zu vereinen und die Datenanalyse in Echtzeit durchzuführen

"Jupyter" - Acronym für die umgesetzten Haupt-Programmiersprachen Julia, python und R; unterstützt heute viele andere Sprachen

Im Browser-Fenster:

  • Startseite der Anwendung mit Liste der Notebooks jupyter notebook Startseite
  • Folge (mehrzeiliger) Text-Eingabe-Felder für
    • Überschriften
    • Code
    • Kommentare
    • Programm-Ausgaben

sowie Steuerelementen für die Ausführung von Code

Start der Anwendung erfolgt im Terminal-Fenster (im Ordner mit Notebooks):

jupyter notebook

bzw. Start eines bestimmten Notebooks:

jupyter notebook notebookname.ipynb

öffnet Browser-Fenster mit der Adresse: http://localhost:8888

Beim Anlegen eines neuen Dokuments: neues Dokument Auswahl von Python 3 (= jupyter notebook), Text File, Folder oder Terminal

Arbeiten mit dem Jupyter Notebook

Bei Start eines neuen Notebooks: neues Notebook

Menü, Werkzeuge und leere Code-Zelle

In [1]:
%matplotlib inline
%pylab inline
Populating the interactive namespace from numpy and matplotlib

Neben Code-Zellen gibt es die Möglichkeit, Zellen vom Typ Markdown für die Dokumentation zu verwenden.

Markdown ist eine Auszeichnungssprache (wie html) für einfache Textgestaltung mit

  • ###### Überschriften
  • Textformatierung
    • kursiv
    • fett
    • kursiv und fett
  • Listen
    1. Geordnet
    2. Ungeordnet
  • Blockzitate
  • Interne und externe Links
  • Blockzitate
  • Code-Abschnitte
    str="Das ist eine Codezeile."
    print(str)
    
  • Tabellen
  • Bilder
  • mathematische Symbole $(\LaTeX)$ und ∃ UTF-8 Mathematische Operatoren
  • Zeilenumbrüche und horizontale Linien

Magic functions

In [2]:
%lsmagic
Out[2]:
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
In [3]:
%ls
images/  jupyter-notebook.ipynb
In [4]:
%quickref

Betriebssystem-Befehle:

In [5]:
!pwd
/home/brose/work/jupyter/Jupyter Notebooks Intro

python-help:

In [6]:
?range

Zeitmessung (eine Code-Zeile):

In [7]:
%time x = range(100)
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 6.91 µs

Zeitmessung (mehrzeilig):

In [8]:
%%timeit x = range(100)
    mean(x)
14.9 µs ± 255 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [9]:
?%%timeit
In [10]:
%%HTML
<img src="https://www.python.org/static/img/python-logo.png">
<p>Jupyter kann HTML rendern!</p>

Jupyter kann HTML rendern!

$\LaTeX$ im Fließtext und in abgesetzten Formelzeilen $$\LaTeX$$ in Markdown sowie in Code Zellen:

In [11]:
%%latex
\begin{align}
\vec{M} = \vec{r}\times\vec{F} =
\begin{vmatrix}
\vec{e}_x & \vec{e}_y & \vec{e}_z \\
x & y & z \\
F_x & F_y & F_z
\end{vmatrix}
\end{align}
\begin{align} \vec{M} = \vec{r}\times\vec{F} = \begin{vmatrix} \vec{e}_x & \vec{e}_y & \vec{e}_z \\ x & y & z \\ F_x & F_y & F_z \end{vmatrix} \end{align}

Nutzer-interaktion mit widgets

In [12]:
from ipywidgets import interact, fixed

def f(x):
    print(x)
    
interact(f, x=(2, 20, 2));
In [13]:
interact(f, x=False);
In [14]:
interact(f, x="Text!");
In [15]:
def plotsin(omega):
    t = arange(0, 1, 0.01)
    plt.plot(t, sin(omega*t))
    plt.show()
    
interact(plotsin, omega=(0.1, 10*pi, 0.1));

Das Display System

Universelles Werkzeug zur Anzeige verschiedener Darstellungen von Objekten.

Der Aufruf von display für ein Objekt sendet alle möglichen Darstellungen des Objekts an das Notebook. Diese Darstellungen werden im Notebook-Dokument gespeichert. Im Allgemeinen verwendet das Notebook die reichhaltigste verfügbare Darstellung. Für konkrete Darstellungen (html, latex, jpeg, png, svg, json) gibt es entsprechende Funktionen.

Bilddarstellung aus

  • Rohdaten oder URL:
In [16]:
from IPython.display import display, Image
Image(url='http://python.org/images/python-logo.gif')
Out[16]:
  • lokalen Dateien:
In [17]:
img = Image(filename="images/python.png")
display(img)
  • Vektorgrafik (SVG):
In [18]:
from IPython.display import SVG
SVG(filename='images/jupyter-logo.svg')
Out[18]:
Group.svg Created using Figma 0.90

Audio:

In [19]:
import numpy as np
from IPython.display import Audio
f1, f2 = 500, 505
dauer, rate = 5, 6000
t = np.linspace(0, dauer, dauer*rate)
signal = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)

Audio(data=signal, rate=rate)
Out[19]:

Video:

In [20]:
from IPython.display import Video
Video("images/Kreisbahn.mp4")
Out[20]:
In [21]:
from IPython.display import YouTubeVideo
YouTubeVideo("ZN2vAQmy4nk")
Out[21]:

Links zu lokalen Dateien:

In [22]:
from IPython.display import FileLink, FileLinks
FileLink('jupyter-notebook.ipynb')
In [23]:
FileLinks(".")

Einbinden externer Webseiten:

In [24]:
from IPython.display import IFrame
IFrame('https://jupyter.org/', width='100%', height=400)
Out[24]:

$\mathbf{\LaTeX}$:

In [25]:
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
Out[25]:
$\displaystyle F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$
In [26]:
from IPython.display import Latex
Latex(r"""
\begin{align}
\epsilon_0 \nabla \cdot \vec{\mathbf{E}} & = \varrho \\
\nabla \times \vec{\mathbf{E}}\, + \, \dot{\vec{\mathbf{B}}} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 \\
\frac{1}{\mu_0}\nabla \times \vec{\mathbf{B}} -\, \epsilon_0 \dot{\vec{\mathbf{E}}} & = \vec{\mathbf{j}}
\end{align}
""") 
Out[26]:
\begin{align} \epsilon_0 \nabla \cdot \vec{\mathbf{E}} & = \varrho \\ \nabla \times \vec{\mathbf{E}}\, + \, \dot{\vec{\mathbf{B}}} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \\ \frac{1}{\mu_0}\nabla \times \vec{\mathbf{B}} -\, \epsilon_0 \dot{\vec{\mathbf{E}}} & = \vec{\mathbf{j}} \end{align}

Notebook Inhalte exportieren

jupyter nbconvert --to html jupyter-notebook.ipynb

jupyter nbconvert --to pdf jupyter-notebook.ipynb