Jupyter Notebook
Jupyter notebooks
What is Jupyter Notebook?
-
A web-based interactive development environment
Jupyter on Windows
If you use Anaconda it already comes with Jupyter notebook. On Windows you can run it from the start menu.
Alternatively:
Set up a virtual environment and install the following package:
pip install jupyter
Jupyter on Linux and OSX
Install
For Linux and OSX I recommend using virtualenv and installing with pip.
virtualenv -p python3 ~/venv3
source ~/venv3/bin/activate
pip install jupyter
Run
cd src/examples/jupyter/
jupyter notebook
- Your browser should open. If not, there is a link in the terminal.
Jupyter New notebook
-
Create a new notebook (New / Python 3)
-
It is called "Untitled" - Rename it
-
Type in some code
x = 2 -
Execute code either by pressing the arrow that looks like a triangle or by pressing
Ctrl-ENTER. -
Show content of variables. It is enough to enter the name of the variable in an edit box and "run that code".
-
import modules
-
Quit - shut down the notebook server.
-
Number is the execution order Out[] refers to the number of the cell for this this the output.
Jupyter Notebook file format
- Jupyter notebooks are JSON file with a
.ipynbextension. Open one such file with a plain text editor. - We can download file as Python (.py) exporting the code as a stand-alone python program.
Jupyter notebook edit and command mode
-
Modes: Blue - command mode, Green - edit mode
-
Enter - Switch to edit mode (or newline if already in edit mode)
-
ESC - Switch to command mode
-
Ctrl-Enter - execute current cell
-
Shift-Enter - execute current cell
-
Clear Cell data before saving
-
Button h for help
-
Button A - add cell Above current cell
-
Button B - add cell Below current cell
-
dd - delete current cell
-
...
Jupyter notebook Intellisense (TAB completition)
-
Just press TAB for word completition and method suggestions
-
When we typed in the name of an object and press dot
.we get the list of available methods and attributes.
e.g.
import sys
sys.
or
name = "Foo"
name.
Jupyter add
- Open an existing notebook:
add.ipynb
Before commiting a Jupyter notebook in git, it is usually a good idea to remove all the generated content. So:
Edit / Clear Output of all Cells
Planets
-
The Planets example we saw in the Pandas chapter
-
planets.csv -
planets.ipynb
Jupyter input
input.ipynb
File chooser
- Avoid hard-coded pathes, use the FileChooser.
filechooser.ipynb
IPy Widgets
-
ipywidgets
-
ipywidgets.ipynb
Jupyter Notebook and Markdown
markdown.ipynb- Jupyter Markdown
- Original Markdown
- Markdown
Latex in Jupyter Notebook
- To insert complex formulas.
latex.ipynb- Latex symbols
Jupyter StackOverflow
-
Download a dataset from the Stack Overflow survey.
-
unzip the file. Feel free to remove the
__MACOSX/directory if it is included. -
SO/so.ipynb -
SO/selected-rows.ipynb -
SO/selected-columns.ipynb
Use SciPy image
scipy_image.ipynb
Create Random image
create_random_image.ipynb
Load image using OpenCV
-
load_image_using_opencv
Genes using Jupyter
jupyter notebook genes.ipynb
More Jupyter examples
generate.ipynbGenerate a single Pandas DataFramesalary_generate.ipynbsalary.ipynbtemperatures.ipynbweather.ipynbnumpy_matrix.ipynbseaborn_tips.ipynb
Jupyter Notebook: run on other port
jupyter notebook --port 8080
Jupyter Notebook: public IP
jupyter notebook --ip 192.168.1.10
Other Jupyter Notebook Kernels
Jupyter Notebook convert to other format (nbconvert)
- nbconvert
jupyter nbconvert try.ipynb --to HTML
jupyter nbconvert try.ipynb --to PDF (needs also pandoc)
Jupyter Notebook extensions
pip install jupyter_contrib_nbextensions
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
- Scratchpad
- Variable Inspector
Jupyter notebook Checkpoints
- A very simple, one-level version control.
- I would not rely on it as it is now. Use a real VCS. (e.g. git)
Jupyter notebook autoreload
def hello():
print("hello")
examples/ml/reload.ipynb