jupyter_hacks

Jupyter has ocean of kool tricks.
I like to put some of them here which are a lot of use to me.
You can use jupyter cells as Linux terminal by just placing '!' in the beginning.

Instead of everytime importing python libraries, can save them in ipython configuration file and start using them directly everytime you open jupyter notebook.

You can use jupyter cells are linux terminal by just placing '!' in the beginning

For that open ~/.ipython/profile_default/ipython_config.py file.

Adding libraries to jupyter notebook by default

In [1]:
import os
In [2]:
# Check if the file is available
file = os.path.expanduser('~/.ipython/profile_default/ipython_config.py')
os.path.isfile(file)
Out[2]:
False
In [3]:
# Now we'll create the file 
os.system('touch {}'.format(file))
Out[3]:
0

Now we'll insert the following text that has some of the python libraries
You can add any other libraries that you want to it to be loaded by default

Sometimes funny indentation creeps in with multiline string output,
So we'll import textwrap for this

In [4]:
import textwrap
In [5]:
with open(file,'w') as f:
    f.write(textwrap.dedent(
    '''
    c = get_config()
    c.InteractiveShellApp.exec_lines = [
        'import numpy as np\\n'
        'import scipy as sp\\n'
        'import matplotlib as plt\\n'
    ]
    '''))

Now when you restart the notebook by pressing '00' or when you load a new notebook you will be able to access all the above libraries without importing them.
Now sitback and relax 🛀

Underscore

Usually, underscore '_' is used for storing the last expression in the interpreter.
In jupyter also it is similarly used.
Above that, we can access particular cell output using underscore.

In [6]:
'random string'
Out[6]:
'random string'
In [7]:
# So, to access cell 6 output, we can use 
_6
Out[7]:
'random string'

Multiple statements output

By default, jupyter gives the output of only last statement. But if you want to output multiple statements, use this

In [8]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
In [9]:
import random
In [10]:
random.randint(0,10)
random.randint(0,10)
Out[10]:
5
Out[10]:
8

Here using underscore we can access the last output statement in the cell

In [11]:
_10
Out[11]:
8

Documentation Help

You can get help documentation about built-in functions or about imported library function using '?'

In [12]:
sum?

or this, both way works

In [13]:
?sum
In [14]:
textwrap.dedent?

A nice pop up comes giving the description

Loading python files in jupyter

You can load python files in jupyter cell

In [ ]:
%load python_file_name.py

We can execute those python files

In [16]:
%run python_file_name.py
Hello world!

Above command can also be used to execute jupyter notebooks

Magic commands

Who

To find all the variables of particular type

In [18]:
x = 4
y = 5
name = 'rakend'
In [19]:
%who int
x	 y
In [20]:
%who str
file	 name

This shows what all the variables of this type are used

Timing

To find the time taken for executing particular cell

In [21]:
import random
In [22]:
%%time
for i in range(100000):
    random.randint(0,10)
CPU times: user 174 ms, sys: 157 µs, total: 175 ms
Wall time: 173 ms
In [23]:
%%timeit
random.randint(0,10)
1.52 µs ± 47.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

This %%timeit, runs the command for million times and gives the average time taken

Executing from different kernel

In [24]:
%%python2
print 'hi'
hi

Other kernels available

%%perl %%python3 %%ruby %%bash %%HTML

Below cell is for notebook styling, you can ignore it

In [15]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))