JupyterLab is a great tool for working with data. As with all tools, work goes much faster if you know their quirks and idiosyncrasies. Today we look at some small but important things that can help you in your daily work with JupyterLab.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Run the current cell
The most often used keyboard shortcut in JupyterLab is Shift + Enter. That runs the current cell and gives us immediate feedback. Jupyter allows us to run cells in arbitrary order, what is a great help to quickly change the things we want. We see the execution order as the small number in front of our code cells:
Rerun all cells from time to time
We can move cells around and run them in any order we want. That flexibility can lead to a notebook that only works for us and nobody else. We can prevent this problem by restarting the kernel and running all cells from top to bottom:
Edit mode and command mode
When we work with a notebook, we run in two basic modes: edit and command. When we edit a cell we are in the edit mode, if we click somewhere outside a cell in our notebook, we enter the command mode. As long as you use your mouse to navigate, you will not notice much difference. But as soon as you start using keyboard shortcuts, those two modes behave differently.
Keyboard shortcuts
The menu and the buttons in the notebook show you all the possibilities. If you want to skip the context switches from entering data to navigate with your mouse to the action you want to do, you should learn a few keyboard shortcuts.
Esc | switch from edit mode to command mode |
Up / Down | the arrow keys allow you to move between cells [command mode] |
Enter | switch from the command mode to the edit mode of the active cell |
Shift + Enter | run the current cell [edit mode] |
00 | the two 0 restart the kernel [command mode] |
M | change the current cell into a Markdown cell [command mode] |
Y | change the current cell into a code cell [command mode] |
A | insert a cell above the current cell [command mode] |
B | insert a cell below the current cell [command mode] |
Ctrl + Shift + – | split the current cell at the cursor [edit mode] |
Shift + Up | select the current cell and the one above [command mode] |
Shift + Down | select the current cell and the one above [command mode] |
Shift + M | merge the selected cells into one [command mode] |
There are many more shortcuts, but I suggest you focus the ones above first before you learn more.
Create your own shortcuts
If the default keyboard shortcuts are not enough for you, you can define your own. Go to Settings / Advanced Settings Editor to adapt the shortcuts to your needs:
Use ; to suppress object details
If we show a graph of our data, we may not only get the graphic but also a textual representation of our object:
If we do not want to see these details, we can use a semicolon ( ; ) at the end of the line in JupyterLab and the textual representation is supressed:
Shutdown notebooks
JupyterLab start a new kernel for every notebook you run. If you work with many notebooks at once, you may run out of kernels. Should that happen, you can right-click on your notebook in the File Browser of JupyterLab and shut down the kernel:
Next
The basic tricks covered in this post allow us to work effectively with notebooks in JupyterLab. Especially the shortcuts turn out to be a great help as soon as you get used to them. Next week we explore the different ways we have to share a notebook.
1 thought on “Python Friday #162: Tips & Tricks to Work Effectively With JupyterLab”