Running Python in JupyterLab

 

JupyterLab runs in your internet browser. It 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

There are 2 ways

  1. Using a terminal

    • Open the program Anaconda Prompt (Windows) or Terminal (Mac)

    • Write jupyter lab and hit Enter

  2. Anaconda Navigator

    • Open the program Anaconda Navigator and launch JupyterLab by mouse click. There you'll also find a bunch of other programs available.


Hopefully, you will experience that your default 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. Git: Open the Git extension for JupyterLab and do your pulls, pushes, diffs etc.

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

  5. Extension manager: See all installed extensions as well available.


The interface of JupyterLab is explained in detail here.

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

 

3. Command palette


It's handy to know that all commands in JupyterLab can be called from the Command Palette, which is essentially a search tool for available commands.

  1. Hit Ctrl+Shift+C (Windows) or Cmd+Shift+C (Mac)

  2. Start typing or browse for the command you want to apply

 

4. 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

 

5. Shortcuts


Other good-to-know standard short-cuts (on Mac, use ⌘ in stead of Ctrl)

  • 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

    • Merge cells: Shift+M

    • 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.

  1. Open the Command Palette (see above) and search for Advanced Settings Editor

  2. Open 'Keyboard Shortcuts'

  3. In the User Preferences tab paste in the code below. Mac users: Replace Ctrl with Alt.

{
    "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 short-cuts below. Mac users: use ⌥ instead of Ctrl.

  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