import addutils.toc ; addutils.toc.js(ipy_notebook=True)
from addutils import css_notebook
css_notebook()
The Jupyter Notebook is an interactive computing environment that enables users to author notebook documents that include:
These documents provide a complete and self-contained record of a computation that can be converted to various formats and shared with others using email, Dropbox, version control systems (like git/GitHub) or nbviewer.jupyter.org.
To find more documentation and examples check the follwing links:
The document you are reading now is precisely an example of one such notebook, and we will show you here how to best use this new interface. The first thing to understand is that a notebook consists of a sequence of 'cells' that can contain either text or code meant for execution:
%magics, !system calls
, etc). When you type Shift-Enter
, the cell content is executed, output displayed and a new cell is created below. Try it now by putting your cursor on the next cell and typing Shift-Enter
:print ('This is the new IPython notebook')
This is the new IPython notebook
You can re-execute the same cell over and over as many times as you want. Simply put your
cursor in the cell again, edit at will, and type Shift-Enter
to execute.
Tip: A cell can also be executed
in-place, where IPython executes its content but leaves the cursor in the same cell. This is done by
typing Ctrl-Enter
instead, and is useful if you want to quickly run a command to check something
before typing the real content you want to leave in the cell. For example, in the next cell, try issuing
several system commands in-place with Ctrl-Enter
, such as pwd
and then ls
:
pwd
'/home/matteo/Projects/tutorials/python-ipython'
In a cell, you can type anything from a single python expression to an arbitrarily long amount of code (although for reasons of readability, you should probably limit this to a few dozen lines):
def f(x):
'''My function description'''
return x+6
value = 3
print ('f({0:.2f})= {0:.2f}'.format(value, f(value)))
f(3.00)= 3.00
The Notebook allows code to be run in a range of different programming languages. For each notebook document that a user opens, the web application starts a kernel that runs the code for that notebook. Each kernel is capable of running code in a single programming language and there are kernels available in the following languages:
The default kernel runs Python code. The notebook provides a simple way for users to pick which of these kernels is used for a given notebook.
Each of these kernels communicate with the notebook web application and web browser using a JSON over ZeroMQ/WebSockets message protocol.
When you start a new notebook server with ipython notebook
, your
browser should open into the Dashboard, a page listing all notebooks
available in the current directory, which allow to create new
notebooks. In this page, you can also drag and drop existing .py
files
over the file list to import them as notebooks (see the manual for
further details on how these files are
interpreted).
Once you open an existing notebook (like this one) or create a new one, you are in the main notebook interface, which consists of a main editing area (where these cells are contained) as well as a menu and permanent header area at the top, and a pager that rises from the bottom when needed and can be collapsed again.
Here, you can move with the arrow keys or use the
scroll bars. The cursor enters code cells immediately, but only selects
text (markdown) cells without entering them; to enter a text cell,
use Enter
(or double-click), and Shift-Enter
to exit it again (just like executing a
code cell).
The menu bar contains all the commands you can use to manipulate the notebook.
Interrupt
stops the running computation while
Restart
fully kills the kernel and starts a fresh one. Obviously this means that all your previous variables are destroyed, but it also makes it easy to get a fresh kernel in which to re-execute a notebook, perhaps after changing an extension module for which Python's reload
mechanism does not work.
Ctrl-m h
(or click the QuickHelp
button atthe top) to show keybindings.
Go to Help -> Keyboard Shortcuts to see the complete shortcuts list
To access the help system symply type help()
in a code cell. Use quit
to finish:
help()
Welcome to Python 3.6's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.6/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt.
The header area at the top allows you to rename an existing notebook. This area also indicates whenever the kernel is busy executing code.
Whenever IPython needs to display additional information, such as when you type somefunction?
in a cell, the notebook
opens a pane at the bottom. You can keep this pager pane open for reference (it doesn't block input in the main area) or dismiss it by clicking on its divider bar. Try this:
print??
Whey you complete with the Tab
key, IPython shows a drop list with all available completions. If you type more characters while this list is open, IPython automatically refines the search; once there is only one option left you can hit Tab
once more (or Enter
) to complete. You can also select the completion you want with the arrow keys or the mouse, and then hit Enter
.
In addition, if you hit Tab
inside of open parentheses, IPython will search for the docstring of the last object left of the parens and will display it on a tooltip. For example, type list(<TAB>
and you will see the docstring for the builtin list
constructor. Moreover, pressing tab several time in a row allows you change the behaviour of the tooltip:
# Remove comment, then position your cursor after the ( and hit the Tab key:
#list(
You can enable inline plotting with %pylab inline
:
# NOTICE: In Jupyter and Python3 you can use UNICODE identifiers as variable names.
# Example: just type \alpha and press <TAB>
β = np.linspace(0, 3*np.pi, 700)
fig = bk.figure(title="A simple Chirp",
plot_width = 700, plot_height = 300)
fig.title.text_font_size="18pt"
fig.line(β, np.sin(β**2), line_width=2, line_color='magenta')
bk.show(fig)
IPython comes with basic widgets that represent common interactive controls.
import ipywidgets
from ipywidgets import interact
# Here "interact" is used as a decorator:
@interact(n=(2.0,30.0), lw=(0.5, 10.0, 0.5), c=(['blue', 'green', 'red']))
def edit_myplot(n=2.0, lw=1.0, c='blue'):
fig = bk.figure(title="An interactive Chirp",
plot_width = 700, plot_height = 300)
fig.title.text_font_size="18pt"
fig.line(β, np.sin(β**n), line_width=lw, line_color=c)
bk.show(fig)
return
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
from IPython.display import display
from sympy import Symbol, Eq, factor, init_printing
init_printing(use_latex='mathjax')
def factorit(n):
x = Symbol('x')
display(Eq(x**n-1, factor(x**n-1)))
interact(factorit, n=(2,60));
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
You can italicize, boldface or typewrite
text
and embed code meant for illustration instead of execution in Python:
def f(x):
"""a docstring"""
return x**2
or other languages:
if (i=0; i<n; i++) {
printf("hello %d\n", i);
x += 4;
}
Courtesy of MathJax, you can include mathematical expressions both inline: $e^{i\pi} + 1 = 0$ and displayed:
$$ {\partial{\bf u}\over{\partial t}} + ({\bf u} \cdot \nabla) {\bf u} = - {1\over\rho} \nabla p + \gamma\nabla^2{\bf u} + {1\over\rho}{\bf F} $$Note that we have an actual protocol for this, see the display_protocol
notebook for further details.
# Video credit: Fernando Pérez, Brian E. Granger, Min Ragan-Kelley
from IPython.display import YouTubeVideo
YouTubeVideo('Rc4JQWowG5I', width=800, height=420)
You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia page for mobile users:
from IPython.display import HTML
HTML('<iframe src="http://jupyter.org/" width=800 height=350>')
.py
in the dashboard%load
with any local or remote url: the Matplotlib Gallery!In this notebook we've kept the output saved so you can see the result, remove the comment #
and run the next cell (with an active internet connection).
%matplotlib inline
# %load http://matplotlib.org/mpl_examples/pie_and_polar_charts/polar_scatter_demo.py
"""
Demo of scatter plot on a polar axis.
Size increases radially in this example and color increases with angle (just to
verify the symbols are being scattered correctly).
"""
import numpy as np
import matplotlib.pyplot as plt
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2 * np.random.rand(N)
colors = theta
ax = plt.subplot(111, polar=True)
c = plt.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv)
c.set_alpha(0.75)
plt.show()
Visit www.add-for.com for more tutorials and updates.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.