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:
1. Starting JupyterLab
For installation of JupyterLab see step 1b here
There are 2 ways
Using a terminal
Open the program Anaconda Prompt (Windows) or Terminal (Mac)
Write
jupyter lab
and hit Enter
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:
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:
Code cells with Python code
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:
File Browser: To open existing notebooks anywhere on your computer.
Running Terminals and Kernels: To shutdown terminals and kernels.
Git: Open the Git extension for JupyterLab and do your pulls, pushes, diffs etc.
Table of Contents: To see links to sections based on headings (#, ##, ###).
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.
Hit
Ctrl+Shift+C
(Windows) orCmd+Shift+C
(Mac)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:
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.
Open the Command Palette (see above) and search for Advanced Settings Editor
Open 'Keyboard Shortcuts'
In the
User Preferences
tab paste in the code below. Mac users: ReplaceCtrl
withAlt
.
{
"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
.
Restart kernal and run all cells: Ctrl+Space
Toggle presentation mode: Ctrl+Shift+P
Toggle line numbers: Ctrl+Shift+K
Move cell up: Ctrl+↑ (only in command mode)
Move cell down: Ctrl+↓ (only in command mode)