IPython/Jupyter Notebook tricks for advanced in 2019

This article summaries some of the most useful and interesting tricks for IPython/ Jupyter Notebook in 2019. If you have problems with some of them or new ideas please do share them.

Video on the topic: Jupyter Notebook tricks for advanced in 2019

In this article:

How to suppress output in IPython Notebook

The output of some cells can be huge or inappropriate and we may want to stop it or suppress it. You can do it in several different ways depending on your version, code and some other constraints:

Suppress simple statements output

For simple statements:

2*2

The output will be: 4

If you want to stop the output then you can add ';' at the end:

2*2;

and no output will be shown.

Suppress functions print output

If you want to stop the messages from print function with Jupyter Notebook you can use: %%capture.

This is an example how to use it:

def myfunc():
    print('Private Message')
myfunc()

the code will produce output:

Private Message

In order to prevent the printing to the output of the cell you can do:

%%capture
def myfunc():
    print('Private Message')
myfunc()

Suppress output for several statements

If you need to not show the output for several functions in Jupyter Notebook then you can use the next example. You need to call all functions and statements that you want to prevent from output with:
with io.capture_output() as captured: as follows:

from IPython.utils import io

def myfunc():
    print('Private Message')

with io.capture_output() as captured:
    myfunc()

Some cells still may produce output as plotly graphs and so on.

See function arguments/signature in IPython/Jupyter Notebook

Sometimes you want to do a quick check for a given functions - what and how many arguments you have or what is the signature for the given function. You have several different ways to achieve it.

Get function signature and body by shift and tab

The first way of getting the body of a function in IPython is by:

  • select the function name
  • press Shift + Tab

If you press it for first time then you will get an output like:

**Signature**: numpy.array_split(ary, indices_or_sections, axis=0)
**Docstring**:
Split an array into multiple sub-arrays.

You can continue to press again - Tab (While you hold the Shift ) and more info will be shown.

Alternatively you can get help by using ?

The other option to retrive information about functions and methods in IPython is by using: '?'. This can be done in a new cell by writing: ? and executing the cell. Which will produce information for IPython and how to get help.

Within IPython you have various way to access help:

  • ? -> Introduction and overview of IPython's features (this screen).
  • object? -> Details about 'object'.
  • object?? -> More detailed, verbose information about 'object'.
  • %quickref -> Quick reference of all IPython specific syntax and magics.
  • help -> Access Python's own help system.

Or you can ask for a given function by:

? numpy.array_split

which will return the following:

Signature:  numpy.array_split(ary, indices_or_sections, axis=0)
Docstring:
Split an array into multiple sub-arrays.

Please refer to the ``split`` documentation.  The only difference
between these functions is that ``array_split`` allows
`indices_or_sections` to be an integer that does *not* equally
divide the axis. For an array of length l that should be split
into n sections, it returns l % n sub-arrays of size l//n + 1
and the rest of size l//n.

See Also
--------
split : Split array into multiple sub-arrays of equal size.

Examples
--------
>>> x = np.arange(8.0)
>>> np.array_split(x, 3)
    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]

Get the list of all IPython magic functions

In order to check the information for all magic functions you can write in a new cell and execute:

%quickref

This will produce:

IPython -- An enhanced Interactive Python - Quick Reference Card
================================================================

obj?, obj??      : Get help, or more help for object (also works as
                   ?obj, ??ojupyter notebook magics    bj).
?foo.*abc*       : List names in 'foo' containing 'abc' in them.
%magic           : Information about IPython's 'magic' % functions.

Magic functions are prefixed by % or %%, and typically take their arguments
without parentheses, quotes or even commas for convenience.  Line magics take a
single % and cell magics are prefixed with two %%.

Example magic function calls:

%alias d ls -F   : 'd' is now an alias for 'ls -F'
alias d ls -F    : Works if 'alias' not a python name
alist = %alias   : Get list of aliases to 'alist'
cd /usr/share    : Obvious. cd -<tab> to choose from visited dirs.
%cd??            : See help AND source for magic %cd
%timeit x=10     : time the 'x=10' statement with high precision.
%%timeit x=2**100
x**100           : time 'x**100' with a setup of 'x=2**100'; setup code is not
                   counted.  This is an example of a cell magic.

System commands:

!cp a.txt b/     : System command escape, calls os.system()
cp a.txt b/      : after %rehashx, most system commands work without !
cp ${f}.txt $bar : Variable expansion in magics and system commands
files = !ls /usr : Capture system command output
files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc'

ipython_theme_help

Change the IPython Notebook theme

If you prefer to use dark themes with IPython Notebook you can very easily switch the main theme by installing: jupyterthemes:

pip install jupyterthemes

After that All you need to do is add a new theme by:

jt -t chesterish

Restoring the main theme can be done by

jt -r  

or even inside the jupyter notebook:

!jt -r  

Bonus: some useful jupyter notebook magics

  • List all kernels in jupyter notebook:
!jupyter kernelspec list

result:

Available kernels:
  python3    /usr/local/share/jupyter/kernels/python3
  • List the kernel in jupyter notebook:
import plotly
print (plotly.__path__)

result:

['/usr/local/lib/python3.7/site-packages/plotly']
  • Check python version:
!python -V

or

!which python

result:

Python 3.7.1
  • Check all python modules in IPython:
!pip freeze

result:

notebook==5.7.4
numpy==1.16.0
pandas==0.23.4
  • Install or update module in IPython:
!pip install --upgrade plotly 

result:

notebook==5.7.4
numpy==1.16.0
pandas==0.23.4
  • Install or update module in IPython:
!pip install --upgrade pip

result:

    100% |################################| 1.4MB 806kB/s eta 0:00:01
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-19.0.1
  • Check for environmental variable:
!echo $PATH  

result:
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Bonus 2: Top 10 most useful ipython key shortcuts

with Ctrl + m or Escape you can free the cell focus and execute additional commands like insert new cell:

Ctrl + m, c

Most useful commands are listed below:

  • Shift + Enter - run cell
  • Alt + Enter - run cell, insert below
  • Ctrl + m, c - copy cell
  • Ctrl + m, v - paste cell
  • Ctrl + m, l - toggle line numbers
  • Ctrl + m, j - move cell
  • Ctrl + m, y - code cell
  • Ctrl + m, m - markdown cell
  • Ctrl + m, . - restart kernel
  • Ctrl + m, h - show keyboard shortcuts

If you are interested in more about jupyter notebook you can check also:

Related Article