IPython / Jupyter Cheatsheet
IPython / Jupyter¶
- Using IPython makes interactive work easy.
- Better shell
- Notebook interface
- Embeddable kernel
- Parallel python
IPython shell shortcuts¶
- TAB expansion to complete python names and file paths
- ~ and * directory / file expansion
- many "magic" methods:
Help¶
? # overall help
help # python help system
?someobj or someobj? # help
??someobj or someobj?? # detailed help
%pdoc %pdef %psource for docstring, function definition, source code only.
Run¶
To run a program directly from the IPython console:
%run has special flags for timing the execution of your scripts (-t) or for running them under the control of either Python's pdb debugger (-d) or profiler (-p):
Other Commands¶
%edit %ed # edit then execute
%save
%load example.py # load local (example) file (or url) allowing modification
%load https://matplotlib.org/plot_directive/mpl_examples/mplot3d/contour3d_demo.py
%macro # define macro with range of history lines, filenames or string objects
%recall
%whos # list identifiers you have defined interactively
%reset -f -s # remove objects -f for force -s for soft (leaves history).
%resetis not a kernel restart- Restart with
Ctrl+.in "qtconsole" import module ; reload(module)to reload a module from disk
Debugging¶
%debug # jump into the Python debugger (pdb)
%pdb # start the debugger on any uncaught exception.
%cd # change directory
%pwd # print working directory
%env # OS environment variables
OS Commands¶
History¶
_ __ ___ # etc... for previous outputs.
_i _ii _i4 # etc.. for previous input. _ih for list of previous inputs
GUI integration¶
Start with ipython --gui=qt or at the IPython prompt:
Arguments can be wx, qt, gtk and tk.
Matplotlib / pylab graphics in an iPython shell¶
Start with: ipython --matplotlib ( or --matplotlib=qt etc...)
At the IPython prompt:
%matplotlib # set matplotlib to work interactively; does not import anythig
%matplotlib inline
%matplotlib qt # request a specific GUI backend
%pylab inline
%pylab makes the following imports:
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs
from pylab import *
from numpy import *
Qtconsole - an improved console¶
At the command prompt:
alternative: --matplotlib inline or within IPython:
To embed plots, SVG or HTML in qtconsole, call display:
from IPython.core.display import display, display_html
from IPython.core.display import display_png, display_svg
display(plt.gcf()) # embeds the current figure in the qtconsole
display(*getfigs()) # embeds all active figures in the qtconsole
#or:
f = plt.figure()
plt.plot(np.rand(100))
display(f)
ipython and ipython notebook for matlab users
IPython Notebook web-based interface¶
- Start with: ipython notebook and switch to browser
- Keyboard shortcuts:
Enterto edit a cellShift + Enterto evaluateCtrl + morEscfor the "command mode"
In command mode:
h list of keyboard shortcuts
1-6 to convert to heading cell
m to convert to markdown cell
y to convert to code
c copy / v paste
d d delete cell
s save notebook
. to restart kernel
Papermill¶
Papermill is a tool for parameterizing and executing Jupyter Notebooks.