Learn about Jupyter Notebook, part of the Jupyter Lab interactive IDE that is ideal for Data Science. We’ll explore its advantages over a regular IDE, show you how to install Jupyter Notebook and Jupyter Lab and demonstrate its abilities.
Jupyter Lab vs Jupyter NotebookJupyterLab is a web-based, interactive development environment. It’s most well known for offering a so-called notebook called Jupyter Notebook, but you can also use it to create and edit other files, like code, text files, and markdown files. In addition, it allows you to open a Python terminal, as most IDEs do, to experiment and tinker.
JupyterLab is flexible: You can configure and arrange the user interface to support a wide range of data science, scientific computing, and machine learning workflows. It’s also extensible: everyone can write plugins that add new components and integrate with existing ones.
JupyterLab supports over 40 programming languages like Java, Scala, Julia, and R. However, I’ll focus solely on Python in this article.
On the other hand, Jupyter Notebook is a REPL-like environment that fuses code, data, and documentation. So in short, JupyterLab is the browser-based IDE, while Jupyter Notebook is the Notebook component inside it. You can install Jupyter Notebook separately if you want, and I’ll show you how in this article.
Advantages of using Jupyter Lab and NotebookPython application developers often prefer and work with a regular Python IDE like VSCode, which facilitates debugging, unit testing, deployment, and version management. In contrast, (data) scientists and data analysts have a different focus and often prefer a Notebook-style IDE. Let’s look at some of the advantages these notebooks have to offer.
Combine and document your code and resultsThe biggest advantage of using notebooks is that it allows you to combine three things into one environment:
All the while offering an interactive environment where you or your fellows can tinker and experiment. The following image perfectly demonstrates this:
A Jupyter notebook combining documentation, output, and code Interactive explorationA Notebook allows you to explore your data interactively. While doing so, the Notebook is a log of what you did and how you did it. It’s ideal to use in combination with NumPy and Pandas. So, a Notebook is more geared toward those working with and analyzing data than regular IDEs. However, some IDEs, like VSCode, will offer a built-in Jupyter Notebook (see further down this article).
It’s what the others useSometimes, it helps to go with the flow. In this case, you’ll notice that many others in data science use Jupyter Notebooks. This has a couple of advantages on its own:
Like all Python software, installing Jupyter Lab is a breeze. I’ll demonstrate how to install with both Pip and Conda. One thing to note beforehand: I recommend installing this system-wide. I usually stress the importance of using a virtual environment. However, I feel that tools like this you want to install once.
Install Jupyter Lab and/or Notebook with PipAs noted above, you want to install Jupyter Lab system-wide or at least in your local bin folder. Before I continue, I’d like to point you to pipx, which is ideal for installing tools like this.
Installing the package globally with the regular Pip install tool requires the -g
option. On many Linux systems, however, Pip will install the package in a .local/bin
folder if you don’t specify -g and if you’re not using a virtual environment. You can force this behavior by using the --user
option. The advantage is that you don’t need root access to the system you’re working on.
So, the most recommended way to install Jupyter Lab with the pip install command is:
$ pip install --user jupyterlab
If you only want Jupyter Notebook, use this instead:
$ pip install --user notebookInstall JupyterLab with Conda
JupyterLab can be installed with mamba
and conda
as well:
$ mamba install -c conda-forge jupyterlab or... $ conda install -c conda-forge jupyterlab
Like with Pip, if you only want o install Jupyter Notebook, use this:
$ mamba install -c conda-forge notebook or... $ conda install -c conda-forge notebookStart and explore JupyterLab How to open Jupyter Lab from the command line
If you followed the installation instructions, you should be able to start Jupyter Lab with:
$ jupyter-lab (a bunch of log message will follow)How to open Jupyter Notebook from the command line
If you only want to start a Notebook, use this:
$ jupyter-notebook (a bunch of log message will follow)Adding to your PATH
If you get an error like “command not found,” don’t panic. Unfortunately, some systems don’t have the .local/bin
directory in the $PATH
variable.
On Windows, you need to look carefully at the output of your installer. In my case, pip warned me that the binaries were not on the PATH. It also hinted to me to add this atrocious path to the PATH:
C:\Users\username\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts
It will almost certainly be a different path for you. And if you change Python versions, I’m afraid this path will also change. If you don’t know how to add something to your Windows PATH, please use Google. There are several good guides out there. Alternatively, I recommend installing this in a virtual environment.
Linux and MacOn Linux and OS X, a quick fix is to add it temporarily like this:
PATH="$HOME/.local/bin:$PATH"
However, this will only work as long as you keep the terminal window open. So, to make this permanent, I recommend adding the following snippet to the end of your ~/.profile
file:
# set PATH so it includes user's private bin if it exists if [ -d "$HOME/.local/bin" ] ; then PATH="$HOME/.local/bin:$PATH" fi
All it does is add the .local/bin folder to the PATH, but only if it exists.
The .profile
file is loaded once when you log in to your system. You probably need to log out and log in to apply the changes. In case you use zsh instead of Bash (zsh is the default on MacOS these days), you may want to try adding the snippet about to a file called .zprofile
instead.
If you start Jupyter Lab or Notebook, it will automatically open your browser. If it didn’t, you can go to http://localhost:8888/ manually to open it. Once there, create a new Notebook, e.g., by clicking File -> New -> Notebook.
I created a new Notebook named ‘First steps’ in the following screenshot. There are a couple of noteworthy things to see:
In
and Out
arrays. E.g., Out[3]
in the example below gives us the output of the expression in the In[3]
cellA dropdown at the top of your notebook allows you to change the cell type. All you need to care about, for now, are the types ‘Python’ and ‘Markdown.’
PythonA Python cell contains Python code. This can be any valid code, so all these are allowed:
Markdown is a lightweight markup language for creating formatted text. You can use it to create headers, lists, code snippets, and other things you can think of. For example, you might be familiar with it if you ever created a README.md file on GitHub.
Markdown is great for adding documentation to your notebook. For most, using a Markdown cheat sheet is enough to start quickly.
FormulasYou can enter nice-looking formulas using Markdown as well. Internally, Jupyter uses MathJax to format and display formulas. A formula starts and ends with either a single or a double dollar sign:
$ .... $
: in-line; the formula is in line with the rest of the current sentence$$ .. $$
: display mode; the formula stands out from the rest of the textInside, you need to enter a TeX-based equation. If you want to see all the options and play around with such equations, I recommend this website that offers a Tex equation editor.
JupyterLab pluginsIf you click on the puzzle icon at the left of the screen, you’ll get a list of plugins that can be installed directly. There are lots of plugins, and many of them will add extra visualization capabilities or integration with products like Tableau.
IPythonInternally, Jupyter Notebooks use a project called IPython. If you tend to use the Python REPL a lot, I recommend you to read our page on IPython since it is an excellent alternative to the regular REPL. I use it a lot personally, and I can’t recommend it enough! For example, it’s worth it just for its terrific auto-completion, and if you are used to Jupyter Notebooks, you’ll feel right at home, obviously.
IPython in action Jupyter Notebook in VSCodeIf you’re a VSCode fan, like me, you need to check out the VSCode extension that allows you to create and work with notebooks directly inside a VSCode tab. If you open up the command pallet, e.g., with ctrl + shift + p, start typing “Jupyter” to create, open, and export Notebooks.
Create a new Jupyter Notebook from VSCode Keep learningThis article is part of a free Python Tutorial. You can browse the tutorial with the navigation buttons at the top and bottom of the article or use the navigation menu. Want to learn more? Here are some great follow-up reads:
Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4