JupyterLab Environment

What is a Notebook?

Notebooks are documents that can contain code (like python) and rich text elements (paragraphs, equations, figures, links). You can edit and re-run cells, in place, in a notebook. Notebooks are:

  • Executable documents that can be run to perform data analysis.

  • Human-readable documents that can contain the analysis description and results.

Using a Notebook

In the Launcher window, by default, there is a Python 3 notebook icon that you can use to launch a Python 3 notebook. If you are logged into the R server, there will also be an R notebook icon.

Python notebook and console options in JupyterLab launcher.

How to Switch Between Code, Markdown, and Raw Inputs

This is how you switch between the Code, Markdown, and Raw input types for a cell in a notebook:

  1. Select the cell you want to modify.

  2. Go to the Select the cell type dropdown menu at the top of the notebook window (the default selection is Code):

    Notebook select the cell type dropdown menu.
  3. In the dropdown menu, select the cell type you want to use:

    Notebook select the cell type dropdown menu opened showing Code, Markdown, and Raw options.

Edit and Command Modes

Jupyter notebooks have two modes of interaction, edit and command. The mode you are in is shown in the bottom-right corner of your notebook:

Jupyter notebook mode displayed in bottom-right corner of the notebook.

Edit Mode

To enter edit mode, click inside a cell.

In edit mode, you can:

  • Edit the contents of a cell

Command Mode

To enter command mode, press the Esc key or click outside of a cell.

In command mode, you can:

  • Navigate between cells

  • Add and delete cells

  • Change the cell type

  • Rearrange cells

What is a Console?

Consoles allow you to run code in a kernel.

In consoles, you can:

  • Display rich text elements from code output.

  • Edit and re-run code in a new cell.

In consoles, you can’t:

  • Input rich text elements separate from a code output.

  • Edit and re-run cells in place.

Using a Console

In the Launcher window, by default, there is a Python 3 console icon that you can use to launch a Python 3 console. If you are logged in to the R server, there is also an R console icon.

Python notebook and console options in JupyterLab launcher.

How to Clear the Console

To clear the console without restarting the kernel, right-click inside the console and select Clear Console Cells.

Console right-click menu that includes clear console cells option.

How to Browse Your Input History

Browse your cell input history using your up and down keys inside the console input cell. You can edit and re-run previously run code with this method more quickly than re-typing.

Jupyter Best Practices

These are best practices to keep in mind when using JupyterLab/Jupyter Notebook.

  • Your entire notebook should run from top to bottom.

    In a notebook, go to the Run menu and select Restart Kernel and Run All Cells. Your notebook should run without errors. If there are errors, troubleshoot/resolve them and try again.

  • Take time to improve your notebook’s readability.

    For example, a redundant cell that re-imports a module that was imported in a higher cell, hurts readability.

    A readable notebook is easier for you to understand after a hiatus and makes it easier for someone else to help you troubleshoot an issue.

  • If you copy an existing notebook or code from another source, actively review it before using it (instead of just copying, pasting, and running). Some things to consider are:

    • Is the code still in active development or has it been abandoned (no longer being maintained)?

      For example, fields like machine learning (ML) are changing rapidly. An ML notebook or code that was relevant a few years ago may not be today, if it isn’t being maintained.

    • Who are the developers? Is there industry backing? Is it a solo developer’s fork of something else?

      Understanding the source can help you decide if it will work for your needs or if you should continue to search.