Running Code

First and foremost, the JupyterLab is an interactive environment for writing and running code. JupyterLab is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel.

This notebook is associated with the IPython kernel. Therefore this notebook runs Python code.

Code cells allow you to enter and run code

Run an active code cell and advance using Shift + Enter or pressing the button in the toolbar above:

a = 10
a
10

As seen above, the state is preserved by kernel between cells: the first cell assigns a value 10 to a variable a, and the second cell returns it.

By convention, the return value of the last line of code cell is displayed after the execution.

These are the main keyboard shortcuts for running code cells:

  • Ctrl + Shift run the current cell and advance.

  • Ctrl + Enter run the current cell but don’t advance.

  • Alt + Enter runs the current cell and insert a cell below.

Please, see the Run and Kernel menus for all available cell execution options.

In addition, while editing any cell:

  • Ctrl + Shift + - splits the code cell in half.

And while navigating the cells:

  • Shift + m merges the selected cells.

Display functions allow you to display result

The implicitly available display function may be called to display values at any point of the execution:

display("The current value of 'a':")
a
"The current value of 'a':"
10

IPython, the core of JupyterLab, includes helper methods for formatting many kind of values for display or implicitly displayed return values.

For example, the code below displays an SVG image:

from IPython.display import SVG
SVG("""
<svg width="122" height="30">
  <rect width="122" height="30" rx="10" style="fill:green"/>
  <text x="10" y="20"  style="fill:white">Hello World!</text>
</svg>
""")
../../_images/01 Running Code_16_0.svg

Output is asynchronous

All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, depending on the Kernel, you may see the output one piece at a time, not all at the end.

import time
for i in range(8):
    display(i)
    time.sleep(0.5)
0
1
2
3
4
5
6
7

Managing the Kernel

Code is run in a separate process called the Kernel. The Kernel can be interrupted or restarted. Try running the following cell and then hit the button in the toolbar above to interrupt the kernel.

import time
time.sleep(30)

When the Kernel dies unexpectedly, you will be prompted to restart it.

i , i is the keyboard shortcut for interrupting the Kernel.

Restarting the kernels

The kernel maintains the state of a notebook’s computations. You can reset this state by restarting the kernel. This is done by clicking on the in the toolbar above.

0 , 0 is the keyboard shortcut for restarting the Kernel.

Execution will stop on exception

When multiple cell executions are on queue, the execution will stop at the first cell raising an exception.

assert False, 'This line always raises AssertionError'
---------------------------------------------------------------
AssertionError                Traceback (most recent call last)
<ipython-input-7-c6a11a2d9dbe> in <module>
----> 1 assert False, 'This line always raises AssertionError'

AssertionError: This line always raises AssertionError
print('And this line can only be executed manually.')