<commit_before>.. _htmlnotebook:

=========================
An HTML Notebook IPython
=========================

.. seealso::

    :ref:`Installation requirements <installnotebook>` for the Notebook.

The IPython Notebook consists of two related components:

* An JSON based Notebook document format for recording and distributing
  Python code and rich text.
* A web-based user interface for authoring and running notebook documents.

The Notebook can be used by starting the Notebook server with the 
command::

    $ ipython notebook

Note that by default, the notebook doesn't load pylab, it's just a normal
IPython session like any other.  If you want pylab support, you must use::
    
    $ ipython notebook --pylab

which will behave similar to the terminal and Qt console versions, using your
default matplotlib backend and providing floating interactive plot windows.  If
you want inline figures, you must manually select the ``inline`` backend::

    $ ipython notebook --pylab=inline

You can start the notebook to communicate via a secure protocol mode using a
self-signed certificate by typing::

    $ ipython notebook --certfile=mycert.pem

.. note::

    A self-signed certificate can be generated with openssl. For example:

    openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

This server uses the same ZeroMQ-based two process kernel architecture as
the QT Console as well Tornado for serving HTTP/S requests. Some of the main
features of the Notebook include:

* Display rich data (png/html/latex/svg) in the browser as a result of
  computations.
* Compose text cells using HTML and Markdown.
* Import and export notebook documents in range of formats (.ipynb, .py).
* In browser syntax highlighting, tab completion and autoindentation.
* Inline matplotlib plots that can be stored in Notebook documents and opened
  later.

See :ref:`our installation documentation <install_index>` for directions on
how to install the notebook and its dependencies.

.. note::

   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 in port 8888, later notebooks search for random ports near
   that one.  You can also manually specify the port with the ``--port``
   option.
   

Basic Usage
===========

The landing page of the notebook server application, which we call the IPython
Notebook *dashboard*, shows the notebooks currently available in the directory
in which the application was started, and allows you to create new notebooks.

A notebook is a combination of two things:

1. An interactive session connected to an IPython kernel, controlled by a web
application that can send input to the console and display many types of output
(text, graphics, mathematics and more).  This is the same kernel used by the
:ref:`Qt console <qtconsole>`, but in this case the web console sends input in
persistent cells that you can edit in-place instead of the vertically scrolling
terminal style used by the Qt console.

2. A document that can save the inputs and outputs of the session as well as
additional text that accompanies the code but is not meant for execution.  In
this way, notebook files serve as a complete computational record of a session
including explanatory text and mathematics, code and resulting figures.  These
documents are internally JSON files and are saved with the ``.ipynb``
extension.

If you have ever used the Mathematica or Sage notebooks (the latter is also
web-based__) you should feel right at home.  If you have not, you should be
able to learn how to use it in just a few minutes.

.. __: http://sagenb.org


Creating and editing notebooks
------------------------------

You can create new notebooks from the dashboard with the ``New Notebook``
button or open existing ones by clicking on their name.  Once in a notebook,
your browser tab will reflect the name of that notebook (prefixed with "IPy:").
The URL for that notebook is not meant to be human-readable and is *not*
persistent across invocations of the notebook server.

You can also drag and drop into the area listing files any python file: it
will be imported into a notebook with the same name (but ``.ipynb`` extension)
located in the directory where the notebook server was started.  This notebook
will consist of a single cell with all the code in the file, which you can
later manually partition into individual cells for gradual execution, add text
and graphics, etc.

Workflow and limitations
------------------------

The normal workflow in a notebook is quite similar to a normal IPython session,
with the difference that you can edit a cell in-place multiple times until you
obtain the desired results rather than having to rerun separate scripts with
the ``%run`` magic (though magics also work in the notebook).   Typically
you'll work on a problem in pieces, organizing related pieces into cells and
moving forward as previous parts work correctly.  This is much more convenient
for interactive exploration than breaking up a computation into scripts that
must be executed together, especially if parts of them take a long time to run
(In the traditional terminal-based IPython, you can use tricks with namespaces
and ``%run -i`` to achieve this capability, but we think the notebook is a more
natural solution for  that kind of problem).

The only significant limitation the notebook currently has, compared to the qt
console, is that it can not run any code that expects input from the kernel
(such as scripts that call :func:`raw_input`).  Very importantly, this means
that the ``%debug`` magic does *not* work in the notebook!  We intend to
correct this limitation, but in the meantime, there is a way to debug problems
in the notebook: you can attach a Qt console to your existing notebook kernel,
and run ``%debug`` from the Qt console.  Simply look for the lines in the
terminal where you started the kernel that read something like::

    [IPKernelApp] To connect another client to this kernel, use:
    [IPKernelApp] --existing --shell=53328 --iopub=53817 --stdin=34736 --hb=45543

and then start a qt console pointing to that kernel::

    ipython qtconsole --existing --shell=53328 --iopub=53817 --stdin=34736 --hb=45543

    
Text input
----------

In addition to code cells and the output they produce (such as figures), you
can also type text not meant for execution.  To type text, change the type of a
cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m`
keybinding (see below).  You can then type any text in Markdown_ syntax, as
well as mathematical expressions if you use ``$...$`` for inline math or
``$$...$$`` for displayed math.

Exporting a notebook
--------------------

If you want to provide others with a static HTML or PDF view of your notebook,<commit_msg>Update info on connecting to existing notebook kernels with qtconsole.<commit_after>.. _htmlnotebook:

=========================
An HTML Notebook IPython
=========================

.. seealso::

    :ref:`Installation requirements <installnotebook>` for the Notebook.

The IPython Notebook consists of two related components:

* An JSON based Notebook document format for recording and distributing
  Python code and rich text.
* A web-based user interface for authoring and running notebook documents.

The Notebook can be used by starting the Notebook server with the 
command::

    $ ipython notebook

Note that by default, the notebook doesn't load pylab, it's just a normal
IPython session like any other.  If you want pylab support, you must use::
    
    $ ipython notebook --pylab

which will behave similar to the terminal and Qt console versions, using your
default matplotlib backend and providing floating interactive plot windows.  If
you want inline figures, you must manually select the ``inline`` backend::

    $ ipython notebook --pylab=inline

You can start the notebook to communicate via a secure protocol mode using a
self-signed certificate by typing::

    $ ipython notebook --certfile=mycert.pem

.. note::

    A self-signed certificate can be generated with openssl. For example:

    openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

This server uses the same ZeroMQ-based two process kernel architecture as
the QT Console as well Tornado for serving HTTP/S requests. Some of the main
features of the Notebook include:

* Display rich data (png/html/latex/svg) in the browser as a result of
  computations.
* Compose text cells using HTML and Markdown.
* Import and export notebook documents in range of formats (.ipynb, .py).
* In browser syntax highlighting, tab completion and autoindentation.
* Inline matplotlib plots that can be stored in Notebook documents and opened
  later.

See :ref:`our installation documentation <install_index>` for directions on
how to install the notebook and its dependencies.

.. note::

   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 in port 8888, later notebooks search for random ports near
   that one.  You can also manually specify the port with the ``--port``
   option.
   

Basic Usage
===========

The landing page of the notebook server application, which we call the IPython
Notebook *dashboard*, shows the notebooks currently available in the directory
in which the application was started, and allows you to create new notebooks.

A notebook is a combination of two things:

1. An interactive session connected to an IPython kernel, controlled by a web
application that can send input to the console and display many types of output
(text, graphics, mathematics and more).  This is the same kernel used by the
:ref:`Qt console <qtconsole>`, but in this case the web console sends input in
persistent cells that you can edit in-place instead of the vertically scrolling
terminal style used by the Qt console.

2. A document that can save the inputs and outputs of the session as well as
additional text that accompanies the code but is not meant for execution.  In
this way, notebook files serve as a complete computational record of a session
including explanatory text and mathematics, code and resulting figures.  These
documents are internally JSON files and are saved with the ``.ipynb``
extension.

If you have ever used the Mathematica or Sage notebooks (the latter is also
web-based__) you should feel right at home.  If you have not, you should be
able to learn how to use it in just a few minutes.

.. __: http://sagenb.org


Creating and editing notebooks
------------------------------

You can create new notebooks from the dashboard with the ``New Notebook``
button or open existing ones by clicking on their name.  Once in a notebook,
your browser tab will reflect the name of that notebook (prefixed with "IPy:").
The URL for that notebook is not meant to be human-readable and is *not*
persistent across invocations of the notebook server.

You can also drag and drop into the area listing files any python file: it
will be imported into a notebook with the same name (but ``.ipynb`` extension)
located in the directory where the notebook server was started.  This notebook
will consist of a single cell with all the code in the file, which you can
later manually partition into individual cells for gradual execution, add text
and graphics, etc.

Workflow and limitations
------------------------

The normal workflow in a notebook is quite similar to a normal IPython session,
with the difference that you can edit a cell in-place multiple times until you
obtain the desired results rather than having to rerun separate scripts with
the ``%run`` magic (though magics also work in the notebook).   Typically
you'll work on a problem in pieces, organizing related pieces into cells and
moving forward as previous parts work correctly.  This is much more convenient
for interactive exploration than breaking up a computation into scripts that
must be executed together, especially if parts of them take a long time to run
(In the traditional terminal-based IPython, you can use tricks with namespaces
and ``%run -i`` to achieve this capability, but we think the notebook is a more
natural solution for  that kind of problem).

The only significant limitation the notebook currently has, compared to the qt
console, is that it can not run any code that expects input from the kernel
(such as scripts that call :func:`raw_input`).  Very importantly, this means
that the ``%debug`` magic does *not* work in the notebook!  We intend to
correct this limitation, but in the meantime, there is a way to debug problems
in the notebook: you can attach a Qt console to your existing notebook kernel,
and run ``%debug`` 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
127.0.0.1), you can just type ``%qtconsole`` in the notebook and a Qt console
will open up connected to that same kernel.

In general, the notebook server prints the full details of how to connect to
each kernel at the terminal, with lines like:

    [IPKernelApp] To connect another client to this kernel, use:
    [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json

This is the name of a JSON file that contains all the port and validation
information necessary to connect to the kernel.  You can manually start a
qt console with::

    ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json

and if you only have a single kernel running, simply typing::

    ipython qtconsole --existing

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 ``%connect_info``; this will print the same file information as well
as the content of the JSON data structure it contains.

    
Text input
----------

In addition to code cells and the output they produce (such as figures), you
can also type text not meant for execution.  To type text, change the type of a
cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m`
keybinding (see below).  You can then type any text in Markdown_ syntax, as
well as mathematical expressions if you use ``$...$`` for inline math or
``$$...$$`` for displayed math.

Exporting a notebook
--------------------

If you want to provide others with a static HTML or PDF view of your notebook,
{'fraction_non_alphanumeric': 0.06693642411357245, 'fraction_numerical': 0.007818393800150881, 'mean_word_length': 3.94640434192673, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5550602', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3534, 'n_words': 2212}