<!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 — IPython 2.3.0 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: '2.3.0', 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 2.3.0 documentation" href="../index.html" /> <link rel="up" title="The IPython notebook" href="index.html" /> <link rel="next" title="Converting notebooks to other formats" href="nbconvert.html" /> <link rel="prev" title="The IPython notebook" href="index.html" /> </head> <body> <div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px"> <a href="http://ipython.org/"><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="nbconvert.html" title="Converting notebooks to other formats" accesskey="N">next</a> |</li> <li class="right" > <a href="index.html" title="The IPython notebook" accesskey="P">previous</a> |</li> <li><a href="http://ipython.org">home</a>| </li> <li><a href="../search.html">search</a>| </li> <li><a href="../index.html">documentation </a> »</li> <li><a href="index.html" accesskey="U">The IPython notebook</a> »</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="#introduction">Introduction</a><ul> <li><a class="reference internal" href="#main-features-of-the-web-application">Main features of the web application</a></li> <li><a class="reference internal" href="#notebook-documents">Notebook documents</a></li> </ul> </li> <li><a class="reference internal" href="#starting-the-notebook-server">Starting the notebook server</a><ul> <li><a class="reference internal" href="#creating-a-new-notebook-document">Creating a new notebook document</a></li> <li><a class="reference internal" href="#opening-notebooks">Opening notebooks</a></li> </ul> </li> <li><a class="reference internal" href="#notebook-user-interface">Notebook user interface</a></li> <li><a class="reference internal" href="#structure-of-a-notebook-document">Structure of a notebook document</a><ul> <li><a class="reference internal" href="#code-cells">Code cells</a></li> <li><a class="reference internal" href="#markdown-cells">Markdown cells</a></li> <li><a class="reference internal" href="#raw-cells">Raw cells</a></li> <li><a class="reference internal" href="#heading-cells">Heading cells</a></li> </ul> </li> <li><a class="reference internal" href="#basic-workflow">Basic workflow</a><ul> <li><a class="reference internal" href="#keyboard-shortcuts">Keyboard shortcuts</a></li> </ul> </li> <li><a class="reference internal" href="#plotting">Plotting</a></li> <li><a class="reference internal" href="#configuring-the-ipython-notebook">Configuring the IPython Notebook</a></li> <li><a class="reference internal" href="#signing-notebooks">Signing Notebooks</a></li> <li><a class="reference internal" href="#importing-py-files">Importing <tt class="docutils literal"><span class="pre">.py</span></tt> files</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="index.html" title="previous chapter">The IPython notebook</a></p> <h4>Next topic</h4> <p class="topless"><a href="nbconvert.html" title="next chapter">Converting notebooks to other formats</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/notebook/notebook.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> <div class="section" id="introduction"> <h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2> <p>The notebook extends the console-based approach to interactive computing in a qualitatively new direction, providing a web-based application suitable for capturing the whole computation process: developing, documenting, and executing code, as well as communicating the results. The IPython notebook combines two components:</p> <p><strong>A web application</strong>: a browser-based tool for interactive authoring of documents which combine explanatory text, mathematics, computations and their rich media output.</p> <p><strong>Notebook documents</strong>: a representation of all content visible in the web application, including inputs and outputs of the computations, explanatory text, mathematics, images, and rich media representations of objects.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last">See the <a class="reference internal" href="../install/install.html#installnotebook"><em>installation documentation</em></a> for directions on how to install the notebook and its dependencies.</p> </div> <div class="section" id="main-features-of-the-web-application"> <h3>Main features of the web application<a class="headerlink" href="#main-features-of-the-web-application" title="Permalink to this headline">¶</a></h3> <ul class="simple"> <li>In-browser editing for code, with automatic syntax highlighting, indentation, and tab completion/introspection.</li> <li>The ability to execute code from the browser, with the results of computations attached to the code which generated them.</li> <li>Displaying the result of computation using rich media representations, such as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures rendered by the <a class="reference external" href="http://matplotlib.org">matplotlib</a> library, can be included inline.</li> <li>In-browser editing for rich text using the <a class="reference external" href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> markup language, which can provide commentary for the code, is not limited to plain text.</li> <li>The ability to easily include mathematical notation within markdown cells using LaTeX, and rendered natively by <a class="reference external" href="http://www.mathjax.org/">MathJax</a>.</li> </ul> </div> <div class="section" id="notebook-documents"> <h3>Notebook documents<a class="headerlink" href="#notebook-documents" title="Permalink to this headline">¶</a></h3> <p>Notebook documents contains the inputs and outputs of a interactive session as well as additional text that accompanies the code but is not meant for execution. In this way, notebook files can serve as a complete computational record of a session, interleaving executable code with explanatory text, mathematics, and rich representations of resulting objects. These documents are internally <a class="reference external" href="http://en.wikipedia.org/wiki/JSON">JSON</a> files and are saved with the <tt class="docutils literal"><span class="pre">.ipynb</span></tt> extension. Since JSON is a plain text format, they can be version-controlled and shared with colleagues.</p> <p>Notebooks may be exported to a range of static formats, including HTML (for example, for blog posts), reStructeredText, LaTeX, PDF, and slide shows, via the new <a class="reference internal" href="nbconvert.html#nbconvert"><em>nbconvert</em></a> command.</p> <p>Furthermore, any <tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebook document available from a public URL can be shared via the <a class="reference external" href="nbviewer">IPython Notebook Viewer</a> (<a class="reference external" href="http://nbviewer.ipython.org">nbviewer</a>). This service loads the notebook document from the URL and renders it as a static web page. The results may thus be shared with a colleague, or as a public blog post, without other users needing to install IPython themselves. In effect, <a class="reference external" href="http://nbviewer.ipython.org">nbviewer</a> is simply <a class="reference internal" href="nbconvert.html#nbconvert"><em>nbconvert</em></a> as a web service, so you can do your own static conversions with nbconvert, without relying on nbviewer.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="nbconvert.html#notebook-format"><em>Details on the notebook JSON file format</em></a></p> </div> </div> </div> <div class="section" id="starting-the-notebook-server"> <h2>Starting the notebook server<a class="headerlink" href="#starting-the-notebook-server" title="Permalink to this headline">¶</a></h2> <p>You can start running a notebook server from the command line using the following command:</p> <div class="highlight-python"><div class="highlight"><pre>ipython notebook </pre></div> </div> <p>This will print some information about the notebook server in your console, and open a web browser to the URL of the web application (by default, <tt class="docutils literal"><span class="pre">http://127.0.0.1:8888</span></tt>).</p> <p>The landing page of the IPython notebook web application, the <strong>dashboard</strong>, shows the notebooks currently available in the notebook directory (by default, the directory from which the notebook server was started).</p> <p>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>When starting a notebook server from the command line, you can also open a particular notebook directly, bypassing the dashboard, with <tt class="docutils literal"><span class="pre">ipython</span> <span class="pre">notebook</span> <span class="pre">my_notebook.ipynb</span></tt>. The <tt class="docutils literal"><span class="pre">.ipynb</span></tt> extension is assumed if no extension is given.</p> <p>When you are inside an open notebook, the <cite>File | Open...</cite> menu option will open the dashboard in a new browser tab, to allow you to open another notebook from the notebook directory or to create a new notebook.</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 class="section" id="creating-a-new-notebook-document"> <h3>Creating a new notebook document<a class="headerlink" href="#creating-a-new-notebook-document" title="Permalink to this headline">¶</a></h3> <p>A new notebook may be created at any time, either from the dashboard, or using the <cite>File | New</cite> menu option from within an active notebook. The new notebook is created within the same directory and will open in a new browser tab. It will also be reflected as a new entry in the notebook list on the dashboard.</p> </div> <div class="section" id="opening-notebooks"> <h3>Opening notebooks<a class="headerlink" href="#opening-notebooks" title="Permalink to this headline">¶</a></h3> <p>An open notebook has <strong>exactly one</strong> interactive session connected to an <a class="reference internal" href="../overview.html#ipythonzmq"><em>IPython kernel</em></a>, which will execute code sent by the user and communicate back results. This kernel remains active if the web browser window is closed, and reopening the same notebook from the dashboard will reconnect the web application to the same kernel. In the dashboard, notebooks with an active kernel have a <tt class="docutils literal"><span class="pre">Shutdown</span></tt> button next to them, whereas notebooks without an active kernel have a <tt class="docutils literal"><span class="pre">Delete</span></tt> button in its place.</p> <p>Other clients may connect to the same underlying IPython kernel. 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"><div class="highlight"><pre>[NotebookApp] Kernel started: 87f7d2c0-13e3-43df-8bb8-1bd37aaf3373 </pre></div> </div> <p>This long string is the kernel’s ID which is sufficient for getting the information necessary to connect to the kernel. You can also request this connection data by running the <tt class="docutils literal"><span class="pre">%connect_info</span></tt> <a class="reference internal" href="../interactive/tutorial.html#magics-explained"><em>magic</em></a>. This will print the same ID information as well as the content of the JSON data structure it contains.</p> <p>You can then, for example, manually start a Qt console connected to the <em>same</em> kernel from the command line, by passing a portion of the ID:</p> <div class="highlight-python"><div class="highlight"><pre>$ ipython qtconsole --existing 87f7d2c0 </pre></div> </div> <p>Without an ID, <tt class="docutils literal"><span class="pre">--existing</span></tt> will connect to the most recently started kernel. This can also be done by running the <tt class="docutils literal"><span class="pre">%qtconsole</span></tt> <a class="reference internal" href="../interactive/tutorial.html#magics-explained"><em>magic</em></a> in the notebook.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="../overview.html#ipythonzmq"><em>Decoupled two-process model</em></a></p> </div> </div> </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 create a new notebook document, you will be presented with the <strong>notebook name</strong>, a <strong>menu bar</strong>, a <strong>toolbar</strong> and an empty <strong>code cell</strong>.</p> <p><strong>notebook name</strong>: The name of the notebook document 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 name reflects the name of the <tt class="docutils literal"><span class="pre">.ipynb</span></tt> notebook document file. Clicking on the notebook name brings up a dialog which allows you to rename it. Thus, renaming a notebook from “Untitled0” to “My first notebook” in the browser, renames the <tt class="docutils literal"><span class="pre">Untitled0.ipynb</span></tt> file to <tt class="docutils literal"><span class="pre">My</span> <span class="pre">first</span> <span class="pre">notebook.ipynb</span></tt>.</p> <p><strong>menu bar</strong>: The menu bar presents different options that may be used to manipulate the way the notebook functions.</p> <p><strong>toolbar</strong>: The tool bar gives a quick way of performing the most-used operations within the notebook, by clicking on an icon.</p> <p><strong>code cell</strong>: the default type of cell, read on for an explanation of cells</p> </div> <div class="section" id="structure-of-a-notebook-document"> <h2>Structure of a notebook document<a class="headerlink" href="#structure-of-a-notebook-document" title="Permalink to this headline">¶</a></h2> <p>The notebook consists of a sequence of cells. A cell is a multi-line text input field, and its contents can be executed by using <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt>, or by clicking either the “Play” button the toolbar, or <cite>Cell | Run</cite> in the menu bar. The execution behavior of a cell is determined the cell’s type. There are four types of cells: <strong>code cells</strong>, <strong>markdown cells</strong>, <strong>raw cells</strong> and <strong>heading cells</strong>. Every cell starts off being a <strong>code cell</strong>, but its type can be changed by using a dropdown on the toolbar (which will be “Code”, initially), or via <a class="reference internal" href="#keyboard-shortcuts"><em>keyboard shortcuts</em></a>.</p> <p>For more information on the different things you can do in a notebook, see the <a class="reference external" href="http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/Notebook/Index.ipynb">collection of examples</a>.</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 cell</em> allows you to edit and write new code, with full syntax highlighting and tab completion. 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 <a class="reference internal" href="../interactive/tutorial.html#magics-explained"><em>cell magic commands</em></a>.</p> <p>When a code cell is executed, code that it contains is sent to the kernel associated with the notebook. The results that are returned from this computation are then displayed in the notebook as the cell’s <em>output</em>. The output is not limited to text, with 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 analysis package). This is known as IPython’s <em>rich display</em> capability.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p><a class="reference external" href="http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Part%202%20-%20Basic%20Output.ipynb">Basic Output</a> example notebook</p> <p class="last"><a class="reference external" href="http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Part%205%20-%20Rich%20Display%20System.ipynb">Rich Display System</a> example notebook</p> </div> </div> <div class="section" id="markdown-cells"> <h3>Markdown cells<a class="headerlink" href="#markdown-cells" 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 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 cell is executed, the Markdown code is converted into the corresponding formatted 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. When the Markdown cell is executed, the LaTeX portions are automatically rendered in the HTML output as equations with high quality typography. This is made possible by <a class="reference external" href="http://www.mathjax.org/">MathJax</a>, which supports a <a class="reference external" href="mathjax_tex">large subset</a> of LaTeX functionality</p> <p>Standard mathematics environments defined by LaTeX and AMS-LaTeX (the <cite>amsmath</cite> package) also work, 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>. New LaTeX macros may be defined using standard methods, such as <tt class="docutils literal"><span class="pre">\newcommand</span></tt>, by placing them anywhere <em>between math delimiters</em> in a Markdown cell. These definitions are then available throughout the rest of the IPython session.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Part%204%20-%20Markdown%20Cells.ipynb">Markdown Cells</a> example notebook</p> </div> </div> <div class="section" id="raw-cells"> <h3>Raw cells<a class="headerlink" href="#raw-cells" title="Permalink to this headline">¶</a></h3> <p><em>Raw</em> cells provide a place in which you can write <em>output</em> directly. Raw cells are not evaluated by the notebook. When passed through <a class="reference internal" href="nbconvert.html#nbconvert"><em>nbconvert</em></a>, raw cells arrive in the destination format unmodified. For example, this allows you to type full LaTeX into a raw cell, which will only be rendered by LaTeX after conversion by nbconvert.</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 (top level) down to level 6 (paragraph). These can be used later for constructing tables of contents, etc. As with Markdown cells, a heading cell is replaced by a rich text rendering of the heading when the cell is executed.</p> </div> </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.</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>At certain moments, it may be necessary to interrupt a calculation which is taking too long to complete. This may be done with the <cite>Kernel | Interrupt</cite> menu option, or the <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">i</span></tt> keyboard shortcut. Similarly, it may be necessary or desirable to restart the whole computational process, with the <cite>Kernel | Restart</cite> menu option or <tt class="kbd docutils literal"><span class="pre">Ctrl-m</span> <span class="pre">.</span></tt> shortcut.</p> <p>A notebook may be downloaded in either a <tt class="docutils literal"><span class="pre">.ipynb</span></tt> or <tt class="docutils literal"><span class="pre">.py</span></tt> file from the menu option <cite>File | Download as</cite>. 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 rich output has been removed and the content of markdown cells have been inserted as comments.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p><a class="reference external" href="http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Part%25201%2520-%2520Running%2520Code.ipynb">Running Code in the IPython Notebook</a> example notebook</p> <p><a class="reference external" href="http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Part%202%20-%20Basic%20Output.ipynb">Basic Output</a> example notebook</p> <p class="last"><a class="reference internal" href="#note-about-roundtrip"><em>a warning about doing “roundtrip” conversions</em></a>.</p> </div> <div class="section" id="keyboard-shortcuts"> <span id="id1"></span><h3>Keyboard shortcuts<a class="headerlink" href="#keyboard-shortcuts" title="Permalink to this headline">¶</a></h3> <p>All actions in the notebook can be performed with the mouse, but keyboard shortcuts are also available for the most common ones. The essential 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>: run cell</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 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 cell. <tt class="kbd docutils literal"><span class="pre">Shift-Enter</span></tt> is equivalent to clicking 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>: run cell in-place</dt> <dd><p class="first last">Execute the current cell as if it were in “terminal mode”, where any output is shown, but the cursor <em>remains</em> in the current cell. The cell’s entire contents are selected after execution, so you can just start typing and only the new input will be in the 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>: run cell, insert below</dt> <dd><p class="first last">Executes the current cell, shows the output, and inserts a <em>new</em> cell between the current cell and the cell below (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 above the current one.)</p> </dd> </dl> </li> <li><dl class="first docutils"> <dt><tt class="kbd docutils literal"><span class="pre">Esc</span></tt> and <tt class="kbd docutils literal"><span class="pre">Enter</span></tt>: Command mode and edit mode</dt> <dd><p class="first last">In command mode, you can easily navigate around the notebook using keyboard shortcuts. In edit mode, you can edit text in cells.</p> </dd> </dl> </li> </ul> <p>For the full list of available shortcuts, click <em class="guilabel">Help</em>, <em class="guilabel">Keyboard Shortcuts</em> in the notebook menus.</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 display plots that are the output of running code cells. IPython is designed to work seamlessly with the <a class="reference external" href="http://matplotlib.org">matplotlib</a> plotting library to provide 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> <a class="reference internal" href="../interactive/tutorial.html#magics-explained"><em>magic command</em></a>. 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>If the <tt class="docutils literal"><span class="pre">%matplotlib</span></tt> magic is called without an argument, the output of a plotting command is displayed using the default <tt class="docutils literal"><span class="pre">matplotlib</span></tt> backend in a separate window. Alternatively, the backend can be explicitly requested using, for example:</p> <div class="highlight-python"><div class="highlight"><pre>%matplotlib gtk </pre></div> </div> <p>A particularly interesting backend, provided by IPython, is the <tt class="docutils literal"><span class="pre">inline</span></tt> backend. This is available only for the IPython Notebook and the <a class="reference internal" href="../interactive/qtconsole.html#qtconsole"><em>IPython QtConsole</em></a>. It can be invoked as follows:</p> <div class="highlight-python"><div class="highlight"><pre>%matplotlib inline </pre></div> </div> <p>With this backend, the output of plotting commands is displayed <em>inline</em> within the notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Part%203%20-%20Plotting%20with%20Matplotlib.ipynb">Plotting with Matplotlib</a> example notebook</p> </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 notebook server can be run with a variety of command line arguments. To see a list of available options enter:</p> <div class="highlight-python"><div class="highlight"><pre>$ ipython notebook --help </pre></div> </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"><div class="highlight"><pre>$ ipython locate </pre></div> </div> <p>To create a new set of default configuration files, with lots of information on available options, use:</p> <div class="highlight-python"><div class="highlight"><pre>$ ipython profile create </pre></div> </div> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p><a class="reference internal" href="../development/config.html#config-overview"><em>Overview of the IPython configuration system</em></a>, in particular <a class="reference internal" href="../config/intro.html#profiles"><em>Profiles</em></a>.</p> <p><a class="reference internal" href="public_server.html#notebook-server-security"><em>Securing a notebook server</em></a></p> <p class="last"><a class="reference internal" href="public_server.html#notebook-public-server"><em>Running a public notebook server</em></a></p> </div> </div> <div class="section" id="signing-notebooks"> <span id="id2"></span><h2>Signing Notebooks<a class="headerlink" href="#signing-notebooks" title="Permalink to this headline">¶</a></h2> <p>To prevent untrusted code from executing on users’ behalf when notebooks open, we have added a signature to the notebook, stored in metadata. The notebook server verifies this signature when a notebook is opened. If the signature stored in the notebook metadata does not match, javascript and HTML output will not be displayed on load, and must be regenerated by re-executing the cells.</p> <p>Any notebook that you have executed yourself <em>in its entirety</em> will be considered trusted, and its HTML and javascript output will be displayed on load.</p> <p>If you need to see HTML or Javascript output without re-executing, you can explicitly trust notebooks, such as those shared with you, or those that you have written yourself prior to IPython 2.0, at the command-line with:</p> <div class="highlight-python"><div class="highlight"><pre>$ ipython trust mynotebook.ipynb [other notebooks.ipynb] </pre></div> </div> <p>This just generates a new signature stored in each notebook.</p> <p>You can generate a new notebook signing key with:</p> <div class="highlight-python"><div class="highlight"><pre>$ ipython trust --reset </pre></div> </div> </div> <div class="section" id="importing-py-files"> <h2>Importing <tt class="docutils literal"><span class="pre">.py</span></tt> files<a class="headerlink" href="#importing-py-files" title="Permalink to this headline">¶</a></h2> <p><tt class="docutils literal"><span class="pre">.py</span></tt> files will be imported as a notebook with the same basename, but an <tt class="docutils literal"><span class="pre">.ipynb</span></tt> extension, located in the notebook 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>Note that <tt class="docutils literal"><span class="pre">.py</span></tt> scripts obtained from a notebook document using <a class="reference internal" href="nbconvert.html"><em>nbconvert</em></a> maintain the structure of the notebook in comments. Reimporting such a script back into a notebook will preserve this structure.</p> <div class="admonition warning" id="note-about-roundtrip"> <p class="first admonition-title">Warning</p> <p class="last">While in simple cases you can “roundtrip” 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 class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="nbconvert.html#notebook-format"><em>LaTeX citations</em></a></p> </div> </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="nbconvert.html" title="Converting notebooks to other formats" >next</a> |</li> <li class="right" > <a href="index.html" title="The IPython notebook" >previous</a> |</li> <li><a href="http://ipython.org">home</a>| </li> <li><a href="../search.html">search</a>| </li> <li><a href="../index.html">documentation </a> »</li> <li><a href="index.html" >The IPython notebook</a> »</li> </ul> </div> <div class="footer"> © Copyright The IPython Development Team. Last updated on Sep 02, 2015. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3. </div> </body> </html>