Jupyter Lab – Shortcut and Magic Functions


In one of my post, I had written about installing Jupyter Lab and configuring it. If you haven’t installed it yet, you can check this post for the information. I have been using these Jupyter lab shortcuts and magic functions in my day to day work and have explored some of the shortcuts and tips of working with it. These have made my experience better with Jupyter lab and have made me more productive. If you find it helpful then bookmark this page for a quick reference in the future.

Shortcut Keys for Jupyter lab

While working with any tools, it helps if you know shortcut key to perform most frequent tasks. It increases your productivity and can be very comfortable while working. I have listed down some of the shortcuts which I use frequently while working on Jupyter Lab. Hopefully, it will be useful for others too. Also, you can check full list of shortcut by accessing the commands tab in the Jupyter lab. You will find it below the Files on the left hand side.

  1. ESC  takes users into command mode view while ENTER takes users into cell mode view.
  2. inserts a cell above the currently selected cell. Before using this, make sure that you are in command mode (by pressing ESC).
  3. B inserts a cell below the currently selected cell. Before using this make sure that you are in command mode (by pressing ESC).
  4. D + D = Pressing D two times in a quick succession in command mode deletes the currently selected cell. 
  5. Jupyter Lab gives you an option to change your cell into Code cell, Markdown cell or Raw Cell. You can use M to change current cell to a markdown cell, Y to change it to a code cell and  R to change it to a raw cell.
  6. CTRL + B = Jupyter lab has two columns design. One column is for launcher or code blocks and another column is for file view etc. To increase workspace while writing code, we can close it.  CTRL + B is the shortcut for toggling the file view column in the Jupyter lab.
  7. SHIFT + M = It merges multiple selected cells into one cell. 
  8. CTRL + SHIFT + – = It splits the current cell into two cells from where your cursor is. 
  9. SHIFT+J or SHIFT + DOWN = It selects the next cell in a downward direction.  It will help in making multiple selections of cells.
  10. SHIFT + K or SHIFT + UP = It selects the next cell in an upwards direction. It will help in making multiple selections of cells.
  11. CTRL + / = It helps you in either commenting or uncommenting any line in the Jupyter lab. For this to work, you don’t even need to select the whole line. It will comment or uncomment line where your cursor is. If you want to do it for more that one line then you will need to first select all the line and then use this shortcut. 

Jupyter Lab Magic Functions

Magic functions are very specific to Jupyter lab or Jupyter notebook and are provided by the IPython kernel. It adds extra functionality to our Jupyter lab notebooks which is not limited by our choice of language. This also provides some of the IDE type functionality to Jupyter Lab.

  1. %matplotlib inline = Most people must be already knowing about this. It allows the output of plotting command to be displayed inline i.e. in Jupyter lab UI.
  2. %lsmagic =It lists all the available magic function for the Jupyter lab. It can be useful if you want to explore all the available magic functions.
  3. %env =It lists all the environment variable. It can be used in the following way:-
    • %env: lists all environment variables and its values
    • %env var: gives value for var environment variable
    • (%env var val) or (%env var=val): sets value for var environment variable
  4. %run = can run .py and .ipnyb code file in the cell.
    • %run file_name
  5. %load = loads code into the cell. It can use various source for this i.e. filename, URL etc.
    • %load source
  6. %who = will list all variables that exist in the global scope. It can be used to see what all data_frames or any other variable is there in memory.
    • %who: all variables present
    • %who df: lists all the data frames present
    • %whos: same as %who, but it gives more information about each variable
  7. %time = will give you information about the time taken in a single run of the code in your cell.
  8. %timeit = uses the Python timeit module which runs a statement 100,000 times and gives the average time taken.
  9. %writefile = writes the contents of the cell to a file
    • %writefile file_name
    • %writefile -a file_name: This will append contents of the cell to an existing file
  10. We can use IPython Magics with the name of a kernel at the start of each cell that we want to use that Kernel for. For e.g. %%bash %%HTML %%python2 %%python3 %%ruby %%perl

Change in Jupyter configuration file

In Jupyter Lab, cells print O/P of the latest print statement. If we use more than one print statement, it overrides the previous O/P. But we can change it so that cells show all the output by running the below code

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

This won’t change the behavior permanently and we will need to run above code in every notebook. We can make it as a default behavior by changing the configuration file. We will need to add this in the configuration file. You can check this post to know about Jupyter Lab configuration file and how to make changes in it.

c.InteractiveShell.ast_node_interactivity = "all"

These are few of the options available to a user in terms of both magic functions and shortcut. You can explore more by visiting the official page about IPython magic functions and find more which is needed for your working. If you use any other magic functions or shortcut which can be useful to others, let us know in the comment section.

Facebook Comments