We can get help on an object using a question mark:
import numpy as np np.sum?
Or two question marks to also see the source code:
List all names in a module matching pattern:
np.cumsum np.einsum np.einsum_path np.nancumsum np.nansum np.sum
%quickref shows a quick reference card of features and shortcuts:
Use Git and the optional Unix tools from the Windows Command Prompt
We can also capture the output of a shell command:
notebooks = !ls *.ipynb
Magics are a simple command language which significantly extend the power of Jupyter.
There are two kinds of magics:
%lsmagic lists all available line and cell magics:
Question mark shows help:
Additional magics can also be installed or created.
Widgets add more interactivity to Notebooks, allowing one to visualize and control changes in data, parameters etc.
from ipywidgets import interact
interactas a function
def f(x, y, s): return (x, y, s) interact(f, x=True, y=1.0, s="Hello");
interactas a decorator
@interact(x=True, y=1.0, s="Hello") def g(x, y, s): return (x, y, s)
A few useful magic commands
Using the computing-$\pi$ notebook, practice using a few magic commands. Remember that cell magics need to be on the first line of the cell.
- In the cell with the for-loop over
N(throwing darts), add the
%%timeitcell magic and run the cell.
- In the same cell, try instead the
%%pruncell profiling magic.
- Try introducing a bug in the code (e.g., use an incorrect variable name:
- run the cell
- after the exception occurs, run the
%debugmagic in a new cell to enter an interactive debugger
hfor a help menu, and
help <keyword>for help on keyword
p xto print the value of
- exit the debugger by typing
- Have a look at the output of
%lsmagic, and use a question mark and double question mark to see help for a magic command that raises your interest.
Playing around with a widget
Widgets can be used to interactively explore or analyze data. Using the computing-$\pi$ notebook, introduce a widget which plots subsets of all the random points:
- Change the last two lines of the plotting cell into a function taking a tuple as argument, and slice the
def plot_points(n=(1,10)): x, y, colors = zip(*points[::n]) pyplot.scatter(x, y, c=colors)
- Add the
@interactdecorator above the function, and execute the cell.
- Drag the slider back and forth and observe the results.
- Can you think of other interesting uses of widgets in the computing-$\pi$ notebook?
Jupyter notebooks have a number of extra features that can come in handy.