2.1. Jupyter

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.

2.1.1. Install

$ pip install jupyter

2.1.2. Run

$ jupyter-notebook
[I 08:58:24.417 NotebookApp] Serving notebooks from local directory: /Users/catherine
[I 08:58:24.417 NotebookApp] 0 active kernels
[I 08:58:24.417 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 08:58:24.417 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
$ jupyter-notebook filename.ipynb

2.1.3. Using

  • Add code

  • Run code

  • Modify code and run

  • Autocomplete

  • Cell type (Markdown, LaTeX, Code)

2.1.4. Shortcut keys

Indent:

  • Tab

  • Shift + Tab

Comment Code:

  • Ctrl + /

Run:

  • Shift + Enter

2.1.5. Cells

  • Insert Below/Above Cells

  • Add, Delete Cells

  • Cut, Copy, Paste Cells

  • Move Up/Down Cells

  • Merge, Split Cells

2.1.6. Run

  • Run Cell Shift-Enter

  • Run All (above/below)

  • Restart Kernel

  • Restart Kernel and Clear Output

  • Clear Output

2.1.7. Magic commands

2.1.9. Functions

  • Checkpoints

  • Download

  • Trust Notebook

  • Close and Halt

2.1.10. Performance and profiling

  • %%timeit

  • %%timeit -n 1000 -r 7

2.1.11. Markdown

Unorganized lists:

* first element
* second element
* third element
- first element
- second element
- third element

Organized lists:

1. first element
1. second element
1. third element

Headers:

# Header level 1
## Header level 2
### Header level 3
#### Header level 4
##### Header level 5
###### Header level 6

Formatting:

*italic*
**bold**

Code inline:

`class`

Code blocks:

```python
name = 'José Jiménez'
print(f'My name... {name}')
```

2.1.12. Tables

| id | firstname | lastname |    agency |
|----|:-----------|:---------:|----------:|
| 1  | José       |  Jiménez  |      NASA |
| 2  | Иван       |  Иванович | Roscosmos |
| 3  | Mark       |   Watney  |      NASA |
| 4  | Alex       |   Vogel   |      NASA |

2.1.13. LaTeX

  • %%latex

%%latex

$$c = \sqrt{a^2 + b^2}$$
%%latex

$$\int_{x=0}^{x=\infty} x^\pi dx$$
%%latex

\begin{equation}
H← ​​​60 ​+​ \frac{​​30(B-R)​​}{Vmax-Vmin}  ​​, if V​max​​ = G
\end{equation}
from IPython.display import Latex

Latex(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
Latex(r'$\lim_{x \to 0} (1+x)^{1/x} = e$')

2.1.14. Matplotlib charts

x = np.linspace(-5, 5, 100)  # vector z 100 równo odległymi wartościami od -5 do 5
y = np.sin(X)                # sinus wszystkich wartości x
plt.plot(x, y)               # wykres liniowy
%matplotlib inline
import math
import random
from matplotlib import pyplot as plt

x1 = [x*0.01 for x in range(0,628)]
y1 = [math.sin(x*0.01)+random.gauss(0, 0.1) for x in range(0,628)]
plt.plot(x1, y1)

x2 = [x*0.5 for x in range(0,round(63/5))]
y2 = [math.cos(x*0.5) for x in range(0,round(63/5))]
plt.plot(x2, y2, 'o-')

plt.show()

2.1.15. HTML

from IPython.display import HTML

HTML("We can <i>generate</i> <code>html</code> code <b>directly</b>!")

2.1.16. JavaScript

from IPython.display import Javascript

Javascript("alert('It is JavaScript!')")

2.1.17. Image

from IPython.display import Image

Image(url="https://python.astrotech.io/_static/favicon.png")

2.1.18. YouTube

from IPython.display import YouTubeVideo

YouTubeVideo("h8mDUc5L0XM")

2.1.19. Execute terminal commands

  • !

  • !pwd

  • !ls

dirs = !ls

for file in dirs:
    if file.find("1_") >= 0:
        print(file)

2.1.20. Output to different formats

  • File -> Download as

  • Notebook (.ipynb)

  • Python (.py)

  • HTML (.html)

  • Reveal.js Slides (.html)

  • Markdown (.md)

  • reST (.rst)

  • LaTeX (.lex)

  • PDF via LaTeX (.pdf)

2.1.21. Generate HTML

  1. File -> Save and Checkpoint

  2. File -> Download as -> HTML (.html)

2.1.22. Slides

  1. View -> Cell Toolbar -> Slideshow

  2. Select slides, sub-slides and speaker notes

  3. File -> Save and Checkpoint

  4. File -> Download as -> Reveal.js slides (.slides.html)

2.1.23. Github pages with Jupyter Slides

$ git submodule add https://github.com/hakimel/reveal.js.git reveal.js

$ jupyter nbconvert --to slides index.ipynb --reveal-prefix=reveal.js

$ jupyter nbconvert --to slides index.ipynb --reveal-prefix=reveal.js \
    --SlidesExporter.reveal_theme=serif \
    --SlidesExporter.reveal_scroll=True \
    --SlidesExporter.reveal_transition=none

2.1.24. Assignments

Code 2.65. Solution
"""
* Assignment: Jupyter First
* Complexity: easy
* Lines of code: 10 lines
* Time: 13 min

English:
    TODO: English Translation

Polish:
    1. Stwórz notebook jupyter o nazwie `jupyter_first.ipynb`
    2. dodaj tekst opisujący następne polecenia
    3. dodaj trzy różne 'code cell'
    4. uruchom code cell z wynikiem wszystkich powyżej
    5. dodaj code cell, który pokaże czas wykonywania instrukcji
    6. dodaj Code Cell, który wyświetli wykres funkcji `sin()` inplace
"""


Code 2.66. Solution
"""
* Assignment: Jupyter Slides
* Complexity: easy
* Lines of code: 1 lines
* Time: 5 min

English:
    TODO: English Translation

Polish:
    1. Poprzedni skrypt przekonwertuj na slajdy i uruchom prezentację w przeglądarce
"""