Jupyter Notebook and lab
Jupyter Notebook
Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualisations and explanatory text.
You may find this useful especially for doing quick prototyping experiments and generating visualisations.
It is not necessary to use Jupyter Notebook for this course (Josiah hardly uses it himself!). So feel free to skip this if you do not find it useful.
Jupyter Notebook was formerly known as iPython Notebook (hence the file extension *.ipynb
)
To install Jupyter notebook, use pip install jupyter
.
Launching Jupyter Notebook
On your terminal, navigate to the directory in which you want to work. This will be Jupyter Notebook’s start-up directory.
To launch Jupyter Notebook, type jupyter notebook
into the terminal.
The application will open in the web browser at the following address: http://localhost:8888
. If this does not happen, then browse through the messages in your terminal to find something that looks like this:
Or copy and paste one of these URLs:
http://localhost:8888/?token=a1g45758f7347c6aa179d56128ae95ea1fdabd148b657d2d
or http://127.0.0.1:8888/?token=a1g45758f7347c6aa179d56128ae95ea1fdabd148b657d2d
And do what it says!
The web application should show you the files in the directory where you started Jupyter Notebook. Click on New
to create a new Jupyter notebook.
This will create a new Notebook in your current directory.
Once you created a notebook, you can rename it, save it, add and delete and move cells, run the cells, etc., from the menubar or toolbar. To save your notebook, you can also press Ctrl + S
.
Using Jupyter Notebook
There are 2 main items in a Jupyter Notebook:
- cells which are containers for text to be displayed in the notebook or code to be executed by the notebook’s kernel
- the kernel that executes the code contained in a notebook document
A Code
cell contains code to be executed in the kernel and displays its output below.
A Markdown
cell contains text formatted using Markdown and displays its output in-place when it is run. This is useful for typing text and explanations.
You can tell the difference between Code
and Markdown
cells because Code
cells have the label In
on the left whereas Markdown
cells do not.
You can set the cell type using the toolbar with Cell > Cell Type
.
The kernel’s state persists over time and between cells. If you import libraries or declare variables in one cell, they will be available in other cells. When you run a Code
cell, the label to its left changes from In [ ]
to In [1]
(or any number in the execution sequence).
That is as far as we will cover for Jupyter Notebooks. Use the Help
menu item in its toolbar for more advanced help. Just play around and experiment for yourself to see how it works and whether it suits your needs.
JupyterLab
JupyterLab is the next-generation iteractive development environment (IDE) for Jupyter. It offers a complete user interface where you can open multiple Jupyter Notebooks and switch between them in tabs, use text editors and run terminals in a single unified interface.
You can install JupyterLab with pip install jupyterlab
Then similar to running the classic Jupyter Notebooks, launch JupyterLab with jupyter lab
or jupyter-lab
.