Using Jupyter¶

Cells¶

A Jupyter sheet is made up of cells that you can guess if you click on the text. There are different cell types that you choose with the drop-down menu in the middle of the icons at the top of the page:

  • Python code (default)
  • rich text (Markdown)
  • plain text (useful for deactivating a code cell)

You can add a cell with the + symbol at the top of the page, delete a cell with the scissors, move a cell with the arrows and edit it by double-clicking on it. We will see that we execute the cells to obtain the result of the written code. All this can be found on the menu Cell.

Kernel¶

Jupyter takes its name from 3 languages, namely Julia, Python and R. Today the list of languages ​​that can be to use in a Jupyter sheet is much longer but in all cases you need a kernel which makes the interface between Jupyter and the language. In the case of Python the core is iPython. We choose it when creating the sheet and we can interact with it using the Kernel menu.

The other menus are more classic and do not require explanation (forget Widget).

Note¶

If you do not know Python or Jupyter, it is advisable to come back here after reading the first chapter .

Cell manipulation¶

Status and execution¶

  • A cell can have 3 states:
    • not selected (no frame around the cell)
    • selected (blue frame or blue bar on the left)
    • selected and in edit mode (change of background color and sometimes green frame)
  • Clicking selects a cell (or even puts it directly into edit mode for calculation cells)
  • Double click selects and enters editing mode (on a text area, Markdown syntax appears)
  • Enter like clicking allows you to activate the edition when Esc allows you to close the edition mode.
  • To make Python recompute the result of a cell
    • Shift+Enter does the calculation and moves to the next cell
    • Crtl+Enter does the calculation and stays on the cell
    • Alt+Enter does the calculation and then adds an empty cell.

Display¶

  • [out] To hide the results, end the last line of the cell with ;.
  • [In] To hide a cell, use 'Hide input' from nbextessions (see config in the Edit menu).
In [ ]:
print(2)
1+1;   # remove the ; to see the difference

Configuration¶

Jupyter has many commands to manipulate your note sheet and of course it is possible to set keyboard shortcuts.

To display the list of commands (the command palette) click on the small keyboard in the icon bar. With Chrome, you can display this command palette with Ctrl+Shift+P.

To display and define your keyboard shortcuts you can go to the help menu or type shortcut in the command palette.

For example, for my use I redefined the cell type change with

  • Crtl+1 for Code mode
  • Crtl+2 for Markdown mode
  • Crtl+3 for Raw mode (uninterpreted data)

This works when the cell is selected but not in edit mode (so blue frame or blue bar).

Exercise¶

Choose your shortcuts to switch from one mode to another then switch the cell below to Code mode and execute it.

print('$qsD!Me+nsodd= 1l lre3W$'[-2:2:-2])

Cell manipulation shortcuts are good to know or redefine when you start using Jupyter seriously.

IPython¶

When using Python in Jupyter, we rely on IPython which enriches Python.

This gives the following functionality:

  • Completion by pressing Tab
  • Help with ? and ?? for even more help with objects (or even code)
  • Shell access with !
  • Magic commands

Completion and help¶

aTab will give a list including abs

In [ ]:
a

For the help examples, you must launch the cells to see the result.

In [ ]:
abs?

To close the help window click on the cross at the top right of the window or use Esc.?

In [ ]:
mot = "Coucou"
mot.split?

Shell under IPython¶

In [ ]:
!pwd
In [ ]:
vlib = '/var/lib/'
res = !ls $vlib
res = res.grep('lib')
for f in res:
    print(f)
In [ ]:
print(type(res))
res??

Magic commands¶

Magic commands improve the use of Python. They start with

  • % and apply to the line
  • %% and apply to the cell (when it makes sense)

The first magic command to run is %quickref to get the list of iPython possibilities.

In [ ]:
%quickref

Here is a selection of some magic commands:

  • %time to get the time the line takes (%%time for the execution time of the cell).
  • %timeit is like %time but it restarts the line several times to get a more accurate estimate
  • %prun to run the line through the Python profiler and see where it's taking time
  • %matplotlib inline so that the graphs are displayed in the Jupyter page
  • %load to load a Python file into the cell (comments %load once executed)
In [ ]:
!echo 'print("coucou")' > /tmp/prog.py
In [ ]:
%load /tmp/prog.py
In [ ]:
import numpy as np
import scipy.linalg as lin

%prun lin.solve(np.random.random([3000,3000]), np.random.random(3000))

{{ PreviousNext('Introduction.ipynb','../lesson1 Python basics/00 - Premiers calculs.ipynb') }}

In [ ]: