Python Friday #183: Advanced Tips & Tricks for JupyterLab

JupyterLab offers us many little tricks to work more effectively. In this post we explore a few helpful tips I would no longer want to miss.

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.

 

Code completion

When we write code in the Jupyter notebook, we can type the name of the methods and functions by ourselves. However, a much faster and less error-prone way is to type the first few characters and then hit the tabulator key (Tab) to get the code completion suggestions in Jupyter in the same way we are used to in our IDE:

Jupyter comes with a code completion that we can access with the tabulator key

 

Show the documentation

When we are typing our method names, we can use Shift+Tab to access the documentation for that method immediately:

The documentation pops up when we hit Shift+Tab.

If we add a ? behind the method name and run the cell with Shift+Enter, we get the documentation as part of the result:

The documentation is now below our cell.

 

Toggle line numbers

We can show (or hide) the line numbers in the cells with the key L when we are in the command mode. That toggles between showing and hiding the line numbers:

The cells now have line numbers.

 

The magic commands

Jupyter offers us magic commands that are prefixed with a % for line commands and with a %% for commands that work on the whole cell. We can get a list of all available magic commands with %lsmagic:

%lsmagic gives us all available magic commands

If we want to know more about a magic command, we can use the help system and type the command followed by a ?:

The docstring for %rerun

 

Accessing the environment variables

We can use %env to read our environment variables. We can also set a value for a variable, but be aware, that this will only affect the current notebook and vanish as soon as you stop the notebook.

We can access the environment variables from JupyterLab

 

Show the history

We can get a list of the last commands we run in the notebook with the %hist magic command:

%hist shows us the last commands we run in this notebook

 

Access the command prompt

We can run commands on the terminal with the cell magic command %%cmd that will execute the remaining lines of the cell in your terminal and show you the result:

We can access the comman terminal from JupyterLab

 

Next

With these tricks you should be able to get a much nicer experience when working with JupyterLab. Next week we explore a few of the performance-related magic commands that help you to figure out how long things run and if we have code that we need to optimise.

1 thought on “Python Friday #183: Advanced Tips & Tricks for JupyterLab”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.