Jupyter tips and tricks
Magic commands¶
First of all, we can use built-in magic commands. The full list of commands can be obtained:
In [1]:
%lsmagic
Out[1]:
We can use also shell commands:
In [2]:
!ls
!pwd
It can be useful, for example if we want to use some fast shell command like sed, awk or just
In [5]:
#first 10 lines of the file:
!head -2 iris.csv
In [11]:
#how to find a specific line with a string using grep:
!grep -m2 -i 'versicolor' 'iris.csv'
In [13]:
#replace string, where searchingStr, newStr are searching string and new string respectively:
!sed -e 's/setosa/versicolor/g' 'iris.csv'
All of this beauty we can use as variable in our notebook:
In [16]:
files = !ls ./*.csv
files[0]
Out[16]:
Very powerfull command is psource, that can show us signature of functions and source code. Also we can use command ?, ?? for that. For capturing output from pager to our notebook we can overload method (also see, stackoverflow's discussion about it.):
In [24]:
from __future__ import print_function
from IPython.core import page
page.page = print
In [26]:
%psource %psource
If we want to run other python file or even notebook file (*.ipynb), command %run helps us.
In [ ]: