Show More
@@ -1,482 +1,470 b'' | |||||
1 | .. _htmlnotebook: |
|
1 | .. _htmlnotebook: | |
2 |
|
2 | |||
3 |
==================== |
|
3 | ==================== | |
4 | An HTML Notebook IPython |
|
4 | The IPython Notebook | |
5 |
==================== |
|
5 | ==================== | |
6 |
|
6 | |||
7 | .. seealso:: |
|
7 | .. seealso:: | |
8 |
|
8 | |||
9 | :ref:`Installation requirements <installnotebook>` for the Notebook. |
|
9 | :ref:`Installation requirements <installnotebook>` for the Notebook. | |
10 |
|
10 | |||
11 | The IPython Notebook consists of two related components: |
|
11 | The IPython Notebook consists of two related components: | |
12 |
|
12 | |||
13 | * An JSON based Notebook document format for recording and distributing |
|
13 | * A web application for interactive authoring of literate computations, combining explanatory text, mathematics, computations and rich media output. Input and output are stored in persistent cells that may be edited in-place. | |
14 | Python code and rich text. |
|
14 | ||
15 | * A web-based user interface for authoring and running notebook documents. |
|
15 | * Notebook documents for recording and distributing | |
|
16 | the results. | |||
|
17 | ||||
|
18 | ||||
|
19 | Features of the IPython Notebook web app | |||
|
20 | ---------------------------------------- | |||
|
21 | ||||
|
22 | Some of the main | |||
|
23 | features of the IPython Notebook app include: | |||
|
24 | ||||
|
25 | * Display rich data representations (e.g. HTML / LaTeX / SVG) in the browser as a result of computations. | |||
|
26 | * Compose text cells using Markdown and HTML. | |||
|
27 | * Include mathematical equations, rendered directly in the browser by MathJax. | |||
|
28 | * Import standard Python scripts | |||
|
29 | * In-browser editing, syntax highlighting, tab completion and autoindentation. | |||
|
30 | * Inline figures rendered by the ``matplotlib`` library with publication quality, in a range of formats (SVG / PDF / PNG). | |||
|
31 | ||||
|
32 | If you have ever used the Mathematica or SAGE notebooks (the latter is also | |||
|
33 | web-based__) you should feel right at home. If you have not, you will be | |||
|
34 | able to learn how to use it in just a few minutes. | |||
|
35 | ||||
|
36 | .. __: http://sagenb.org | |||
|
37 | ||||
16 |
|
38 | |||
17 | The Notebook can be used by starting the Notebook server with the |
|
|||
18 | command:: |
|
|||
19 |
|
39 | |||
20 | $ ipython notebook |
|
|||
21 |
|
40 | |||
22 | Note that by default, the notebook doesn't load pylab, it's just a normal |
|
|||
23 | IPython session like any other. If you want pylab support, you must use:: |
|
|||
24 |
|
||||
25 | $ ipython notebook --pylab |
|
|||
26 |
|
41 | |||
27 | which will behave similar to the terminal and Qt console versions, using your |
|
42 | Notebook documents | |
28 | default matplotlib backend and providing floating interactive plot windows. If |
|
43 | ------------------ | |
29 | you want inline figures, you must manually select the ``inline`` backend:: |
|
|||
30 |
|
44 | |||
31 | $ ipython notebook --pylab inline |
|
45 | Notebook documents, or *notebooks*, are files which record all computations carried out and the results obtained in a literate way, including inputs, outputs, toegether with descriptive text and mathematics. | |
32 |
|
46 | |||
33 | This server uses the same ZeroMQ-based two process kernel architecture as |
|
47 | They are plain text files, which are thus easy to share with colleagues and place under version control. But, by using the | |
34 | the QT Console as well Tornado for serving HTTP/S requests. Some of the main |
|
48 | JSON format, they can record all aspects of the computation, including embedding rich media output. | |
35 | features of the Notebook include: |
|
49 | The standard file extension for notebook documents is ``.ipynb``. | |
|
50 | ||||
|
51 | Notebooks may easily be exported to a range of static formats, including HTML (for example, for blog posts), PDF and slide shows. | |||
|
52 | Furthermore, any publicly | |||
|
53 | available notebook may be shared via the `IPython Notebook Viewer | |||
|
54 | <http://nbviewer.ipython.org>`_ service, which will provide it as a static web | |||
|
55 | page. The results may thus be shared without having to install anything. | |||
36 |
|
56 | |||
37 | * Display rich data (png/html/latex/svg) in the browser as a result of |
|
|||
38 | computations. |
|
|||
39 | * Compose text cells using HTML and Markdown. |
|
|||
40 | * Import and export notebook documents in range of formats (.ipynb, .py). |
|
|||
41 | * In browser syntax highlighting, tab completion and autoindentation. |
|
|||
42 | * Inline matplotlib plots that can be stored in Notebook documents and opened |
|
|||
43 | later. |
|
|||
44 |
|
57 | |||
45 | See :ref:`our installation documentation <install_index>` for directions on |
|
58 | See :ref:`our installation documentation <install_index>` for directions on | |
46 | how to install the notebook and its dependencies. |
|
59 | how to install the notebook and its dependencies. | |
47 |
|
60 | |||
48 | .. note:: |
|
61 | .. note:: | |
49 |
|
62 | |||
50 | You can start more than one notebook server at the same time, if you want to |
|
63 | You can start more than one notebook server at the same time, if you want to | |
51 | work on notebooks in different directories. By default the first notebook |
|
64 | work on notebooks in different directories. By default the first notebook | |
52 |
server starts |
|
65 | server starts on port 8888, and later notebook servers search for ports near | |
53 | that one. You can also manually specify the port with the ``--port`` |
|
66 | that one. You can also manually specify the port with the ``--port`` | |
54 | option. |
|
67 | option. | |
55 |
|
68 | |||
56 |
|
69 | |||
57 | Basic Usage |
|
70 | Starting the IPython Notebook web app | |
58 | =========== |
|
71 | ===================================== | |
59 |
|
72 | |||
60 | The landing page of the notebook server application, which we call the IPython |
|
73 | The Notebook web app can be started with the command:: | |
61 | Notebook *dashboard*, shows the notebooks currently available in the directory |
|
|||
62 | in which the application was started, and allows you to create new notebooks. |
|
|||
63 |
|
74 | |||
64 | A notebook is a combination of two things: |
|
75 | $ ipython notebook | |
65 |
|
76 | |||
66 | 1. An interactive session connected to an IPython kernel, controlled by a web |
|
77 | The landing page of the notebook server application, the *dashboard*, shows the notebooks currently available in the working directory (the directory from which the notebook was started). | |
67 | application that can send input to the console and display many types of |
|
78 | You can create new notebooks from the dashboard with the ``New Notebook`` | |
68 | output (text, graphics, mathematics and more). This is the same kernel used |
|
79 | button, or open existing ones by clicking on their name. | |
69 | by the :ref:`Qt console <qtconsole>`, but in this case the web console sends |
|
|||
70 | input in persistent cells that you can edit in-place instead of the |
|
|||
71 | vertically scrolling terminal style used by the Qt console. |
|
|||
72 |
|
80 | |||
73 | 2. A document that can save the inputs and outputs of the session as well as |
|
|||
74 | additional text that accompanies the code but is not meant for execution. |
|
|||
75 | In this way, notebook files serve as a complete computational record of a |
|
|||
76 | session including explanatory text and mathematics, code and resulting |
|
|||
77 | figures. These documents are internally JSON files and are saved with the |
|
|||
78 | ``.ipynb`` extension. |
|
|||
79 |
|
81 | |||
80 | If you have ever used the Mathematica or Sage notebooks (the latter is also |
|
82 | You can also drag and drop into the area listing files any ``.py`` | |
81 | web-based__) you should feel right at home. If you have not, you should be |
|
83 | file: it will be imported into a notebook with the same name (but | |
82 | able to learn how to use it in just a few minutes. |
|
84 | ``.ipynb`` extension), located in the working directory. 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, and add text | |
|
85 | and graphics, etc. | |||
|
86 | Alternatively, | |||
|
87 | prior to import, you can manually add ``# <nbformat>2</nbformat>`` | |||
|
88 | markers at the start and then add separators for text/code cells, to get a cleaner import with the file already broken into individual cells. | |||
83 |
|
89 | |||
84 | .. __: http://sagenb.org |
|
|||
85 |
|
90 | |||
|
91 | The IPython Notebook web app is based on a server-client structure. | |||
|
92 | 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. | |||
86 |
|
93 | |||
87 | Creating and editing notebooks |
|
|||
88 | ------------------------------ |
|
|||
89 |
|
94 | |||
90 | You can create new notebooks from the dashboard with the ``New Notebook`` |
|
|||
91 | button or open existing ones by clicking on their name. Once in a notebook, |
|
|||
92 | your browser tab will reflect the name of that notebook (prefixed with "IPy:"). |
|
|||
93 | The URL for that notebook is not meant to be human-readable and is *not* |
|
|||
94 | persistent across invocations of the notebook server. |
|
|||
95 |
|
95 | |||
96 | You can also drag and drop into the area listing files any python file: it |
|
96 | Basic workflow | |
97 | will be imported into a notebook with the same name (but ``.ipynb`` extension) |
|
97 | ------------------------ | |
98 | located in the directory where the notebook server was started. This notebook |
|
|||
99 | will consist of a single cell with all the code in the file, which you can |
|
|||
100 | later manually partition into individual cells for gradual execution, add text |
|
|||
101 | and graphics, etc. |
|
|||
102 |
|
98 | |||
103 |
|
99 | |||
104 | Workflow and limitations |
|
100 | Once in a notebook, your browser tab will reflect the name of that notebook (prefixed with "IPy"). | |
105 | ------------------------ |
|
101 | The URL for that notebook is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change soon. | |
|
102 | ||||
|
103 | The normal workflow in a notebook is quite similar to a normal IPython | |||
|
104 | session, with the difference that you can edit a cell in-place multiple | |||
|
105 | times until you obtain the desired resultsj, rather than having to | |||
|
106 | rerun separate scripts with the ``%run`` magic (though magics also work | |||
|
107 | in the notebook). Typically you'll work on a problem in pieces, | |||
|
108 | organizing related pieces into cells and moving forward as previous | |||
|
109 | parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be | |||
|
110 | executed together, especially if parts of them take a long time to run | |||
|
111 | ||||
|
112 | The only significant limitation that the notebook currently has, compared to the qt console, is that it can not run any code that | |||
|
113 | expects input from the kernel (such as scripts that call | |||
|
114 | :func:`raw_input`). Very importantly, this means that the ``%debug`` | |||
|
115 | magic does *not* currently work in the notebook! This limitation will | |||
|
116 | be overcome in the future, 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. | |||
|
117 | If your notebook is running on a local | |||
|
118 | 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. | |||
|
119 | ||||
|
120 | ||||
|
121 | Connecting to an existing kernel | |||
|
122 | --------------------------------- | |||
106 |
|
123 | |||
107 | The normal workflow in a notebook is quite similar to a normal IPython session, |
|
124 | The notebook server always prints to the terminal the full details of | |
108 | with the difference that you can edit a cell in-place multiple times until you |
|
125 | how to connect to each kernel, with lines like:: | |
109 | obtain the desired results rather than having to rerun separate scripts with |
|
|||
110 | the ``%run`` magic (though magics also work in the notebook). Typically |
|
|||
111 | you'll work on a problem in pieces, organizing related pieces into cells and |
|
|||
112 | moving forward as previous parts work correctly. This is much more convenient |
|
|||
113 | for interactive exploration than breaking up a computation into scripts that |
|
|||
114 | must be executed together, especially if parts of them take a long time to run |
|
|||
115 | (In the traditional terminal-based IPython, you can use tricks with namespaces |
|
|||
116 | and ``%run -i`` to achieve this capability, but we think the notebook is a more |
|
|||
117 | natural solution for that kind of problem). |
|
|||
118 |
|
||||
119 | The only significant limitation the notebook currently has, compared to the qt |
|
|||
120 | console, is that it can not run any code that expects input from the kernel |
|
|||
121 | (such as scripts that call :func:`raw_input`). Very importantly, this means |
|
|||
122 | that the ``%debug`` magic does *not* work in the notebook! We intend to |
|
|||
123 | correct this limitation, but in the meantime, there is a way to debug problems |
|
|||
124 | in the notebook: you can attach a Qt console to your existing notebook kernel, |
|
|||
125 | and run ``%debug`` from the Qt console. If your notebook is running on a local |
|
|||
126 | computer (i.e. if you are accessing it via your localhost address at |
|
|||
127 | 127.0.0.1), you can just type ``%qtconsole`` in the notebook and a Qt console |
|
|||
128 | will open up connected to that same kernel. |
|
|||
129 |
|
||||
130 | In general, the notebook server prints the full details of how to connect to |
|
|||
131 | each kernel at the terminal, with lines like:: |
|
|||
132 |
|
126 | |||
133 | [IPKernelApp] To connect another client to this kernel, use: |
|
127 | [IPKernelApp] To connect another client to this kernel, use: | |
134 | [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json |
|
128 | [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json | |
135 |
|
129 | |||
136 |
This is the name of a JSON file that contains all the port and |
|
130 | This is the name of a JSON file that contains all the port and | |
137 |
information necessary to connect to the kernel. You can |
|
131 | validation information necessary to connect to the kernel. You can | |
138 |
|
|
132 | manually start a Qt console with:: | |
139 |
|
133 | |||
140 | ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json |
|
134 | ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json | |
141 |
|
135 | |||
142 | and if you only have a single kernel running, simply typing:: |
|
136 | and if you only have a single kernel running, simply typing:: | |
143 |
|
137 | |||
144 | ipython qtconsole --existing |
|
138 | ipython qtconsole --existing | |
145 |
|
139 | |||
146 |
will automatically find it (it will always find the most recently |
|
140 | will automatically find it (it will always find the most recently | |
147 |
kernel if there is more than one). You can also request this |
|
141 | started kernel if there is more than one). You can also request this | |
148 |
by typing ``%connect_info``; this will print the same |
|
142 | connection data by typing ``%connect_info``; this will print the same | |
149 | as the content of the JSON data structure it contains. |
|
143 | file information as well as the content of the JSON data structure it contains. | |
150 |
|
144 | |||
151 |
|
145 | |||
152 | Text input |
|
146 | Text input | |
153 | ---------- |
|
147 | ---------- | |
154 |
|
148 | |||
155 | In addition to code cells and the output they produce (such as figures), you |
|
149 | In addition to code cells and the output they produce (such as figures), you | |
156 | can also type text not meant for execution. To type text, change the type of a |
|
150 | can also type text not meant for execution. To type text, change the type of a | |
157 | cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m` |
|
151 | cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m` | |
158 | keybinding (see below). You can then type any text in Markdown_ syntax, as |
|
152 | keybinding (see below). You can then type any text in Markdown_ syntax, as | |
159 | well as mathematical expressions if you use ``$...$`` for inline math or |
|
153 | well as mathematical expressions if you use ``$...$`` for inline math or | |
160 | ``$$...$$`` for displayed math. |
|
154 | ``$$...$$`` for displayed math. | |
161 |
|
155 | |||
162 |
|
156 | |||
163 | Exporting a notebook and importing existing scripts |
|
157 | Exporting a notebook and importing existing scripts | |
164 | --------------------------------------------------- |
|
158 | --------------------------------------------------- | |
165 |
|
159 | |||
166 | If you want to provide others with a static HTML or PDF view of your notebook, |
|
160 | If you want to provide others with a static HTML or PDF view of your notebook, | |
167 | use the ``Print`` button. This opens a static view of the document, which you |
|
161 | use the ``Print`` button. This opens a static view of the document, which you | |
168 | can print to PDF using your operating system's facilities, or save to a file |
|
162 | can print to PDF using your operating system's facilities, or save to a file | |
169 | with your web browser's 'Save' option (note that typically, this will create |
|
163 | with your web browser's 'Save' option (note that typically, this will create | |
170 | both an html file *and* a directory called `notebook_name_files` next to it |
|
164 | both an html file *and* a directory called `notebook_name_files` next to it | |
171 | that contains all the necessary style information, so if you intend to share |
|
165 | that contains all the necessary style information, so if you intend to share | |
172 | this, you must send the directory along with the main html file). |
|
166 | this, you must send the directory along with the main html file). | |
173 |
|
167 | |||
174 | The `Download` button lets you save a notebook file to the Download area |
|
168 | The `Download` button lets you save a notebook file to the Download area | |
175 | configured by your web browser (particularly useful if you are running the |
|
169 | configured by your web browser (particularly useful if you are running the | |
176 | notebook server on a remote host and need a file locally). The notebook is |
|
170 | notebook server on a remote host and need a file locally). The notebook is | |
177 | saved by default with the ``.ipynb`` extension and the files contain JSON data |
|
171 | saved by default with the ``.ipynb`` extension and the files contain JSON data | |
178 | that is not meant for human editing or consumption. But you can always export |
|
172 | that is not meant for human editing or consumption. But you can always export | |
179 | the input part of a notebook to a plain python script by choosing Python format |
|
173 | the input part of a notebook to a plain python script by choosing Python format | |
180 | in the `Download` drop list. This removes all output and saves the text cells |
|
174 | in the `Download` drop list. This removes all output and saves the text cells | |
181 | in comment areas. See ref:`below <notebook_format>` for more details on the |
|
175 | in comment areas. See ref:`below <notebook_format>` for more details on the | |
182 | notebook format. |
|
176 | notebook format. | |
183 |
|
177 | |||
184 | The notebook can also *import* ``.py`` files as notebooks, by dragging and |
|
|||
185 | dropping the file into the notebook dashboard file list area. By default, the |
|
|||
186 | entire contents of the file will be loaded into a single code cell. But if |
|
|||
187 | prior to import, you manually add the ``# <nbformat>2</nbformat>`` marker at |
|
|||
188 | the start and then add separators for text/code cells, you can get a cleaner |
|
|||
189 | import with the file broken into individual cells. |
|
|||
190 |
|
178 | |||
191 | .. warning:: |
|
179 | .. warning:: | |
192 |
|
180 | |||
193 | While in simple cases you can roundtrip a notebook to Python, edit the |
|
181 | While in simple cases you can roundtrip a notebook to Python, edit the | |
194 | python file and import it back without loss of main content, this is in |
|
182 | python file and import it back without loss of main content, this is in | |
195 | general *not guaranteed to work at all*. First, there is extra metadata |
|
183 | general *not guaranteed to work at all*. First, there is extra metadata | |
196 | saved in the notebook that may not be saved to the ``.py`` format. And as |
|
184 | saved in the notebook that may not be saved to the ``.py`` format. And as | |
197 | the notebook format evolves in complexity, there will be attributes of the |
|
185 | the notebook format evolves in complexity, there will be attributes of the | |
198 | notebook that will not survive a roundtrip through the Python form. You |
|
186 | notebook that will not survive a roundtrip through the Python form. You | |
199 | should think of the Python format as a way to output a script version of a |
|
187 | should think of the Python format as a way to output a script version of a | |
200 | notebook and the import capabilities as a way to load existing code to get a |
|
188 | notebook and the import capabilities as a way to load existing code to get a | |
201 | notebook started. But the Python version is *not* an alternate notebook |
|
189 | notebook started. But the Python version is *not* an alternate notebook | |
202 | format. |
|
190 | format. | |
203 |
|
191 | |||
204 |
|
192 | |||
205 | Importing or executing a notebook as a normal Python file |
|
193 | Importing or executing a notebook as a normal Python file | |
206 | --------------------------------------------------------- |
|
194 | --------------------------------------------------------- | |
207 |
|
195 | |||
208 | The native format of the notebook, a file with a ``.ipynb`` extension, is a |
|
196 | The native format of the notebook, a file with a ``.ipynb`` extension, is a | |
209 | JSON container of all the input and output of the notebook, and therefore not |
|
197 | JSON container of all the input and output of the notebook, and therefore not | |
210 | valid Python by itself. This means that by default, you can not import a |
|
198 | valid Python by itself. This means that by default, you can not import a | |
211 | notebook or execute it as a normal python script. But if you want use |
|
199 | notebook or execute it as a normal python script. But if you want use | |
212 | notebooks as regular Python files, you can start the notebook server with:: |
|
200 | notebooks as regular Python files, you can start the notebook server with:: | |
213 |
|
201 | |||
214 | ipython notebook --script |
|
202 | ipython notebook --script | |
215 |
|
203 | |||
216 | or you can set this option permanently in your configuration file with:: |
|
204 | or you can set this option permanently in your configuration file with:: | |
217 |
|
205 | |||
218 | c.NotebookManager.save_script=True |
|
206 | c.NotebookManager.save_script=True | |
219 |
|
207 | |||
220 | This will instruct the notebook server to save the ``.py`` export of each |
|
208 | This will instruct the notebook server to save the ``.py`` export of each | |
221 | notebook adjacent to the ``.ipynb`` at every save. These files can be |
|
209 | notebook adjacent to the ``.ipynb`` at every save. These files can be | |
222 | ``%run``, imported from regular IPython sessions or other notebooks, or |
|
210 | ``%run``, imported from regular IPython sessions or other notebooks, or | |
223 | executed at the command-line as normal Python files. Since we export the raw |
|
211 | executed at the command-line as normal Python files. Since we export the raw | |
224 | code you have typed, for these files to be importable from other code you will |
|
212 | code you have typed, for these files to be importable from other code you will | |
225 | have to avoid using syntax such as ``%magics`` and other IPython-specific |
|
213 | have to avoid using syntax such as ``%magics`` and other IPython-specific | |
226 | extensions to the language. |
|
214 | extensions to the language. | |
227 |
|
215 | |||
228 | In regular practice, the standard way to differentiate importable code from the |
|
216 | In regular practice, the standard way to differentiate importable code from the | |
229 | 'executable' part of a script is to put at the bottom:: |
|
217 | 'executable' part of a script is to put at the bottom:: | |
230 |
|
218 | |||
231 | if __name__ == '__main__': |
|
219 | if __name__ == '__main__': | |
232 | # rest of the code... |
|
220 | # rest of the code... | |
233 |
|
221 | |||
234 | Since all cells in the notebook are run as top-level code, you'll need to |
|
222 | Since all cells in the notebook are run as top-level code, you'll need to | |
235 | similarly protect *all* cells that you do not want executed when other scripts |
|
223 | similarly protect *all* cells that you do not want executed when other scripts | |
236 | try to import your notebook. A convenient shortand for this is to define early |
|
224 | try to import your notebook. A convenient shortand for this is to define early | |
237 | on:: |
|
225 | on:: | |
238 |
|
226 | |||
239 | script = __name__ == '__main__' |
|
227 | script = __name__ == '__main__' | |
240 |
|
228 | |||
241 | and then on any cell that you need to protect, use:: |
|
229 | and then on any cell that you need to protect, use:: | |
242 |
|
230 | |||
243 | if script: |
|
231 | if script: | |
244 | # rest of the cell... |
|
232 | # rest of the cell... | |
245 |
|
233 | |||
246 | Configuration |
|
234 | Configuration | |
247 | ------------- |
|
235 | ------------- | |
248 |
|
236 | |||
249 | The IPython notebook server can be run with a variety of command line arguments. |
|
237 | The IPython notebook server can be run with a variety of command line arguments. | |
250 | To see a list of available options enter: |
|
238 | To see a list of available options enter: | |
251 |
|
239 | |||
252 | $ ipython notebook --help |
|
240 | $ ipython notebook --help | |
253 |
|
241 | |||
254 | Defaults for these options can also be set by creating a file named |
|
242 | Defaults for these options can also be set by creating a file named | |
255 | ipython_notebook_config.py in your IPython profile folder. The profile folder is |
|
243 | ipython_notebook_config.py in your IPython profile folder. The profile folder is | |
256 | a subfolder of your IPython directory (`ipython locate` will show you where that |
|
244 | a subfolder of your IPython directory (`ipython locate` will show you where that | |
257 | is). To create default configuration files (with lots of info on available |
|
245 | is). To create default configuration files (with lots of info on available | |
258 | options) use: |
|
246 | options) use: | |
259 |
|
247 | |||
260 | $ ipython profile create |
|
248 | $ ipython profile create | |
261 |
|
249 | |||
262 | .. seealso: |
|
250 | .. seealso: | |
263 |
|
251 | |||
264 | :ref:`config_overview`, in particular :ref:`Profiles`. |
|
252 | :ref:`config_overview`, in particular :ref:`Profiles`. | |
265 |
|
253 | |||
266 |
|
254 | |||
267 | Keyboard use |
|
255 | Keyboard use | |
268 | ------------ |
|
256 | ------------ | |
269 |
|
257 | |||
270 | All actions in the notebook can be achieved with the mouse, but we have also |
|
258 | All actions in the notebook can be achieved with the mouse, but we have also | |
271 | added keyboard shortcuts for the most common ones, so that productive use of |
|
259 | added keyboard shortcuts for the most common ones, so that productive use of | |
272 | the notebook can be achieved with minimal mouse intervention. The main |
|
260 | the notebook can be achieved with minimal mouse intervention. The main | |
273 | key bindings you need to remember are: |
|
261 | key bindings you need to remember are: | |
274 |
|
262 | |||
275 | * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console), |
|
263 | * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console), | |
276 | show output (if any) and jump to the next cell below. If :kbd:`Shift-Enter` |
|
264 | show output (if any) and jump to the next cell below. If :kbd:`Shift-Enter` | |
277 | was invoked on the last input line, a new code cell will also be created. Note |
|
265 | was invoked on the last input line, a new code cell will also be created. Note | |
278 | that in the notebook, simply using :kbd:`Enter` *never* forces execution, |
|
266 | that in the notebook, simply using :kbd:`Enter` *never* forces execution, | |
279 | it simply inserts a new line in the current cell. Therefore, in the notebook |
|
267 | it simply inserts a new line in the current cell. Therefore, in the notebook | |
280 | you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and |
|
268 | you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and | |
281 | click on the ``Run Selected`` button). |
|
269 | click on the ``Run Selected`` button). | |
282 |
|
270 | |||
283 | * :kbd:`Alt-Enter`: this combination is similar to the previous one, with the |
|
271 | * :kbd:`Alt-Enter`: this combination is similar to the previous one, with the | |
284 | exception that, if the next cell below is not empty, a new code cell will be |
|
272 | exception that, if the next cell below is not empty, a new code cell will be | |
285 | added to the notebook, even if the cell execution happens not in the last cell. |
|
273 | added to the notebook, even if the cell execution happens not in the last cell. | |
286 | In this regard, :kbd:`Alt-Enter`: is simply a shortcut for the :kbd:`Shift-Enter`, |
|
274 | In this regard, :kbd:`Alt-Enter`: is simply a shortcut for the :kbd:`Shift-Enter`, | |
287 | :kbd:`Ctrl-m a` sequence. |
|
275 | :kbd:`Ctrl-m a` sequence. | |
288 |
|
276 | |||
289 | * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any |
|
277 | * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any | |
290 | output is shown but the cursor stays in the current cell, whose input |
|
278 | output is shown but the cursor stays in the current cell, whose input | |
291 | area is flushed empty. This is convenient to do quick in-place experiments |
|
279 | area is flushed empty. This is convenient to do quick in-place experiments | |
292 | or query things like filesystem content without creating additional cells you |
|
280 | or query things like filesystem content without creating additional cells you | |
293 | may not want saved in your notebook. |
|
281 | may not want saved in your notebook. | |
294 |
|
282 | |||
295 | * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist |
|
283 | * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist | |
296 | of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole |
|
284 | of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole | |
297 | letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining |
|
285 | letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining | |
298 | available keybindings. |
|
286 | available keybindings. | |
299 |
|
287 | |||
300 |
|
288 | |||
301 | .. _notebook_security: |
|
289 | .. _notebook_security: | |
302 |
|
290 | |||
303 | Security |
|
291 | Security | |
304 | ======== |
|
292 | ======== | |
305 |
|
293 | |||
306 | You can protect your notebook server with a simple single-password by |
|
294 | You can protect your notebook server with a simple single-password by | |
307 | setting the :attr:`NotebookApp.password` configurable. You can prepare a |
|
295 | setting the :attr:`NotebookApp.password` configurable. You can prepare a | |
308 | hashed password using the function :func:`IPython.lib.security.passwd`: |
|
296 | hashed password using the function :func:`IPython.lib.security.passwd`: | |
309 |
|
297 | |||
310 | .. sourcecode:: ipython |
|
298 | .. sourcecode:: ipython | |
311 |
|
299 | |||
312 | In [1]: from IPython.lib import passwd |
|
300 | In [1]: from IPython.lib import passwd | |
313 | In [2]: passwd() |
|
301 | In [2]: passwd() | |
314 | Enter password: |
|
302 | Enter password: | |
315 | Verify password: |
|
303 | Verify password: | |
316 | Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' |
|
304 | Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' | |
317 |
|
305 | |||
318 | .. note:: |
|
306 | .. note:: | |
319 |
|
307 | |||
320 | :func:`~IPython.lib.security.passwd` can also take the password as a string |
|
308 | :func:`~IPython.lib.security.passwd` can also take the password as a string | |
321 | argument. **Do not** pass it as an argument inside an IPython session, as it |
|
309 | argument. **Do not** pass it as an argument inside an IPython session, as it | |
322 | will be saved in your input history. |
|
310 | will be saved in your input history. | |
323 |
|
311 | |||
324 | You can then add this to your :file:`ipython_notebook_config.py`, e.g.:: |
|
312 | You can then add this to your :file:`ipython_notebook_config.py`, e.g.:: | |
325 |
|
313 | |||
326 | # Password to use for web authentication |
|
314 | # Password to use for web authentication | |
327 | c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' |
|
315 | c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' | |
328 |
|
316 | |||
329 | When using a password, it is a good idea to also use SSL, so that your password |
|
317 | When using a password, it is a good idea to also use SSL, so that your password | |
330 | is not sent unencrypted by your browser. You can start the notebook to |
|
318 | is not sent unencrypted by your browser. You can start the notebook to | |
331 | communicate via a secure protocol mode using a self-signed certificate by |
|
319 | communicate via a secure protocol mode using a self-signed certificate by | |
332 | typing:: |
|
320 | typing:: | |
333 |
|
321 | |||
334 | $ ipython notebook --certfile=mycert.pem |
|
322 | $ ipython notebook --certfile=mycert.pem | |
335 |
|
323 | |||
336 | .. note:: |
|
324 | .. note:: | |
337 |
|
325 | |||
338 | A self-signed certificate can be generated with openssl. For example, the |
|
326 | A self-signed certificate can be generated with openssl. For example, the | |
339 | following command will create a certificate valid for 365 days with both |
|
327 | following command will create a certificate valid for 365 days with both | |
340 | the key and certificate data written to the same file:: |
|
328 | the key and certificate data written to the same file:: | |
341 |
|
329 | |||
342 | $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem |
|
330 | $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem | |
343 |
|
331 | |||
344 | Your browser will warn you of a dangerous certificate because it is |
|
332 | Your browser will warn you of a dangerous certificate because it is | |
345 | self-signed. If you want to have a fully compliant certificate that will not |
|
333 | self-signed. If you want to have a fully compliant certificate that will not | |
346 | raise warnings, it is possible (but rather involved) to obtain one for free, |
|
334 | raise warnings, it is possible (but rather involved) to obtain one for free, | |
347 | `as explained in detailed in this tutorial`__. |
|
335 | `as explained in detailed in this tutorial`__. | |
348 |
|
336 | |||
349 | .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars |
|
337 | .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars | |
350 |
|
338 | |||
351 | Keep in mind that when you enable SSL support, you'll need to access the |
|
339 | Keep in mind that when you enable SSL support, you'll need to access the | |
352 | notebook server over ``https://``, not over plain ``http://``. The startup |
|
340 | notebook server over ``https://``, not over plain ``http://``. The startup | |
353 | message from the server prints this, but it's easy to overlook and think the |
|
341 | message from the server prints this, but it's easy to overlook and think the | |
354 | server is for some reason non-responsive. |
|
342 | server is for some reason non-responsive. | |
355 |
|
343 | |||
356 | Quick how to's |
|
344 | Quick how to's | |
357 | ============== |
|
345 | ============== | |
358 |
|
346 | |||
359 | Running a public notebook server |
|
347 | Running a public notebook server | |
360 | -------------------------------- |
|
348 | -------------------------------- | |
361 |
|
349 | |||
362 | If you want to access your notebook server remotely with just a web browser, |
|
350 | If you want to access your notebook server remotely with just a web browser, | |
363 | here is a quick set of instructions. Start by creating a certificate file and |
|
351 | here is a quick set of instructions. Start by creating a certificate file and | |
364 | a hashed password as explained above. Then, create a custom profile for the |
|
352 | a hashed password as explained above. Then, create a custom profile for the | |
365 | notebook. At the command line, type:: |
|
353 | notebook. At the command line, type:: | |
366 |
|
354 | |||
367 | ipython profile create nbserver |
|
355 | ipython profile create nbserver | |
368 |
|
356 | |||
369 | In the profile directory, edit the file ``ipython_notebook_config.py``. By |
|
357 | In the profile directory, edit the file ``ipython_notebook_config.py``. By | |
370 | default the file has all fields commented, the minimum set you need to |
|
358 | default the file has all fields commented, the minimum set you need to | |
371 | uncomment and edit is here:: |
|
359 | uncomment and edit is here:: | |
372 |
|
360 | |||
373 | c = get_config() |
|
361 | c = get_config() | |
374 |
|
362 | |||
375 | # Kernel config |
|
363 | # Kernel config | |
376 | c.IPKernelApp.pylab = 'inline' # if you want plotting support always |
|
364 | c.IPKernelApp.pylab = 'inline' # if you want plotting support always | |
377 |
|
365 | |||
378 | # Notebook config |
|
366 | # Notebook config | |
379 | c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem' |
|
367 | c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem' | |
380 | c.NotebookApp.ip = '*' |
|
368 | c.NotebookApp.ip = '*' | |
381 | c.NotebookApp.open_browser = False |
|
369 | c.NotebookApp.open_browser = False | |
382 | c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here' |
|
370 | c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here' | |
383 | # It's a good idea to put it on a known, fixed port |
|
371 | # It's a good idea to put it on a known, fixed port | |
384 | c.NotebookApp.port = 9999 |
|
372 | c.NotebookApp.port = 9999 | |
385 |
|
373 | |||
386 | You can then start the notebook and access it later by pointing your browser to |
|
374 | You can then start the notebook and access it later by pointing your browser to | |
387 | ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``. |
|
375 | ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``. | |
388 |
|
376 | |||
389 | Running with a different URL prefix |
|
377 | Running with a different URL prefix | |
390 | ----------------------------------- |
|
378 | ----------------------------------- | |
391 |
|
379 | |||
392 | The notebook dashboard (i.e. the default landing page with an overview |
|
380 | The notebook dashboard (i.e. the default landing page with an overview | |
393 | of all your notebooks) typically lives at a URL path of |
|
381 | of all your notebooks) typically lives at a URL path of | |
394 | "http://localhost:8888/". If you want to have it, and the rest of the |
|
382 | "http://localhost:8888/". If you want to have it, and the rest of the | |
395 | notebook, live under a sub-directory, |
|
383 | notebook, live under a sub-directory, | |
396 | e.g. "http://localhost:8888/ipython/", you can do so with |
|
384 | e.g. "http://localhost:8888/ipython/", you can do so with | |
397 | configuration options like these (see above for instructions about |
|
385 | configuration options like these (see above for instructions about | |
398 | modifying ``ipython_notebook_config.py``):: |
|
386 | modifying ``ipython_notebook_config.py``):: | |
399 |
|
387 | |||
400 | c.NotebookApp.base_project_url = '/ipython/' |
|
388 | c.NotebookApp.base_project_url = '/ipython/' | |
401 | c.NotebookApp.base_kernel_url = '/ipython/' |
|
389 | c.NotebookApp.base_kernel_url = '/ipython/' | |
402 | c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'} |
|
390 | c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'} | |
403 |
|
391 | |||
404 | Using a different notebook store |
|
392 | Using a different notebook store | |
405 | -------------------------------- |
|
393 | -------------------------------- | |
406 |
|
394 | |||
407 | By default the notebook server stores notebooks as files in the working |
|
395 | By default the notebook server stores notebooks as files in the working | |
408 | directory of the notebook server, also known as the ``notebook_dir``. This |
|
396 | directory of the notebook server, also known as the ``notebook_dir``. This | |
409 | logic is implemented in the :class:`FileNotebookManager` class. However, the |
|
397 | logic is implemented in the :class:`FileNotebookManager` class. However, the | |
410 | server can be configured to use a different notebook manager class, which can |
|
398 | server can be configured to use a different notebook manager class, which can | |
411 | store the notebooks in a different format. Currently, we ship a |
|
399 | store the notebooks in a different format. Currently, we ship a | |
412 | :class:`AzureNotebookManager` class that stores notebooks in Azure blob |
|
400 | :class:`AzureNotebookManager` class that stores notebooks in Azure blob | |
413 | storage. This can be used by adding the following lines to your |
|
401 | storage. This can be used by adding the following lines to your | |
414 | ``ipython_notebook_config.py`` file:: |
|
402 | ``ipython_notebook_config.py`` file:: | |
415 |
|
403 | |||
416 | c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager' |
|
404 | c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager' | |
417 | c.AzureNotebookManager.account_name = u'paste_your_account_name_here' |
|
405 | c.AzureNotebookManager.account_name = u'paste_your_account_name_here' | |
418 | c.AzureNotebookManager.account_key = u'paste_your_account_key_here' |
|
406 | c.AzureNotebookManager.account_key = u'paste_your_account_key_here' | |
419 | c.AzureNotebookManager.container = u'notebooks' |
|
407 | c.AzureNotebookManager.container = u'notebooks' | |
420 |
|
408 | |||
421 | In addition to providing your Azure Blob Storage account name and key, you will |
|
409 | In addition to providing your Azure Blob Storage account name and key, you will | |
422 | have to provide a container name; you can use multiple containers to organize |
|
410 | have to provide a container name; you can use multiple containers to organize | |
423 | your Notebooks. |
|
411 | your Notebooks. | |
424 |
|
412 | |||
425 | .. _notebook_format: |
|
413 | .. _notebook_format: | |
426 |
|
414 | |||
427 | The notebook format |
|
415 | The notebook format | |
428 | =================== |
|
416 | =================== | |
429 |
|
417 | |||
430 | The notebooks themselves are JSON files with an ``ipynb`` extension, formatted |
|
418 | The notebooks themselves are JSON files with an ``ipynb`` extension, formatted | |
431 | as legibly as possible with minimal extra indentation and cell content broken |
|
419 | as legibly as possible with minimal extra indentation and cell content broken | |
432 | across lines to make them reasonably friendly to use in version-control |
|
420 | across lines to make them reasonably friendly to use in version-control | |
433 | workflows. You should be very careful if you ever edit manually this JSON |
|
421 | workflows. You should be very careful if you ever edit manually this JSON | |
434 | data, as it is extremely easy to corrupt its internal structure and make the |
|
422 | data, as it is extremely easy to corrupt its internal structure and make the | |
435 | file impossible to load. In general, you should consider the notebook as a |
|
423 | file impossible to load. In general, you should consider the notebook as a | |
436 | file meant only to be edited by IPython itself, not for hand-editing. |
|
424 | file meant only to be edited by IPython itself, not for hand-editing. | |
437 |
|
425 | |||
438 | .. note:: |
|
426 | .. note:: | |
439 |
|
427 | |||
440 | Binary data such as figures are directly saved in the JSON file. This |
|
428 | Binary data such as figures are directly saved in the JSON file. This | |
441 | provides convenient single-file portability but means the files can be |
|
429 | provides convenient single-file portability but means the files can be | |
442 | large and diffs of binary data aren't very meaningful. Since the binary |
|
430 | large and diffs of binary data aren't very meaningful. Since the binary | |
443 | blobs are encoded in a single line they only affect one line of the diff |
|
431 | blobs are encoded in a single line they only affect one line of the diff | |
444 | output, but they are typically very long lines. You can use the |
|
432 | output, but they are typically very long lines. You can use the | |
445 | 'ClearAll' button to remove all output from a notebook prior to |
|
433 | 'ClearAll' button to remove all output from a notebook prior to | |
446 | committing it to version control, if this is a concern. |
|
434 | committing it to version control, if this is a concern. | |
447 |
|
435 | |||
448 | The notebook server can also generate a pure-python version of your notebook, |
|
436 | The notebook server can also generate a pure-python version of your notebook, | |
449 | by clicking on the 'Download' button and selecting ``py`` as the format. This |
|
437 | by clicking on the 'Download' button and selecting ``py`` as the format. This | |
450 | file will contain all the code cells from your notebook verbatim, and all text |
|
438 | file will contain all the code cells from your notebook verbatim, and all text | |
451 | cells prepended with a comment marker. The separation between code and text |
|
439 | cells prepended with a comment marker. The separation between code and text | |
452 | cells is indicated with special comments and there is a header indicating the |
|
440 | cells is indicated with special comments and there is a header indicating the | |
453 | format version. All output is stripped out when exporting to python. |
|
441 | format version. All output is stripped out when exporting to python. | |
454 |
|
442 | |||
455 | Here is an example of a simple notebook with one text cell and one code input |
|
443 | Here is an example of a simple notebook with one text cell and one code input | |
456 | cell, when exported to python format:: |
|
444 | cell, when exported to python format:: | |
457 |
|
445 | |||
458 | # <nbformat>2</nbformat> |
|
446 | # <nbformat>2</nbformat> | |
459 |
|
447 | |||
460 | # <markdowncell> |
|
448 | # <markdowncell> | |
461 |
|
449 | |||
462 | # A text cell |
|
450 | # A text cell | |
463 |
|
451 | |||
464 | # <codecell> |
|
452 | # <codecell> | |
465 |
|
453 | |||
466 | print "hello IPython" |
|
454 | print "hello IPython" | |
467 |
|
455 | |||
468 |
|
456 | |||
469 | Known issues |
|
457 | Known issues | |
470 | ============ |
|
458 | ============ | |
471 |
|
459 | |||
472 | When behind a proxy, especially if your system or browser is set to autodetect |
|
460 | When behind a proxy, especially if your system or browser is set to autodetect | |
473 | the proxy, the html notebook might fail to connect to the server's websockets, |
|
461 | the proxy, the html notebook might fail to connect to the server's websockets, | |
474 | and present you with a warning at startup. In this case, you need to configure |
|
462 | and present you with a warning at startup. In this case, you need to configure | |
475 | your system not to use the proxy for the server's address. |
|
463 | your system not to use the proxy for the server's address. | |
476 |
|
464 | |||
477 | In Firefox, for example, go to the Preferences panel, Advanced section, |
|
465 | In Firefox, for example, go to the Preferences panel, Advanced section, | |
478 | Network tab, click 'Settings...', and add the address of the notebook server |
|
466 | Network tab, click 'Settings...', and add the address of the notebook server | |
479 | to the 'No proxy for' field. |
|
467 | to the 'No proxy for' field. | |
480 |
|
468 | |||
481 |
|
469 | |||
482 | .. _Markdown: http://daringfireball.net/projects/markdown/basics |
|
470 | .. _Markdown: http://daringfireball.net/projects/markdown/basics |
General Comments 0
You need to be logged in to leave comments.
Login now