Running Python in JupyterLab

 

JupyterLab is an awesome way of combining text and code in the same framework. This is a short guide on how to use JupyterLab. If you haven't seen JupyterLab before it looks like this:

jupyterlab

1. Starting JupyterLab


For installation of JupyterLab see step 1b here

  1. Open the program Anaconda Prompt (Windows) or Terminal (Mac)

  2. Paste in jupyter lab + Enter


Hopefully, you will experience that you browser automatically opens a new tab looking something like this:

jupyterlab-open

Note: Despite JupyterLab is running in a browser, it is running offline (the path is something like localhos:8888/lab).

 

2. Creating a notebook


In the Launcher tab you create a new Jupyter notebook by pressing the Python 3 bottom under Notebook. Notebooks consists of two types of cells:

  1. Code cells with Python code

  2. Markdown cells with text (see the guide Writing markdown)


When inside a cell you are in edit mode, when not you are in command mode.

The most important notebook commands are:

  • Movements: Arrows and scrolling

  • Run cell and advance: Shift+Enter

  • Run cell: Ctrl+Enter

  • Enter edit mode: Enter

  • Enter command mode: Ctrl+M

  • Change to markdown cell: M (only in command mode)

  • Change to code cell: Y (only in command mode)


In the left-panel on JupyterLab you can e.g. access:

  1. File Browser: To open existing notebooks anywhere on your computer.

  2. Running Terminals and Kernels: To shutdown terminals and kernels.

  3. Command Palette: To see a list of possible commands.

  4. Table of Contents: To see links to sections based on headings (#, ##, ###).


The interface of JupyterLab is explained in detail here.

The use of Jupyter notebooks in JupyterLab is explained in detail here.

 

3. Variable inspector


A very nice feature of JupyterLab is that it has a variable inspector that visualize vectors, DataFrames and matrices. You can open it by right-clicking in a notebook and choose "Open Variable Inspector". It looks like this:

jupyterlab-variable-inspector

4. Shortcuts


Other good-to-know standard short-cuts are:

  • Only in command mode:

    • Create new cell above: A

    • Create new cell below: B

    • Cut cell: X

    • Copy cell: C

    • Paste cell: V

    • Delete cell: D+D

    • Toggle sidebar: Ctrl+B

    • Restart kernel: 0+0

  • Single-document mode: Ctrl+Shift+D

  • Autocomplete (when writing code): Tab

  • Show tooltip: Shift+Tab (used when inside function)


Advanced: Additionally, you can create customized short-cuts. Open settings with Ctrl+,.

  1. Open 'Settings: Advanced Settings Editor

  2. Open 'Keyboard Shortcuts'

  3. In the User Preferences tab paste in the followng:

{
    "shortcuts": [
        {
            "command": "runmenu:restart-and-run-all",
            "keys": [
                "Ctrl Space"
            ],
            "selector": "[data-jp-code-runner]"
        },
        {
            "command": "notebook:move-cell-up",
            "keys": [
                "Ctrl ArrowUp"
            ],
            "selector": ".jp-Notebook:focus"
        },
        {
            "command": "notebook:move-cell-down",
            "keys": [
                "Ctrl ArrowDown"
            ],
            "selector": ".jp-Notebook:focus"
        },
        {
            "command": "application:toggle-presentation-mode",
            "keys": [
                "Ctrl Shift P"
            ],
            "selector": "body"
        },
        {
            "command": "viewmenu:line-numbering",
            "keys": [
                "Ctrl Shift K"
            ],
            "selector": ".jp-Notebook.jp-mod-commandMode"
        }
    ]
}


You now have access to the following short-cuts:

  1. Restart kernal and run all cells: Ctrl+Space

  2. Toggle presentation mode: Ctrl+Shift+P

  3. Toggle line numbers: Ctrl+Shift+K

  4. Move cell up: Ctrl+ (only in command mode)

  5. Move cell down: Ctrl+ (only in command mode)

 

Next guide


Running Python in VSCode