The Jupyter Notebook#

Introduction#

Jupyter Notebook is a notebook authoring application, under theProjectJupyter umbrella. Built on the power ofthecomputational notebook format,Jupyter Notebook offers fast, interactive new ways to prototype and explainyour code, explore and visualize your data, and share your ideas with others.

Notebooks extend the console-based approach to interactive computing in aqualitatively new direction, providing a web-based application suitable forcapturing the whole computation process: developing, documenting, and executingcode, as well as communicating the results. The Jupyter notebook combines twocomponents:

A web application: A browser-based editing program for interactive authoringof computational notebooks which provides a fast interactive environment for prototyping andexplaining code, exploring and visualizing data, and sharing ideas with others

Computational Notebook documents: A shareable document that combines computercode, plain language descriptions, data, rich visualizations like 3D models, charts,mathematics, graphs and figures, and interactive controls

See also

See theinstallation guide on how to install thenotebook and its dependencies.

Main features of the web application#

  • In-browser editing for code, with automatic syntax highlighting,indentation, and tab completion/introspection.

  • The ability to execute code from the browser, with the results ofcomputations attached to the code which generated them.

  • Displaying the result of computation using rich media representations, suchas HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figuresrendered by the [matplotlib] library, can be included inline.

  • In-browser editing for rich text using the [Markdown] markup language, whichcan provide commentary for the code, is not limited to plain text.

  • The ability to easily include mathematical notation within markdown cellsusing LaTeX, and rendered natively byMathJax.

Notebook documents#

Notebook documents contains the inputs and outputs of a interactive session aswell as additional text that accompanies the code but is not meant forexecution. In this way, notebook files can serve as a complete computationalrecord of a session, interleaving executable code with explanatory text,mathematics, and rich representations of resulting objects. These documentsare internallyJSON files and are saved with the.ipynb extension. SinceJSON is a plain text format, they can be version-controlled and shared withcolleagues.

Notebooks may be exported to a range of static formats, including HTML (forexample, for blog posts), reStructuredText, LaTeX, PDF, and slide shows, viathe [nbconvert] command.

Furthermore, any.ipynb notebook document available from a publicURL can be shared via the Jupyter Notebook Viewer <nbviewer>.This service loads the notebook document from the URL and renders it as astatic web page. The results may thus be shared with a colleague, or as apublic blog post, without other users needing to install the Jupyter notebookthemselves. In effect, nbviewer is simply [nbconvert] asa web service, so you can do your own static conversions with nbconvert,without relying on nbviewer.

Notebooks and privacy#

Because you use Jupyter in a web browser, some people are understandablyconcerned about using it with sensitive data.However, if you followed the standardinstall instructions,Jupyter is actually running on your own computer.If the URL in the address bar starts withhttp://localhost: orhttp://127.0.0.1:, it’s your computer acting as the server.Jupyter doesn’t send your data anywhere else—and as it’s open source,other people can check that we’re being honest about this.

You can also use Jupyter remotely:your company or university might run the server for you, for instance.If you want to work with sensitive data in those cases,talk to your IT or data protection staff about it.

We aim to ensure that other pages in your browser or other users on the samecomputer can’t access your notebook server. See thesecurity documentation formore about this.

Starting the notebook server#

You can start running a notebook server from the command line using thefollowing command:

jupyternotebook

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,http://127.0.0.1:8888).

The landing page of the Jupyter notebook web application, thedashboard,shows the notebooks currently available in the notebook directory (by default,the directory from which the notebook server was started).

You can create new notebooks from the dashboard with theNewNotebookbutton, or open existing ones by clicking on their name. You can also dragand drop.ipynb notebooks and standard.py Python source code filesinto the notebook list area.

When starting a notebook server from the command line, you can also open aparticular notebook directly, bypassing the dashboard, withjupyternotebookmy_notebook.ipynb. The.ipynb extension is assumed if no extension isgiven.

When you are inside an open notebook, theFile|Open... menu option willopen the dashboard in a new browser tab, to allow you to open another notebookfrom the notebook directory or to create a new notebook.

Note

You can start more than one notebook server at the same time, if you wantto work on notebooks in different directories. By default the firstnotebook server starts on port 8888, and later notebook servers search forports near that one. You can also manually specify the port with the--port option.

Creating a new notebook document#

A new notebook may be created at any time, either from the dashboard, or usingtheFile ‣ New menu option from within an active notebook.The new notebook is created within the same directory and will open in a newbrowser tab. It will also be reflected as a new entry in the notebook list onthe dashboard.

a screencast showing how to create new notebooks from the user interface

Opening notebooks#

An open notebook hasexactly one interactive session connected to akernel, which will execute code sent by the userand communicate back results. This kernel remains active if the web browserwindow is closed, and reopening the same notebook from the dashboard willreconnect the web application to the same kernel. In the dashboard, notebookswith an active kernel have aShutdown button next to them, whereasnotebooks without an active kernel have aDelete button in its place.

Other clients may connect to the same kernel.When each kernel is started, the notebook server prints to the terminal amessage like this:

[JupyterNotebookApp]Kernelstarted:87f7d2c0-13e3-43df-8bb8-1bd37aaf3373

This long string is the kernel’s ID which is sufficient for getting theinformation necessary to connect to the kernel. If the notebook uses the IPythonkernel, you can also see thisconnection data by running the%connect_infomagic, which will print the same ID information along with otherdetails.

You can then, for example, manually start a Qt console connected to thesamekernel from the command line, by passing a portion of the ID:

$ jupyter qtconsole --existing 87f7d2c0

Without an ID,--existing will connect to the most recentlystarted kernel.

With the IPython kernel, you can also run the%qtconsolemagic in the notebook to open a Qt console connectedto the same kernel.

Notebook user interface#

When you create a new notebook document, you will be presented with thenotebook name, amenu bar, atoolbar and an emptycode cell.

a screenshot showing a blank notebook

Notebook name: The name displayed at the top of the page,next to the Jupyter logo, reflects the name of the.ipynb file.Clicking on the notebook name brings up a dialog which allows you to rename it.Thus, renaming a notebookfrom “Untitled0” to “My first notebook” in the browser, renames theUntitled0.ipynb file toMyfirstnotebook.ipynb.

Menu bar: The menu bar presents different options that may be used tomanipulate the way the notebook functions.

Toolbar: The tool bar gives a quick way of performing the most-usedoperations within the notebook, by clicking on an icon.

Code cell: the default type of cell; read on for an explanation of cells.

Structure of a notebook document#

The notebook consists of a sequence of cells. A cell is a multiline text inputfield, and its contents can be executed by usingShift-Enter, or byclicking either the “Play” button the toolbar, orCell,Run in the menu bar.The execution behavior of a cell is determined by the cell’s type. There are threetypes of cells:code cells,markdown cells, andraw cells. Everycell starts off being acode cell, but its type can be changed by using adrop-down on the toolbar (which will be “Code”, initially), or viakeyboard shortcuts.

For more information on the different things you can do in a notebook,see thecollection of examples.

Code cells#

Acode cell allows you to edit and write new code, with full syntaxhighlighting and tab completion. The programming language you use dependson thekernel, and the default kernel (IPython) runs Python code.

When a code cell is executed, code that it contains is sent to the kernelassociated with the notebook. The results that are returned from thiscomputation are then displayed in the notebook as the cell’soutput. Theoutput is not limited to text, with many other possible forms of output arealso possible, includingmatplotlib figures and HTML tables (as used, forexample, in thepandas data analysis package). This is known as IPython’srich display capability.

See also

[Rich Output] example notebook

Markdown cells#

You can document the computational process in a literate way, alternatingdescriptive text with code, usingrich text. In IPython this is accomplishedby marking up text with the Markdown language. The corresponding cells arecalledMarkdown cells. The Markdown language provides a simple way toperform this text markup, that is, to specify which parts of the text shouldbe emphasized (italics), bold, form lists, etc.

If you want to provide structure for your document, you can use markdownheadings. Markdown headings consist of 1 to 6 hash # signs# followed by aspace and the title of your section. The markdown heading will be convertedto a clickable link for a section of the notebook. It is also used as a hintwhen exporting to other document formats, like PDF.

When a Markdown cell is executed, the Markdown code is converted intothe corresponding formatted rich text. Markdown allows arbitrary HTML code forformatting.

Within Markdown cells, you can also includemathematics in a straightforwardway, using standard LaTeX notation:$...$ for inline mathematics and$$...$$ for displayed mathematics. When the Markdown cell is executed,the LaTeX portions are automatically rendered in the HTML output as equationswith high quality typography. This is made possible byMathJax, whichsupports alarge subset of LaTeX functionality

Standard mathematics environments defined by LaTeX and AMS-LaTeX (theamsmath package) also work, such as\begin{equation}...\end{equation}, and\begin{align}...\end{align}.New LaTeX macros may be defined using standard methods,such as\newcommand, by placing them anywherebetween math delimiters ina Markdown cell. These definitions are then available throughout the rest ofthe IPython session.

See also

[Working with Markdown Cells] example notebook

Raw cells#

Raw cells provide a place in which you can writeoutput directly.Raw cells are not evaluated by the notebook.When passed through [nbconvert], raw cells arrive in thedestination format unmodified. For example, you can type full LaTeXinto a raw cell, which will only be rendered by LaTeX after conversion bynbconvert.

Basic workflow#

The normal workflow in a notebook is, then, quite similar to a standardIPython session, with the difference that you can edit cells in-place multipletimes until you obtain the desired results, rather than having torerun separate scripts with the%run magic command.

Typically, you will work on a computational problem in pieces, organizingrelated ideas into cells and moving forward once previous parts workcorrectly. This is much more convenient for interactive exploration thanbreaking up a computation into scripts that must be executed together, as waspreviously necessary, especially if parts of them take a long time to run.

To interrupt a calculation which is taking too long, use theKernel,Interrupt menu option, or thei,i keyboard shortcut.Similarly, to restart the whole computational process,use theKernel,Restart menu option or0,0shortcut.

A notebook may be downloaded as a.ipynb file or converted to a number ofother formats using the menu optionFile,Download as.

See also

[Running Code in the Jupyter Notebook] example notebook

[Notebook Basics] example notebook

Keyboard shortcuts#

All actions in the notebook can be performed with the mouse, but keyboardshortcuts are also available for the most common ones. The essential shortcutsto remember are the following:

  • Shift-Enter: run cell: Execute the current cell, show any output, and jump to the next cell below.IfShift-Enter is invoked on the last cell, it makes a new cell below.This is equivalent to clicking theCell,Run menuitem, or the Play button in the toolbar.

  • Esc: Command mode: In command mode, you can navigate around the notebook using keyboard shortcuts.

  • Enter: Edit mode: In edit mode, you can edit text in cells.

For the full list of available shortcuts, clickHelp,Keyboard Shortcuts in the notebook menus.

Searching#

Jupyter Notebook has an advanced built-in search plugin for finding text within anotebook or other document, which uses theCtrl-F (Cmd+F for macOS) shortcut by default.

Your browser’sfind function will give unexpected results because it doesn’t haveaccess to the full content of a document (by default), but you can still use your browser findfunction from the browser menu if you want, or you can disable the built-in searchshortcut using the Advanced Settings Editor.

Alternatively, you can disable windowed notebook rendering to expose the fulldocument content to the browser at the expense of performance.

Plotting#

One major feature of the Jupyter notebook is the ability to display plots thatare the output of running code cells. The IPython kernel is designed to workseamlessly with the [matplotlib] plotting library to provide this functionality.Specific plotting library integration is a feature of the kernel.

Installing kernels#

For information on how to install a Python kernel, refer to theIPython install page.

The Jupyter wiki has a long list ofKernels for other languages.They usually come with instructions on how to make the kernel availablein the notebook.

Trusting Notebooks#

To prevent untrusted code from executing on users’ behalf when notebooks open,we store a signature of each trusted notebook.The notebook server verifies this signature when a notebook is opened.If no matching signature is found,Javascript and HTML output will not be displayeduntil they are regenerated by re-executing the cells.

Any notebook that you have fully executed yourself will beconsidered trusted, and its HTML and Javascript output will be displayed onload.

If you need to see HTML or Javascript output without re-executing,and you are sure the notebook is not malicious, you can tell Jupyter to trust itat the command-line with:

$ jupyter trust mynotebook.ipynb

See thesecurity documentation for more details about the trust mechanism.

Browser Compatibility#

The Jupyter Notebook aims to support the latest versions of these browsers:

  • Chrome

  • Safari

  • Firefox

Up to date versions of Opera and Edge may also work, but if they don’t, pleaseuse one of the supported browsers.

Using Safari with HTTPS and an untrusted certificate is known to not work(websockets will fail).