While working through a Python data analytics course, I started to like Jupyter Notebooks a lot. Not only do we get much faster feedback when we plot our data, this format offers an effortless way to keep your documentation next to your code. Let us explore this fascinating runtime environment for our code.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Jupyter Notebook or JupyterLab?
If you want to run Jupyter notebooks on your local machine, you hit an obstacle right away. Should you install JupyterLab or Jupyter Notebook? And what is even the difference?
Jupyter Notebook is the old basic web application that made the work in shareable notebooks so popular, while JupyterLab is the successor that offers more of an IDE feeling.
The good news for us is that the format for the notebooks is the same and that we can use whatever tool we want. However, I strongly suggest that you use JupyterLab if you start today.
If you are still unsure, you can go to https://jupyter.org/try-jupyter/lab/ and try it online – no installation or credit card required.
Installation
We can install JupyterLab with this command:
1 |
pip install jupyterlab |
Start JupyterLab
When the installation was successful, we can start JupyterLab with this command:
1 |
jupyter-lab |
This should open your browser and redirect you to a page like this one:
If this does not happen, go back to the terminal, and check the output. There should be an entry like this:
To access the server, open this file in a browser:
file:///C:/Users/**/AppData/Roaming/jupyter/runtime/jpserver-21424-open.html
Or copy and paste one of these URLs:
http://localhost:8888/lab?token=913d740*****
or http://127.0.0.1:8888/lab?token=913d740*****
Try one of the URLs to open JupyterLab. If one does not work, try the other one.
How does it work?
We can create a new Notebook on the Launcher
or with the menu entry File / New / Notebook
:
The great benefit of Jupiter Notebooks is that they offer a seamless integration of code, data, and documentation. To achieve this, Jupiter Notebooks offer these 3 cell formats:
- Code: Where we write our source code in one of the over 40 supported programming languages.
- Markdown: For documentation written in GitHub-flavoured markdown
- Raw: For special formats like LaTeX that are not rendered in the web interface but could be used as part of a later step in our workflow.
For our first steps we can focus on the Code and the Markup cells. We can enter our Python code and then hit Shift+Enter
to run the current cell. Jupiter will show us the output directly under our code cell:
We can add new cells above (1) or below (2) the current cell. If we do not like the order, we can move the current cell up (3) or down (4) with the arrow buttons:
For the Markdown cells, we need to change the type of the cell to Markdown:
If we hit Shift+Enter
in a Markdown cell, it replaces the raw Markdown with the rendered HTML:
To go back into the edit mode, we can double-click the rendered text of the Markdown cell. The other cells only need a single click to enter the edit mode.
Run the old Jupiter Notebook
If you still want to use the old Jupiter Notebook web application, you can use this command to launch it:
1 |
jupyter notebook |
Next
With the basics of JupyterLab and Jupiter Notebook covered, we can make our first steps and explore this way of writing code. Next week we take a deeper look at how we can organise the code and the data in our notebooks.
6 thoughts on “Python Friday #160: Jupyter Notebooks for Your Data Analysis”