JupyterLab & notebook tips

Unit 04

JupyterLab: shortcuts and tips

To efficiently write code in an integrated development environment (like JupyterLab) it is useful to know several keyboard shortcuts for tasks that you have to perform regularly or in bulk.

Here is a table of JupyterLab shortcuts that I often use and appreciate. Please note that they are different from the Jupyter notebook shortcuts that allow you to navigate and execute cells in a notebook. The shortcuts presented here, are meant to be applied within a code cell of a notebook, but also within a .py file or a python console.

Shortcut Task
[Ctrl-/] Toggle comment/uncomment line or block of highlighted lines
[TAB] Indent line or block of highlighted lines by 4 spaces
[Ctrl-TAB] Dedent line or block of highlighted lines by 4 spaces
[Ctrl-A] Highlight all text in file or cell
[Ctrl-C] Copy highlighted text
[Ctrl-X] Cut highlighted text
[Ctrl-V] Paste text
[Ctrl-Arrow] Jump cursor to beginning/end of current word
[Ctrl-Shift-Arrow] Jump cursor to beginning/end of current word and highlight the word (I find this useful to copy/cut/delete text without the need to use the mouse)
Unfortunately, there is no shortcut available in our current version of JupyterLab to delete single code lines. It used to be [Ctrl-D], but the shortcut is broken for specific JupyterLab versions.

Jupyter Notebook tips

In the following video I demonstrate you how you can work interactively in a Jupyter notebook, which does not allow you to run single lines of code, but rather entire code cells only. You will also find an explanation and a solution to the issue that interactive help (by invoking [Shift-TAB]) is not always availble to the session.

In case the video does not play, you can follow this link to the video, or right-click on the link and select Save As... to download the mp4 file directly and play it locally. The video is also available on ILIAS at 04_unit/ipynb_interactive_ex0301_convertedB.mp4.

Python tip on source code

Sometimes it can be very useful to inspect the source code of functions you are using. You can do this in python like that:

import numpy as np
import inspect

src = inspect.getsource(np.array_equal)
print(src)

Run the code cell above yourself to see the source code of the np.array_equal() function that we coded from scratch ourselves last week.

Learning checklist

  • I know several useful keyboard shortcuts that allow me to work efficiently with Jupyter notebooks.
  • I have a strategy for developping code in a notebook, also when the code chunks get more complicated and contain loops, etc.
  • I know how to access interactive help for objects (mainly functions and methods) in Jupyter-lab. I regularly use interactive help to write code myself.