Markdown cells are used to store text formatted using Markdown syntax. A cheat sheet for Markdown can be found here. Most often these are used to introduce and describe the code that is being used and to make titles. Markdown cells can be selected in Jupyter Notebook by using the drop-down or also by the keyboard shortcut 'm/M' immediately after inserting a new cell. Headings The Headings starts with '#,' i.e., hash symbol followed by the space, and there are six Headings with the largest heading only using one hash symbol and the smallest titles using six.
Love them or hate them, notebooks are part of being a data scientist. Historically the only way to work with notebooks was using Jupyter Notebook - in 2017 we were given Jupyter Lab.
Jupyter Lab offers a superior development experience to Jupyter Notebook, with improvements such as:
- a file browser
- a text editor
- terminal access
- split views
This post is aimed at showing off what Jupyter Lab can do. If you are already a Jupyter user, skip straight to tips & tricks and important keyboard shortcuts.
Notebook 101
Installation & Use
Install without a space:
Start a server with a space:
You can then point your browser to
http://localhost:8888/lab
, and you’ll see the Launcher screen, shown below with the File Browser open in the sidebar:Wd security for mac. This download is provided to you free of charge. We also encourage you to check the files with your own antivirus before launching the installation. The software is periodically scanned by our antivirus system.
Unlike Jupyter Notebook, you can have multiple tabs open side by side - you can also keep track of your kernels by selecting Running Terminals & Kernels in the sidebar:
Notebooks vs. Jupyter Notebook vs. Jupyter Lab
Something that can be confusing is the difference between the
.ipynb
notebook file, Jupyter Notebook & Jupyter Lab:- a notebook file (
.ipynb
) is a JSON text file that defines the structure & code of a notebook - Jupyter Notebook & Jupyter Lab are programs that allow you to run & edit notebook files
Both Jupyter Notebook & Jupyter Lab can be used to run any
.ipynb
notebook file.Kernels & virtual environments
Another area for confusion is what exactly a kernel is. A kernel is just a running Python interpreter, usually connected to a notebook. It is possible swap kernels after they are running, but it’s not something we do a lot.
Kernels can be managed from the menu bar - restarting kernels is something that is done a lot. Unfortunately there are no default shortcuts setup for kernel management - see our post on Customizing Jupyter Lab Shortcuts to see how to set these up.
In order to control which virtual environment you are using in the notebook, it’s best to activate the environment before you install Jupyter Lab or start the server. For example, if you are using
conda
to manage virtual environments:You’ll also want to make sure you activated this environment before installing Jupyter Lab.
Modal editor
When editing notebooks, Jupyter Lab becomes a modal editor with two modes - Command and Edit:
- Command Mode for operating on cells
- Edit Mode for operating on text
A modal editor will operate differently depending on the mode - the same key will do different things based on what mode you are in. You can see what mode you are in on the status bar.
You can move between modes using:
Enter
to move from Command Mode to Edit ModeEscape
to move from Edit Mode to Command Mode
Cells can be run using
Shift Enter
in either mode, ending up in Command Mode.Cells
A notebook is composed of cells - blocks that contain text. The text inside a cell can either be code, Markdown or raw text. The ability to have mix executable code and Markdown documentation is a key feature of notebooks.
You can change the cell type using a dropdown:
You can change cell types using the following shortcuts in Command mode:
m
= change cell to Markdowny
= change cell to code
Three other useful Command Mode shortcuts for operating on cells are:
a
= insert cell aboveb
= insert cell belowdd
= delete cell
Mastering these five shortcuts will allow you to work with cells as efficiently as you work with text.
Jupyter Lab Tips and Tricks
This section is a rapid fire look at the features Jupyter Lab offers.
Tooltip
Pressing
Shift-Tab
will show a tooltip for the function or class your cursor is on:Contextual help
Similar to the tooltip, except it’s always there. You can have this help always shown in a separate pane by opening a Show Contextual Help window from the Launcher:
Consoles
Consoles are another way to interact with a running kernel - a cool trick is to connect a console to a Python script (
.py
):Markdown preview
No more committing
README.md
changes just to see the updates:?
and ??
These are actually
iPython
features, and will also work in an iPython
kernel:- a single
?
to see the docstring - a double
??
to see the source code
Running shell commands
Another
iPython
feature - running shell commands inside a notebook:You can use this to automatically install packages in the first cell of a notebook - make sure to use the
-q
flag to hide the output:%%autoreload
Autoreload helps deal with problem of source code outside the notebook not being reimported when it’s changed. This is only a problem when the source code changes after you start the kernel - essentially only when you are writing code yourself, external to the notebook.
Putting these two commands at the top of your notebook will mean that if you make changes to source code outside of the notebook, you can get these changes without having to restart the kernel.
Theme
The most important tip of all - a dark theme:
Jupyter Notebook Cheat Sheet
Keyboard Shortcuts Cheat Sheet
Below is a list of the most useful keyboard shortcuts in Jupyter Lab:
Cmd B
= toggle sidebarAlt W
= close tabEnter
= to move from Command to EditEscape
= to move from Edit to CommandShift Enter
= run cell
Command Mode
a
= insert cell aboveb
= insert cell belowdd
= delete cellz
= undo cellshift z
= redo cellm
= change cell to Markdowny
= change cell to code
Text mode
Cmd Z
= undo textShift Cmd Z
= redo text
Jupiter Lab Markdown Cheat Sheet Download
Thanks for reading!
Jupyter Markdown Indent
If you enjoyed this post, make sure to take a look at our post on Customizing Jupyter Lab Shortcuts, to learn how to add the few additional custom shortcuts needed to cover the entire data science workflow with your keyboard.