Skip to content

Instantly share code, notes, and snippets.

@dpsanders
Created July 20, 2013 07:16
Show Gist options
  • Save dpsanders/6044164 to your computer and use it in GitHub Desktop.
Save dpsanders/6044164 to your computer and use it in GitHub Desktop.
Rendered Sphinx documentation for IPython Notebook.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The IPython Notebook &mdash; IPython 1.0.dev documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '1.0.dev',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="IPython 1.0.dev documentation" href="../index.html" />
<link rel="up" title="Using IPython for interactive work" href="index.html" />
<link rel="next" title="Using IPython for parallel computing" href="../parallel/index.html" />
<link rel="prev" title="A Qt Console for IPython" href="qtconsole.html" />
</head>
<body>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="../index.html"><img src="../_static/logo.png" border="0" alt="IPython Documentation"/></a>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../parallel/index.html" title="Using IPython for parallel computing"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="qtconsole.html" title="A Qt Console for IPython"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">home</a>|&nbsp;</li>
<li><a href="../search.html">search</a>|&nbsp;</li>
<li><a href="../index.html">documentation </a> &raquo;</li>
<li><a href="index.html" accesskey="U">Using IPython for interactive work</a> &raquo;</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">The IPython Notebook</a><ul>
<li><a class="reference internal" href="#basic-structure">Basic structure</a></li>
<li><a class="reference internal" href="#main-features-of-the-ipython-notebook-web-app">Main features of the IPython Notebook web app</a></li>
<li><a class="reference internal" href="#notebook-documents">Notebook documents</a></li>
<li><a class="reference internal" href="#starting-up-the-ipython-notebook-web-app">Starting up the IPython Notebook web app</a></li>
<li><a class="reference internal" href="#notebook-user-interface">Notebook user interface</a><ul>
<li><a class="reference internal" href="#notebook-title">Notebook title</a></li>
<li><a class="reference internal" href="#menu-bar">Menu bar</a></li>
<li><a class="reference internal" href="#toolbar">Toolbar</a></li>
</ul>
</li>
<li><a class="reference internal" href="#input-cells">Input cells</a></li>
<li><a class="reference internal" href="#basic-workflow">Basic workflow</a></li>
<li><a class="reference internal" href="#keyboard-shortcuts">Keyboard shortcuts</a></li>
<li><a class="reference internal" href="#input-cell-types">Input cell types</a><ul>
<li><a class="reference internal" href="#code-cells">Code cells</a></li>
<li><a class="reference internal" href="#rich-text-using-markdown">Rich text using Markdown</a></li>
<li><a class="reference internal" href="#raw-input-cells">Raw input cells</a></li>
<li><a class="reference internal" href="#heading-cells">Heading cells</a></li>
<li><a class="reference internal" href="#magic-commands">Magic commands</a></li>
<li><a class="reference internal" href="#line-magics">Line magics</a></li>
<li><a class="reference internal" href="#cell-magics">Cell magics</a></li>
</ul>
</li>
<li><a class="reference internal" href="#plotting">Plotting</a></li>
<li><a class="reference internal" href="#converting-notebooks-to-other-formats">Converting notebooks to other formats</a></li>
<li><a class="reference internal" href="#configuring-the-ipython-notebook">Configuring the IPython Notebook</a></li>
<li><a class="reference internal" href="#extracting-standard-python-files-from-notebooks">Extracting standard Python files from notebooks</a></li>
<li><a class="reference internal" href="#security">Security</a></li>
<li><a class="reference internal" href="#connecting-to-an-existing-kernel">Connecting to an existing kernel</a></li>
<li><a class="reference internal" href="#running-a-public-notebook-server">Running a public notebook server</a></li>
<li><a class="reference internal" href="#running-with-a-different-url-prefix">Running with a different URL prefix</a></li>
<li><a class="reference internal" href="#using-a-different-notebook-store">Using a different notebook store</a></li>
<li><a class="reference internal" href="#notebook-json-file-format">Notebook JSON file format</a></li>
<li><a class="reference internal" href="#known-issues">Known issues</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="qtconsole.html"
title="previous chapter">A Qt Console for IPython</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="../parallel/index.html"
title="next chapter">Using IPython for parallel computing</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/interactive/htmlnotebook.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="the-ipython-notebook">
<span id="htmlnotebook"></span><h1>The IPython Notebook<a class="headerlink" href="#the-ipython-notebook" title="Permalink to this headline">¶</a></h1>
<p>The IPython Notebook is part of the IPython package, which aims to provide a powerful, interactive approach to scientific computation.
The IPython Notebook extends the previous text-console-based approach, and the later Qt console, in a qualitatively new diretion, providing a web-based application suitable for capturing the whole scientific computation process.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../install/install.html#installnotebook"><em>Installation requirements</em></a> for the Notebook.</p>
</div>
<div class="section" id="basic-structure">
<h2>Basic structure<a class="headerlink" href="#basic-structure" title="Permalink to this headline">¶</a></h2>
<p>The IPython Notebook combines two components:</p>
<ul>
<li><p class="first"><strong>The IPython Notebook web application</strong>:</p>
<p>The IPython Notebook web app is a browser-based tool for interactive authoring of literate computations, in which explanatory text, mathematics,computations and rich media output may be combined. Input and output are stored in persistent cells that may be edited in-place.</p>
</li>
<li><p class="first"><strong>Notebook documents</strong>:</p>
<p><em>Notebook documents</em>, or <em>notebooks</em>, are plain text documents which record all inputs and outputs of the computations, interspersed with text, mathematics and HTML 5 representations of objects, in a literate style.</p>
</li>
</ul>
<p>Since the similarity in names can lead to some confusion, in the documentation we will use capitalization of the word &#8220;notebook&#8221; to distinguish the <a href="#id1"><span class="problematic" id="id2">*</span></a>N*otebook app and <a href="#id3"><span class="problematic" id="id4">*</span></a>n*otebook documents, thinking of the Notebook app as being a proper noun. We will also always refer to the &#8220;Notebook app&#8221; when we are referring to the browser-based interface, and usually to &#8220;notebook documents&#8221;, instead of &#8220;notebooks&#8221;, for added precision.</p>
<p>We refer to the current state of the computational process taking place in the Notebook app, i.e. the (numbered) sequence of input and output cells, as the
<em>notebook space</em>. Notebook documents provide an <em>exact</em>, <em>one-to-one</em> record of all the content in the notebook space, as a plain text file in JSON format. The Notebook app automatically saves, at certain intervals, the contents of the notebook space to a notebook document stored on disk, with the same name as the title of the notebook space, and the file extension <tt class="docutils literal"><span class="pre">.ipynb</span></tt>. For this reason, there is no confusion about using the same word &#8220;notebook&#8221; for both the notebook space and the corresonding notebook document, since they are really one and the same concept (we could say that they are &#8220;isomorphic&#8221;).</p>
</div>
<div class="section" id="main-features-of-the-ipython-notebook-web-app">
<h2>Main features of the IPython Notebook web app<a class="headerlink" href="#main-features-of-the-ipython-notebook-web-app" title="Permalink to this headline">¶</a></h2>
<p>The main features of the IPython Notebook app include:</p>
<ul class="simple">
<li>In-browser editing for code, with automatic syntax highlighting and indentation and tab completion/introspection.</li>
<li>Literate combination of code with rich text using the Markdown markup language.</li>
<li>Mathematics is easily included within the Markdown using LaTeX notation, and rendered natively by MathJax.</li>
<li>Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result of computations.</li>
<li>Publication-quality figures in a range of formats (SVG / PNG), rendered by the <tt class="docutils literal"><span class="pre">matplotlib</span></tt> library, may be included inline and exported.</li>
</ul>
</div>
<div class="section" id="notebook-documents">
<h2>Notebook documents<a class="headerlink" href="#notebook-documents" title="Permalink to this headline">¶</a></h2>
<p>Notebook document files are just standard text files with the extension
<tt class="docutils literal"><span class="pre">.ipynb</span></tt>, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues.</p>
<p>Despite the fact that the notebook documents are plain text files, they use
the JSON format in order to store a <em>complete</em>, <em>reproducible</em>, <em>one-to-one</em> copy of the state of the computational state as it is inside the Notebook app.
All computations carried out, and the corresponding results obtained, can be
combined in a literate way, mixing them with descriptive text, mathematics,
and HTML 5 representations of objects.</p>
<p>Notebooks may easily be exported to a range of static formats, including
HTML (for example, for blog posts), PDF and slide shows.
Furthermore, any publicly available notebook may be shared via the
<a class="reference external" href="http://nbviewer.ipython.org">IPython Notebook Viewer</a> service, which will
provide it as a static web page. The results may thus be shared without having to install anything.</p>
<p>See the <a class="reference internal" href="../install/index.html#install-index"><em>installation documentation</em></a> for directions on
how to install the notebook and its dependencies.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You can start more than one notebook server at the same time, if you want to
work on notebooks in different directories. By default the first notebook
server starts on port 8888, and later notebook servers search for ports near
that one. You can also manually specify the port with the <tt class="docutils literal"><span class="pre">--port</span></tt>
option.</p>
</div>
</div>
<div class="section" id="starting-up-the-ipython-notebook-web-app">
<h2>Starting up the IPython Notebook web app<a class="headerlink" href="#starting-up-the-ipython-notebook-web-app" title="Permalink to this headline">¶</a></h2>
<p>You can start running the Notebook web app using the following command:</p>
<div class="highlight-python"><pre>$ ipython notebook</pre>
</div>
<p>The landing page of the notebook server application, the <em>dashboard</em>, shows the notebooks currently available in the <em>working directory</em> (the directory from which the notebook was started).
You can create new notebooks from the dashboard with the <tt class="docutils literal"><span class="pre">New</span> <span class="pre">Notebook</span></tt>
button, or open existing ones by clicking on their name.
You can also drag and drop <tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebooks and standard <tt class="docutils literal"><span class="pre">.py</span></tt> Python source code files into the notebook list area.</p>
<p><tt class="docutils literal"><span class="pre">.py</span></tt> files will be imported into the IPython Notebook as a notebook with the same name, but an <tt class="docutils literal"><span class="pre">.ipynb</span></tt> extension, located in the working directory. The notebook created will have just one cell, which will contain all the
code in the <tt class="docutils literal"><span class="pre">.py</span></tt> file. You can later manually partition this into individual cells using the <tt class="docutils literal"><span class="pre">Edit</span> <span class="pre">|</span> <span class="pre">Split</span> <span class="pre">Cell</span></tt> menu option, or the <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">-</span></tt> keyboard shortcut.</p>
<p>When you open or create a new notebook, your browser tab will reflect the name of that notebook, prefixed with &#8220;IPy&#8221;.
The URL is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change in a future version of IPython.</p>
<p>The IPython Notebook web app is based on a server-client structure.
This server uses a two-process kernel architecture based on ZeroMQ, as well as Tornado for serving HTTP requests. Other clients may connect to the same underlying IPython kernel; see below.</p>
</div>
<div class="section" id="notebook-user-interface">
<h2>Notebook user interface<a class="headerlink" href="#notebook-user-interface" title="Permalink to this headline">¶</a></h2>
<p>When you open a new notebook document in the Notebook, you will be presented with the title associated to the notebook space/document, a <em>menu bar</em>, a <em>toolbar</em> and an empty <em>input cell</em>.</p>
<div class="section" id="notebook-title">
<h3>Notebook title<a class="headerlink" href="#notebook-title" title="Permalink to this headline">¶</a></h3>
<p>The title of the notebook document that is currently being edited is displayed at the top of the page, next to the <tt class="docutils literal"><span class="pre">IP[y]:</span> <span class="pre">Notebook</span></tt> logo. This title may be edited directly by clicking on it. The title is reflected in the name of the <tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebook document file that is saved.</p>
</div>
<div class="section" id="menu-bar">
<h3>Menu bar<a class="headerlink" href="#menu-bar" title="Permalink to this headline">¶</a></h3>
<p>The menu bar presents different options that may be used to manipulate the way the Notebook functions.</p>
</div>
<div class="section" id="toolbar">
<h3>Toolbar<a class="headerlink" href="#toolbar" title="Permalink to this headline">¶</a></h3>
<p>The tool bar gives a quick way of accessing the most-used operations within the Notebook, by clicking on an icon.</p>
</div>
</div>
<div class="section" id="input-cells">
<h2>Input cells<a class="headerlink" href="#input-cells" title="Permalink to this headline">¶</a></h2>
<p>Input cells are at the core of the functionality of the IPython Notebook.
They are regions in the document in which you can enter different types of text and commands. To <em>execute</em> or <em>run</em> the <em>current cell</em>, i.e. the cell under the cursor, you can use the <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt> key combination.
This tells the Notebook app to perform the relevant operation for each type of cell (see below), and then to display the resulting output.</p>
<p>The notebook consists of a sequence of input cells, labelled <tt class="docutils literal"><span class="pre">In[n]</span></tt>, which may be executed in a non-linear way, and outputs <tt class="docutils literal"><span class="pre">Out[n]</span></tt>, where <tt class="docutils literal"><span class="pre">n</span></tt> is a number which denotes the order in which the cells were executed over the history of the computational process. The contents of all of these cells are accessible as Python variables with the same names, forming a complete record of the history of the computation.</p>
</div>
<div class="section" id="basic-workflow">
<h2>Basic workflow<a class="headerlink" href="#basic-workflow" title="Permalink to this headline">¶</a></h2>
<p>The normal workflow in a notebook is, then, quite similar to a standard IPython session, with the difference that you can edit cells in-place multiple
times until you obtain the desired results, rather than having to
rerun separate scripts with the <tt class="docutils literal"><span class="pre">%run</span></tt> magic command. (Magic commands do, however, also work in the notebook; see below).</p>
<p>Typically, you will work on a computational problem in pieces, organizing related ideas into cells and moving forward once previous parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be executed together, as was previously necessary, especially if parts of them take a long time to run</p>
<p>The only significant limitation that the Notebook currently has, compared to the Qt console, is that it cannot run any code that expects input from the kernel (such as scripts that call <tt class="xref py py-func docutils literal"><span class="pre">raw_input()</span></tt>). Very importantly, this means that the <tt class="docutils literal"><span class="pre">%debug</span></tt> magic does <em>not</em> currently work in the notebook!</p>
<p>This limitation will be overcome in the future, but in the meantime, there is a simple solution for debugging: you can attach a Qt console to your existing notebook kernel, and run <tt class="docutils literal"><span class="pre">%debug</span></tt> from the Qt console.
If your notebook is running on a local computer (i.e. if you are accessing it via your localhost address at <tt class="docutils literal"><span class="pre">127.0.0.1</span></tt>), then you can just type
<tt class="docutils literal"><span class="pre">%qtconsole</span></tt> in the notebook and a Qt console will open up, connected to that same kernel.</p>
<p>At certain moments, it may be necessary to interrupt a particularly long calculation, or even to kill the entire computational process. This may be achieved by interrupting or restarting the kernel, respectively.
After a kernel restart, it as if the notebook were loaded afresh.</p>
<p>A notebook may be downloaded in either <tt class="docutils literal"><span class="pre">.ipynb</span></tt> or raw <tt class="docutils literal"><span class="pre">.py</span></tt> form from the menu option <tt class="docutils literal"><span class="pre">File</span> <span class="pre">|</span> <span class="pre">Download</span> <span class="pre">as</span></tt>.
Choosing the <tt class="docutils literal"><span class="pre">.py</span></tt> option downloads a Python <tt class="docutils literal"><span class="pre">.py</span></tt> script, in which all output has been removed and the content of Markdown cells
in comment areas. See ref:<cite>below &lt;notebook_format&gt;</cite> for more details on the
notebook format.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">While in simple cases you can &#8220;roundtrip&#8221; a notebook to Python, edit the
Python file, and then import it back without loss of main content, this is in general <em>not guaranteed to work</em>. First, there is extra metadata
saved in the notebook that may not be saved to the <tt class="docutils literal"><span class="pre">.py</span></tt> format. And as
the notebook format evolves in complexity, there will be attributes of the
notebook that will not survive a roundtrip through the Python form. You
should think of the Python format as a way to output a script version of a
notebook and the import capabilities as a way to load existing code to get a
notebook started. But the Python version is <em>not</em> an alternate notebook
format.</p>
</div>
</div>
<div class="section" id="keyboard-shortcuts">
<h2>Keyboard shortcuts<a class="headerlink" href="#keyboard-shortcuts" title="Permalink to this headline">¶</a></h2>
<p>All actions in the notebook can be achieved with the mouse, but
keyboard shortcuts are also available for the most common ones, so that productive use of the notebook can be achieved with minimal mouse usage. The main shortcuts to remember are the following:</p>
<ul>
<li><dl class="first docutils">
<dt><tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt>:</dt>
<dd><p class="first last">Execute the current cell, show output (if any), and jump to the next cell below. If <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt> is invoked on the last input cell, a new code cell will also be created. Note that in the notebook, typing <tt class="kbd docutils literal"><span class="pre">Enter</span></tt> on its own <em>never</em> forces execution, but rather just inserts a new line in the current input cell. In the Notebook it is thus always necessary to use <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt> to execute the cell (or use the <tt class="docutils literal"><span class="pre">Cell</span> <span class="pre">|</span> <span class="pre">Run</span></tt> menu item).</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="kbd docutils literal"><span class="pre">Ctrl-Enter</span></tt>:</dt>
<dd><p class="first last">Execute the current cell as if it were in &#8220;terminal mode&#8221;, where any output is shown, but the cursor <em>remains</em> in the current cell. This is convenient for doing quick experiments in place, or for querying things like filesystem content, without needing to create additional cells that you may not want to be saved in the notebook.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="kbd docutils literal"><span class="pre">Alt-Enter</span></tt>:</dt>
<dd><p class="first last">Executes the current cell, shows the output, and inserts a <em>new</em> input cell between the current cell and the adjacent cell (if one exists). This is thus a shortcut for the sequence <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt>, <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">a</span></tt>.
(<tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">a</span></tt> adds a new cell below the current one.)</p>
</dd>
</dl>
</li>
<li><p class="first"><tt class="kbd docutils literal"><span class="pre">Ctrl-m</span></tt>:
This is the prefix for <em>all</em> other shortcuts, which consist of <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span></tt> followed by a single letter or character. For example, if you type <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">h</span></tt> (that is, the sole letter <tt class="kbd docutils literal"><span class="pre">h</span></tt> after <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span></tt>), IPython will show you all the available keyboard shortcuts.</p>
</li>
</ul>
</div>
<div class="section" id="input-cell-types">
<h2>Input cell types<a class="headerlink" href="#input-cell-types" title="Permalink to this headline">¶</a></h2>
<p>Each IPython input cell has a <em>cell type</em>, of which there is a restricted number. The type of a cell may be set by using the cell type dropdown on the toolbar, or via the following keyboard shortcuts:</p>
<ul class="simple">
<li><strong>code</strong>: <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">y</span></tt></li>
<li><strong>markdown</strong>: <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">m</span></tt></li>
<li><strong>raw</strong>: <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">t</span></tt></li>
<li><strong>heading</strong>: <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">1</span></tt> - <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">6</span></tt></li>
</ul>
<p>Upon initial creation, each input cell is by default a code cell.</p>
<div class="section" id="code-cells">
<h3>Code cells<a class="headerlink" href="#code-cells" title="Permalink to this headline">¶</a></h3>
<p>A <em>code input cell</em> allows you to edit code inline within the cell, with full syntax highlighting and autocompletion/introspection. By default, the language associated to a code cell is Python, but other languages, such as <tt class="docutils literal"><span class="pre">julia</span></tt> and <tt class="docutils literal"><span class="pre">R</span></tt>, can be handled using magic commands (see below).</p>
<p>When a code cell is executed with <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt>, the code that it contains is transparently exported and run in that language
(with automatic compiling, etc., if necessary). The result that is returned from this computation is then displayed in the notebook space as the cell&#8217;s
<em>output</em>. If this output is of a textual nature, then it is placed into a numbered <em>output cell</em>.
However, many other possible forms of output are also possible, including
<tt class="docutils literal"><span class="pre">matplotlib</span></tt> figures and HTML tables (as used, for example, in the
<tt class="docutils literal"><span class="pre">pandas</span></tt> data analyis package). This is known as IPython&#8217;s <em>rich display</em> capability.</p>
</div>
<div class="section" id="rich-text-using-markdown">
<h3>Rich text using Markdown<a class="headerlink" href="#rich-text-using-markdown" title="Permalink to this headline">¶</a></h3>
<p>You can document the computational process in a literate way, alternating descriptive text with code, using <em>rich text</em>. In IPython this is accomplished by marking up text with the Markdown language. The corresponding cells are called <em>Markdown input cells</em>. The Markdown language provides a simple way to perform this text markup, that is, to specify which parts of the text should be emphasized (italics), bold, form lists, etc.</p>
<p>When a Markdown input cell is executed, the Markdown code is converted into the corresponding formatted rich text. This output then <em>replaces</em> the original Markdown input cell, leaving just the visually-significant marked up rich text. Markdown allows arbitrary HTML code for formatting.</p>
<p>Within Markdown cells, you can also include <em>mathematics</em> in a straightforward way, using standard LaTeX notation: <tt class="docutils literal"><span class="pre">$...$</span></tt> for inline mathematics and <tt class="docutils literal"><span class="pre">$$...$$</span></tt> for displayed mathematics. Mathematics is rendered natively in the browser by MathJax. Standard LaTeX and AMS-math environments, such as
<tt class="docutils literal"><span class="pre">\begin{equation}...\end{equation}</span></tt>, and <tt class="docutils literal"><span class="pre">\begin{align}...\end{align}</span></tt> also work, and new LaTeX macros may be defined using standard LaTeX methods, such as <tt class="docutils literal"><span class="pre">\newcommand</span></tt>, by placing them anywhere in a Markdown cell.</p>
</div>
<div class="section" id="raw-input-cells">
<h3>Raw input cells<a class="headerlink" href="#raw-input-cells" title="Permalink to this headline">¶</a></h3>
<p><em>Raw</em> input cells provide a place in which you can put additional information which you do not want to evaluated by the Notebook. This can be used, for example, to include extra information that is needed when exporting to a certain format. The output after evaluating a raw cell is just a verbatim copy of the input.</p>
</div>
<div class="section" id="heading-cells">
<h3>Heading cells<a class="headerlink" href="#heading-cells" title="Permalink to this headline">¶</a></h3>
<p>You can provide a conceptual structure for your computational document as a whole using different levels of headings; there are 6 levels available, from level 1 (main title) down to level 6 (paragraph). These can be used later for constructing tables of contents, etc.</p>
<p>As with Markdown cells, a heading input cell is replaced by a rich text rendering of the heading when the cell is executed.</p>
</div>
<div class="section" id="magic-commands">
<h3>Magic commands<a class="headerlink" href="#magic-commands" title="Permalink to this headline">¶</a></h3>
<p>Magic commands, or <em>magics</em>, are commands for controlling IPython itself.
They all begin with <tt class="docutils literal"><span class="pre">%</span></tt> and are entered into code input cells; the code cells are executed as usual with <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt>.</p>
<p>The magic commands call special functions defined by IPython which manipulate the computational state in certain ways.</p>
<p>There are two types of magics:</p>
<blockquote>
<div><dl class="docutils">
<dt><strong>line magics</strong>:</dt>
<dd>These begin with a single <tt class="docutils literal"><span class="pre">%</span></tt> and take as arguments the rest of the <em>same line</em> of the code cell. Any other lines of the code cell are treated as if they were part of a standard code cell.</dd>
<dt><strong>cell magics</strong>:</dt>
<dd>These begin with <tt class="docutils literal"><span class="pre">%%</span></tt> and operate on the <em>entire</em> remaining contents of the code cell.</dd>
</dl>
</div></blockquote>
</div>
<div class="section" id="line-magics">
<h3>Line magics<a class="headerlink" href="#line-magics" title="Permalink to this headline">¶</a></h3>
<p>Some of the available line magics are the following:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">%load</span> <span class="pre">filename</span></tt>:
Loads the contents of the file <tt class="docutils literal"><span class="pre">filename</span></tt> into a new code cell.</li>
<li><tt class="docutils literal"><span class="pre">%timeit</span> <span class="pre">code</span></tt>:
A simple way to time how long the single line of code <tt class="docutils literal"><span class="pre">code</span></tt> takes to run</li>
<li><tt class="docutils literal"><span class="pre">%config</span></tt>:
Configuration of the IPython Notebook</li>
<li><tt class="docutils literal"><span class="pre">%lsmagic</span></tt>:
Provides a list of all available magic commands</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="cell-magics">
<h3>Cell magics<a class="headerlink" href="#cell-magics" title="Permalink to this headline">¶</a></h3>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">%%latex</span></tt>:
Renders the entire contents of the cell in LaTeX, without needing to use explicit LaTeX delimiters.</li>
<li><tt class="docutils literal"><span class="pre">%%bash</span></tt>:
The code cell is executed by sending it to be executed by <tt class="docutils literal"><span class="pre">bash</span></tt>. The output of the <tt class="docutils literal"><span class="pre">bash</span></tt> commands is captured and displayed in the notebook.</li>
<li><tt class="docutils literal"><span class="pre">%%file</span> <span class="pre">filename</span></tt>:
Writes the contents of the cell to the file <tt class="docutils literal"><span class="pre">filename</span></tt>.
<strong>Caution</strong>: The file is over-written without warning!</li>
<li><tt class="docutils literal"><span class="pre">%%R</span></tt>:
Execute the contents of the cell using the R language.</li>
</ul>
</div></blockquote>
<p>Several of the cell magics provide functionality to manipulate the filesystem of a remote server to which you otherwise do not have access.</p>
</div>
</div>
<div class="section" id="plotting">
<h2>Plotting<a class="headerlink" href="#plotting" title="Permalink to this headline">¶</a></h2>
<p>One major feature of the Notebook is the ability to capture the result of plots as <em>inline</em> output, thus displaying the result of running some code right next to the code itself. IPython is designed to work seamlessly with the <tt class="docutils literal"><span class="pre">matplotlib</span></tt> plotting library to attain this functionality.</p>
<p>To set this up, before any plotting is performed you must execute the
<tt class="docutils literal"><span class="pre">%matplotlib</span></tt> magic command. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with <tt class="docutils literal"><span class="pre">matplotlib</span></tt>; it does
<em>not</em>, however, actually execute any Python <tt class="docutils literal"><span class="pre">import</span></tt> commands, that is, no names are added to the namespace.</p>
<p>For more agile <em>interactive</em> use of the notebook space, an alternative magic, <tt class="docutils literal"><span class="pre">%pylab</span></tt>, is provided. This does the same work as the <tt class="docutils literal"><span class="pre">%matplotlib</span></tt> magic, but <em>in addition</em> it automatically executes a standard sequence of <tt class="docutils literal"><span class="pre">import</span></tt> statements required to work with the <tt class="docutils literal"><span class="pre">%matplotlib</span></tt> library:</p>
<p>It will import at the top level <cite>numpy</cite> as <cite>np</cite>, <cite>pyplot</cite> as <cite>plt</cite>, <cite>matplotlib</cite>, <cite>pylab</cite> and <cite>mlab</cite> from <cite>matplotlib</cite>, as well as <em>all names</em> from <tt class="docutils literal"><span class="pre">numpy</span></tt> and <tt class="docutils literal"><span class="pre">pylab</span></tt>. A less invasive, but less interactive, option is <tt class="docutils literal"><span class="pre">%pylab</span> <span class="pre">--no-import-all</span></tt>, which does not do these <tt class="docutils literal"><span class="pre">import</span> <span class="pre">*</span></tt> imports.</p>
<p>When the default <tt class="docutils literal"><span class="pre">%matplotlib</span></tt> or <tt class="docutils literal"><span class="pre">%pylab</span></tt> magics are used, the output of a plotting command is captured in a <em>separate</em> window. An alternative is to use:</p>
<div class="highlight-python"><pre>%matplotlib inline</pre>
</div>
<p>or:</p>
<div class="highlight-python"><pre>%pylab inline</pre>
</div>
<p>These instead capture the output of the plot and display it <em>inline</em> within the notebook format, below the input cell that produced it. The resulting plots will then also be stored in the notebook document.</p>
</div>
<div class="section" id="converting-notebooks-to-other-formats">
<h2>Converting notebooks to other formats<a class="headerlink" href="#converting-notebooks-to-other-formats" title="Permalink to this headline">¶</a></h2>
<p>Newly added in the 1.0 release of IPython is the <tt class="docutils literal"><span class="pre">nbconvert</span></tt> tool, which allows you to convert an <tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebook document file into another static format.</p>
<p>Currently, only a command line tool is provided; in the future, it will also be possible to export from within the Notebook app. The command line syntax is:</p>
<div class="highlight-python"><pre>$ ipython nbconvert --format=FORMAT notebook.ipynb</pre>
</div>
<p>This will convert the IPython document file <tt class="docutils literal"><span class="pre">notebook.ipynb</span></tt> into the output format given by the <tt class="docutils literal"><span class="pre">FORMAT</span></tt> string.</p>
<p>The default output format is HTML, for which the <cite>&#8211;format`</cite> modifier is not actually required:</p>
<div class="highlight-python"><pre>$ ipython nbconvert notebook.ipynb</pre>
</div>
<p>The currently supported export formats are the following:</p>
<ul>
<li><p class="first">HTML:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">full_html</span></tt>:
Standard HTML</li>
<li><tt class="docutils literal"><span class="pre">simple_html</span></tt>:
Simplified HTML</li>
<li><tt class="docutils literal"><span class="pre">reveal</span></tt>:
HTML slideshow presentation for use with the <tt class="docutils literal"><span class="pre">reveal.js</span></tt> package</li>
</ul>
</li>
<li><p class="first">PDF:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">sphinx_howto</span></tt>:
The format for Sphinx HOWTOs; similar to an <tt class="docutils literal"><span class="pre">article</span></tt> in LaTeX</li>
<li><tt class="docutils literal"><span class="pre">sphinx_manual</span></tt>:
The format for Sphinx manuals; similar to a <cite>book`</cite> in LaTeX</li>
<li><tt class="docutils literal"><span class="pre">latex</span></tt>:
An article formatted completely using LaTeX</li>
</ul>
</li>
<li><p class="first">Markup:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">rst</span></tt>:
reStructuredText markup</li>
<li><tt class="docutils literal"><span class="pre">markdown</span></tt>:
Markdown markup</li>
</ul>
</li>
<li><p class="first">Python:</p>
<blockquote>
<div><p>Comments out all the non-Python code to produce a standard Python <tt class="docutils literal"><span class="pre">.py</span></tt> script with just the code content.</p>
</div></blockquote>
</li>
</ul>
<p>The files output by <tt class="docutils literal"><span class="pre">nbconvert`</span> <span class="pre">are</span> <span class="pre">currently</span> <span class="pre">placed</span> <span class="pre">in</span> <span class="pre">a</span> <span class="pre">new</span> <span class="pre">subdirectory</span> <span class="pre">called</span> <span class="pre">``nbconvert_build</span></tt>.</p>
<p>Each of the options for PDF export produces as an intermediate step a root LaTeX <tt class="docutils literal"><span class="pre">.tex</span></tt> file with the same name as the notebook, as well as individual files for each figure, and <tt class="docutils literal"><span class="pre">.text`</span> <span class="pre">files</span> <span class="pre">with</span> <span class="pre">textual</span> <span class="pre">output</span> <span class="pre">from</span> <span class="pre">running</span> <span class="pre">code</span> <span class="pre">cells;</span> <span class="pre">all</span> <span class="pre">of</span> <span class="pre">these</span> <span class="pre">files</span> <span class="pre">are</span> <span class="pre">located</span> <span class="pre">together</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">``nbconvert_build</span></tt> subdirectory.</p>
<p>To actually produce the final PDF file, run the following commands:</p>
<div class="highlight-python"><pre>$ cd nbconvert_build
$ pdflatex notebook</pre>
</div>
<p>This requires a local installation of LaTeX on your machine;
it outputs the PDF file <tt class="docutils literal"><span class="pre">notebook.pdf</span></tt>, also inside the
<tt class="docutils literal"><span class="pre">nbconvert_build</span></tt> subdirectory.</p>
<p>Alternatively, the output may be piped to standard output, <tt class="docutils literal"><span class="pre">stdout</span></tt>, with:</p>
<div class="highlight-python"><pre>$ ipython nbconvert mynotebook.ipynb --stdout</pre>
</div>
<p>Multiple notebooks can be specified from the command line:</p>
<div class="highlight-python"><pre>$ ipython nbconvert notebook*.ipynb
$ ipython nbconvert notebook1.ipynb notebook2.ipynb</pre>
</div>
<p>or via a list in a configuration file, say <tt class="docutils literal"><span class="pre">mycfg.py</span></tt>, containing the text:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span> <span class="o">=</span> <span class="n">get_config</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">NbConvertApp</span><span class="o">.</span><span class="n">notebooks</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;notebook1.ipynb&quot;</span><span class="p">,</span> <span class="s">&quot;notebook2.ipynb&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>and using the command:</p>
<div class="highlight-python"><pre>$ ipython nbconvert --config mycfg.py</pre>
</div>
</div>
<div class="section" id="configuring-the-ipython-notebook">
<h2>Configuring the IPython Notebook<a class="headerlink" href="#configuring-the-ipython-notebook" title="Permalink to this headline">¶</a></h2>
<p>The IPython Notebook can be run with a variety of command line arguments.
To see a list of available options enter:</p>
<div class="highlight-python"><pre>$ ipython notebook --help</pre>
</div>
<p>Defaults for these options can also be set by creating a file named
<tt class="docutils literal"><span class="pre">ipython_notebook_config.py</span></tt> in your IPython <em>profile folder</em>. The profile folder is a subfolder of your IPython directory; to find out where it is located, run:</p>
<div class="highlight-python"><pre>$ ipython locate</pre>
</div>
<p>To create a new set of default configuration files, with lots of information on available options, use:</p>
<div class="highlight-python"><pre>$ ipython profile create</pre>
</div>
</div>
<div class="section" id="extracting-standard-python-files-from-notebooks">
<h2>Extracting standard Python files from notebooks<a class="headerlink" href="#extracting-standard-python-files-from-notebooks" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebook document files are plain text files which store a representation in JSON format of the contents of a notebook space. As such, it is not a valid <tt class="docutils literal"><span class="pre">.py</span></tt> Python script, and so can be neither imported with
<tt class="docutils literal"><span class="pre">import</span></tt> in Python, nor run as a standard Python script.</p>
<p>To extract just the Python code from within a notebook document, one option is to use <tt class="docutils literal"><span class="pre">ipython</span> <span class="pre">nbconvert</span></tt>, as described above.
An alternative is to pass an argument to the IPython Notebook, from the moment when it is originally started, specifying that whenever it saves an <tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebook document, it should, at the same time, save the corresponding standard <tt class="docutils literal"><span class="pre">.py</span></tt> script. To do so, you can execute the following command:</p>
<div class="highlight-python"><pre>ipython notebook --script</pre>
</div>
<p>or you can set this option permanently in your configuration file with:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span> <span class="o">=</span> <span class="n">get_config</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookManager</span><span class="o">.</span><span class="n">save_script</span><span class="o">=</span><span class="bp">True</span>
</pre></div>
</div>
<p>The result is that standard <tt class="docutils literal"><span class="pre">.py</span></tt> files are also now generated, and so they can be <tt class="docutils literal"><span class="pre">%run</span></tt>, imported from regular IPython sessions or other notebooks, or executed at the command line, as usual. Since the raw code you have typed is exported you must avoid using syntax such as IPython magics and other IPython-specific extensions to the language for the files to be able to be successfully imported.</p>
<p>In normal Python practice, the standard way to differentiate importable code in a Python script from the &#8220;executable&#8221; part of a script is to use the following idiom at the start of the executable part of the code:</p>
<div class="highlight-python"><pre>if __name__ == '__main__'
# rest of the code...</pre>
</div>
<p>Since all cells in the notebook are run as top-level code, you will need to
similarly protect <em>all</em> cells that you do not want executed when other scripts
try to import your notebook. A convenient shortand for this is to define early
on:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">script</span> <span class="o">=</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span>
</pre></div>
</div>
<p>Then in any cell that you need to protect, use:</p>
<div class="highlight-python"><pre>if script:
# rest of the cell...</pre>
</div>
</div>
<div class="section" id="security">
<span id="notebook-security"></span><h2>Security<a class="headerlink" href="#security" title="Permalink to this headline">¶</a></h2>
<p>You can protect your Notebook server with a simple single password by
setting the <tt class="xref py py-attr docutils literal"><span class="pre">NotebookApp.password</span></tt> configurable. You can prepare a
hashed password using the function <a class="reference internal" href="../api/generated/IPython.lib.security.html#IPython.lib.security.passwd" title="IPython.lib.security.passwd"><tt class="xref py py-func docutils literal"><span class="pre">IPython.lib.security.passwd()</span></tt></a>:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="kn">from</span> <span class="nn">IPython.lib</span> <span class="kn">import</span> <span class="n">passwd</span>
<span class="gp">In [2]: </span><span class="n">passwd</span><span class="p">()</span>
<span class="go">Enter password:</span>
<span class="go">Verify password:</span>
<span class="gr">Out[2]: </span><span class="s">&#39;sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference internal" href="../api/generated/IPython.lib.security.html#IPython.lib.security.passwd" title="IPython.lib.security.passwd"><tt class="xref py py-func docutils literal"><span class="pre">passwd()</span></tt></a> can also take the password as a string
argument. <strong>Do not</strong> pass it as an argument inside an IPython session, as it
will be saved in your input history.</p>
</div>
<p>You can then add this to your <tt class="file docutils literal"><span class="pre">ipython_notebook_config.py</span></tt>, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Password to use for web authentication</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">get_config</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">password</span> <span class="o">=</span> <span class="s">u&#39;sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed&#39;</span>
</pre></div>
</div>
<p>When using a password, it is a good idea to also use SSL, so that your password
is not sent unencrypted by your browser. You can start the notebook to
communicate via a secure protocol mode using a self-signed certificate with the command:</p>
<div class="highlight-python"><pre>$ ipython notebook --certfile=mycert.pem</pre>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>A self-signed certificate can be generated with <tt class="docutils literal"><span class="pre">openssl</span></tt>. For example, the following command will create a certificate valid for 365 days with both the key and certificate data written to the same file:</p>
<div class="last highlight-python"><pre>$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem</pre>
</div>
</div>
<p>Your browser will warn you of a dangerous certificate because it is
self-signed. If you want to have a fully compliant certificate that will not
raise warnings, it is possible (but rather involved) to obtain one,
<a class="reference external" href="http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars">as explained in detailed in this tutorial</a>.</p>
<p>Keep in mind that when you enable SSL support, you will need to access the
notebook server over <tt class="docutils literal"><span class="pre">https://</span></tt>, not over plain <tt class="docutils literal"><span class="pre">http://</span></tt>. The startup
message from the server prints this, but it is easy to overlook and think the
server is for some reason non-responsive.</p>
</div>
<div class="section" id="connecting-to-an-existing-kernel">
<h2>Connecting to an existing kernel<a class="headerlink" href="#connecting-to-an-existing-kernel" title="Permalink to this headline">¶</a></h2>
<p>The notebook server always prints to the terminal the full details of
how to connect to each kernel, with messages such as the following:</p>
<div class="highlight-python"><pre>[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json</pre>
</div>
<p>This long string is the name of a JSON file that contains all the port and
validation information necessary to connect to the kernel. You can then, for example, manually start a Qt console connected to the <em>same</em> kernel with:</p>
<div class="highlight-python"><pre>$ ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json</pre>
</div>
<p>If you have only a single kernel running, simply typing:</p>
<div class="highlight-python"><pre>$ ipython qtconsole --existing</pre>
</div>
<p>will automatically find it. (It will always find the most recently
started kernel if there is more than one.) You can also request this
connection data by typing <tt class="docutils literal"><span class="pre">%connect_info</span></tt>; this will print the same
file information as well as the content of the JSON data structure it contains.</p>
</div>
<div class="section" id="running-a-public-notebook-server">
<h2>Running a public notebook server<a class="headerlink" href="#running-a-public-notebook-server" title="Permalink to this headline">¶</a></h2>
<p>If you want to access your notebook server remotely via a web browser,
you can do the following.</p>
<p>Start by creating a certificate file and a hashed password, as explained above. Then create a custom profile for the notebook, with the following command line, type:</p>
<div class="highlight-python"><pre>$ ipython profile create nbserver</pre>
</div>
<p>In the profile directory just created, edit the file <tt class="docutils literal"><span class="pre">ipython_notebook_config.py</span></tt>. By default, the file has all fields commented; the minimum set you need to uncomment and edit is the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span> <span class="o">=</span> <span class="n">get_config</span><span class="p">()</span>
<span class="c"># Kernel config</span>
<span class="n">c</span><span class="o">.</span><span class="n">IPKernelApp</span><span class="o">.</span><span class="n">pylab</span> <span class="o">=</span> <span class="s">&#39;inline&#39;</span> <span class="c"># if you want plotting support always</span>
<span class="c"># Notebook config</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">certfile</span> <span class="o">=</span> <span class="s">u&#39;/absolute/path/to/your/certificate/mycert.pem&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">ip</span> <span class="o">=</span> <span class="s">&#39;*&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">open_browser</span> <span class="o">=</span> <span class="bp">False</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">password</span> <span class="o">=</span> <span class="s">u&#39;sha1:bcd259ccf...[your hashed password here]&#39;</span>
<span class="c"># It is a good idea to put it on a known, fixed port</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="mi">9999</span>
</pre></div>
</div>
<p>You can then start the notebook and access it later by pointing your browser to
<tt class="docutils literal"><span class="pre">https://your.host.com:9999</span></tt> with <tt class="docutils literal"><span class="pre">ipython</span> <span class="pre">notebook</span> <span class="pre">--profile=nbserver</span></tt>.</p>
</div>
<div class="section" id="running-with-a-different-url-prefix">
<h2>Running with a different URL prefix<a class="headerlink" href="#running-with-a-different-url-prefix" title="Permalink to this headline">¶</a></h2>
<p>The notebook dashboard (the landing page with an overview
of the notebooks in your working directory) typically lives at the URL
<tt class="docutils literal"><span class="pre">http://localhost:8888/</span></tt>. If you prefer that it lives, together with the rest of the notebook, under a sub-directory,
e.g. <tt class="docutils literal"><span class="pre">http://localhost:8888/ipython/</span></tt>, you can do so with
configuration options like the following (see above for instructions about
modifying <tt class="docutils literal"><span class="pre">ipython_notebook_config.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">base_project_url</span> <span class="o">=</span> <span class="s">&#39;/ipython/&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">base_kernel_url</span> <span class="o">=</span> <span class="s">&#39;/ipython/&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">webapp_settings</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;static_url_prefix&#39;</span><span class="p">:</span><span class="s">&#39;/ipython/static/&#39;</span><span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="using-a-different-notebook-store">
<h2>Using a different notebook store<a class="headerlink" href="#using-a-different-notebook-store" title="Permalink to this headline">¶</a></h2>
<p>By default, the Notebook app stores the notebook documents that it saves as files in the working directory of the Notebook app, also known as the <tt class="docutils literal"><span class="pre">notebook_dir</span></tt>. This logic is implemented in the <tt class="xref py py-class docutils literal"><span class="pre">FileNotebookManager</span></tt> class. However, the server can be configured to use a different notebook manager class, which can
store the notebooks in a different format.</p>
<p>Currently, we ship a <tt class="xref py py-class docutils literal"><span class="pre">AzureNotebookManager</span></tt> class that stores notebooks in Azure blob storage. This can be used by adding the following lines to your
<tt class="docutils literal"><span class="pre">ipython_notebook_config.py</span></tt> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span><span class="o">.</span><span class="n">NotebookApp</span><span class="o">.</span><span class="n">notebook_manager_class</span> <span class="o">=</span> <span class="s">&#39;IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">AzureNotebookManager</span><span class="o">.</span><span class="n">account_name</span> <span class="o">=</span> <span class="s">u&#39;paste_your_account_name_here&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">AzureNotebookManager</span><span class="o">.</span><span class="n">account_key</span> <span class="o">=</span> <span class="s">u&#39;paste_your_account_key_here&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">AzureNotebookManager</span><span class="o">.</span><span class="n">container</span> <span class="o">=</span> <span class="s">u&#39;notebooks&#39;</span>
</pre></div>
</div>
<p>In addition to providing your Azure Blob Storage account name and key, you will
have to provide a container name; you can use multiple containers to organize
your notebooks.</p>
</div>
<div class="section" id="notebook-json-file-format">
<span id="notebook-format"></span><h2>Notebook JSON file format<a class="headerlink" href="#notebook-json-file-format" title="Permalink to this headline">¶</a></h2>
<p>Notebook documents are JSON files with an <tt class="docutils literal"><span class="pre">.ipynb</span></tt> extension, formatted
as legibly as possible with minimal extra indentation and cell content broken
across lines to make them reasonably friendly to use in version-control
workflows. You should be very careful if you ever manually edit this JSON
data, as it is extremely easy to corrupt its internal structure and make the
file impossible to load. In general, you should consider the notebook as a
file meant only to be edited by the IPython Notebook app itself, not for hand-editing.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Binary data such as figures are also saved directly in the JSON file. This provides convenient single-file portability, but means that the files can be large; a <tt class="docutils literal"><span class="pre">diff</span></tt> of binary data is also not very meaningful. Since the binary blobs are encoded in a single line, they affect only one line of the <tt class="docutils literal"><span class="pre">diff</span></tt> output, but they are typically very long lines. You can use the <tt class="docutils literal"><span class="pre">Cell</span> <span class="pre">|</span> <span class="pre">All</span> <span class="pre">Output</span> <span class="pre">|</span> <span class="pre">Clear</span></tt> menu option to remove all output from a notebook prior to committing it to version control, if this is a concern.</p>
</div>
<p>The notebook server can also generate a pure Python version of your notebook,
using the <tt class="docutils literal"><span class="pre">File</span> <span class="pre">|</span> <span class="pre">Download</span> <span class="pre">as</span></tt> menu option. The resulting <tt class="docutils literal"><span class="pre">.py</span></tt> file will
contain all the code cells from your notebook verbatim, and all Markdown cells
prepended with a comment marker. The separation between code and Markdown
cells is indicated with special comments and there is a header indicating the
format version. All output is removed when exporting to Python.</p>
<p>As an example, consider a simple notebook called <tt class="docutils literal"><span class="pre">simple.ipynb</span></tt> which contains one Markdown cell, with the content <tt class="docutils literal"><span class="pre">The</span> <span class="pre">simplest</span> <span class="pre">notebook.</span></tt>, one code input cell with the content <tt class="docutils literal"><span class="pre">print</span> <span class="pre">&quot;Hello,</span> <span class="pre">IPython!&quot;</span></tt>, and the corresponding output.</p>
<p>The contents of the notebook document <tt class="docutils literal"><span class="pre">simple.ipynb</span></tt> is the following JSON container:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span>
<span class="s">&quot;metadata&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="s">&quot;simple&quot;</span>
<span class="p">},</span>
<span class="s">&quot;nbformat&quot;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
<span class="s">&quot;nbformat_minor&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;worksheets&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s">&quot;cells&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s">&quot;cell_type&quot;</span><span class="p">:</span> <span class="s">&quot;markdown&quot;</span><span class="p">,</span>
<span class="s">&quot;metadata&quot;</span><span class="p">:</span> <span class="p">{},</span>
<span class="s">&quot;source&quot;</span><span class="p">:</span> <span class="s">&quot;The simplest notebook.&quot;</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="s">&quot;cell_type&quot;</span><span class="p">:</span> <span class="s">&quot;code&quot;</span><span class="p">,</span>
<span class="s">&quot;collapsed&quot;</span><span class="p">:</span> <span class="n">false</span><span class="p">,</span>
<span class="s">&quot;input&quot;</span><span class="p">:</span> <span class="s">&quot;print </span><span class="se">\&quot;</span><span class="s">Hello, IPython</span><span class="se">\&quot;</span><span class="s">&quot;</span><span class="p">,</span>
<span class="s">&quot;language&quot;</span><span class="p">:</span> <span class="s">&quot;python&quot;</span><span class="p">,</span>
<span class="s">&quot;metadata&quot;</span><span class="p">:</span> <span class="p">{},</span>
<span class="s">&quot;outputs&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s">&quot;output_type&quot;</span><span class="p">:</span> <span class="s">&quot;stream&quot;</span><span class="p">,</span>
<span class="s">&quot;stream&quot;</span><span class="p">:</span> <span class="s">&quot;stdout&quot;</span><span class="p">,</span>
<span class="s">&quot;text&quot;</span><span class="p">:</span> <span class="s">&quot;Hello, IPython</span><span class="se">\n</span><span class="s">&quot;</span>
<span class="p">}</span>
<span class="p">],</span>
<span class="s">&quot;prompt_number&quot;</span><span class="p">:</span> <span class="mi">1</span>
<span class="p">}</span>
<span class="p">],</span>
<span class="s">&quot;metadata&quot;</span><span class="p">:</span> <span class="p">{}</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The corresponding Python script is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># -*- coding: utf-8 -*-</span>
<span class="c"># &lt;nbformat&gt;3.0&lt;/nbformat&gt;</span>
<span class="c"># &lt;markdowncell&gt;</span>
<span class="c"># The simplest notebook.</span>
<span class="c"># &lt;codecell&gt;</span>
<span class="k">print</span> <span class="s">&quot;Hello, IPython&quot;</span>
</pre></div>
</div>
<p>Note that indeed the output of the code cell, which is present in the JSON container, has been removed in the <tt class="docutils literal"><span class="pre">.py</span></tt> script.</p>
</div>
<div class="section" id="known-issues">
<h2>Known issues<a class="headerlink" href="#known-issues" title="Permalink to this headline">¶</a></h2>
<p>When behind a proxy, especially if your system or browser is set to autodetect
the proxy, the Notebook app might fail to connect to the server&#8217;s websockets,
and present you with a warning at startup. In this case, you need to configure
your system not to use the proxy for the server&#8217;s address.</p>
<p>For example, in Firefox, go to the Preferences panel, Advanced section,
Network tab, click &#8216;Settings...&#8217;, and add the address of the notebook server
to the &#8216;No proxy for&#8217; field.</p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../parallel/index.html" title="Using IPython for parallel computing"
>next</a> |</li>
<li class="right" >
<a href="qtconsole.html" title="A Qt Console for IPython"
>previous</a> |</li>
<li><a href="../index.html">home</a>|&nbsp;</li>
<li><a href="../search.html">search</a>|&nbsp;</li>
<li><a href="../index.html">documentation </a> &raquo;</li>
<li><a href="index.html" >Using IPython for interactive work</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2008, The IPython Development Team.
Last updated on Jul 20, 2013.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment