##// END OF EJS Templates
Document notebook format and quick howto for a public nb server....
Fernando Perez -
Show More
@@ -28,7 +28,7 b' which will behave similar to the terminal and Qt console versions, using your'
28 28 default matplotlib backend and providing floating interactive plot windows. If
29 29 you want inline figures, you must manually select the ``inline`` backend::
30 30
31 $ ipython notebook --pylab=inline
31 $ ipython notebook --pylab inline
32 32
33 33 This server uses the same ZeroMQ-based two process kernel architecture as
34 34 the QT Console as well Tornado for serving HTTP/S requests. Some of the main
@@ -64,18 +64,18 b' in which the application was started, and allows you to create new notebooks.'
64 64 A notebook is a combination of two things:
65 65
66 66 1. An interactive session connected to an IPython kernel, controlled by a web
67 application that can send input to the console and display many types of output
68 (text, graphics, mathematics and more). This is the same kernel used by the
69 :ref:`Qt console <qtconsole>`, but in this case the web console sends input in
70 persistent cells that you can edit in-place instead of the vertically scrolling
71 terminal style used by the Qt console.
67 application that can send input to the console and display many types of
68 output (text, graphics, mathematics and more). This is the same kernel used
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 72
73 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. In
75 this way, notebook files serve as a complete computational record of a session
76 including explanatory text and mathematics, code and resulting figures. These
77 documents are internally JSON files and are saved with the ``.ipynb``
78 extension.
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 79
80 80 If you have ever used the Mathematica or Sage notebooks (the latter is also
81 81 web-based__) you should feel right at home. If you have not, you should be
@@ -100,6 +100,7 b' will consist of a single cell with all the code in the file, which you can'
100 100 later manually partition into individual cells for gradual execution, add text
101 101 and graphics, etc.
102 102
103
103 104 Workflow and limitations
104 105 ------------------------
105 106
@@ -158,8 +159,9 b' keybinding (see below). You can then type any text in Markdown_ syntax, as'
158 159 well as mathematical expressions if you use ``$...$`` for inline math or
159 160 ``$$...$$`` for displayed math.
160 161
161 Exporting a notebook
162 --------------------
162
163 Exporting a notebook and importing existing scripts
164 ---------------------------------------------------
163 165
164 166 If you want to provide others with a static HTML or PDF view of your notebook,
165 167 use the ``Print`` button. This opens a static view of the document, which you
@@ -176,18 +178,28 b' saved by default with the ``.ipynb`` extension and the files contain JSON data'
176 178 that is not meant for human editing or consumption. But you can always export
177 179 the input part of a notebook to a plain python script by choosing Python format
178 180 in the `Download` drop list. This removes all output and saves the text cells
179 in comment areas.
181 in comment areas. See ref:`below <notebook_format>` for more details on the
182 notebook format.
183
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.
180 190
181 191 .. warning::
182 192
183 193 While in simple cases you can roundtrip a notebook to Python, edit the
184 python file and import it back without loss, this is in general *not
185 guaranteed to work at all*. As the notebook format evolves in complexity,
186 there will be attributes of the notebook that will not survive a roundtrip
187 through the Python form. You should think of the Python format as a way to
188 output a script version of a notebook and the import capabilities as a way
189 to load existing code to get a notebook started. But the Python version is
190 *not* an alternate notebook format.
194 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
196 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
198 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
200 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
202 format.
191 203
192 204
193 205 Keyboard use
@@ -216,6 +228,9 b' key bindings you need to remember are:'
216 228 letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
217 229 available keybindings.
218 230
231
232 .. _notebook_security:
233
219 234 Security
220 235 ========
221 236
@@ -243,17 +258,107 b' You can then add this to your :file:`ipython_notebook_config.py`, e.g.::'
243 258 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
244 259
245 260 When using a password, it is a good idea to also use SSL, so that your password
246 is not sent in the clear. You can start the notebook to communicate via a secure
247 protocol mode using a self-signed certificate by typing::
261 is not sent unencripted by your browser. You can start the notebook to
262 communicate via a secure protocol mode using a self-signed certificate by
263 typing::
248 264
249 265 $ ipython notebook --certfile=mycert.pem
250 266
251 267 .. note::
252 268
253 A self-signed certificate can be generated with openssl. For example::
269 A self-signed certificate can be generated with openssl. For example, the
270 following command will create a certificate valid for 365 days with both
271 the key and certificate data written to the same file::
254 272
255 273 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
256 274
275 Your browser will warn you of a dangerous certificate because it is
276 self-signed. If you want to have a fully compliant certificate that will not
277 raise warnings, it is possible (but rather involved) to obtain one for free,
278 `as explained in detailed in this tutorial`__.
279
280 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
281
282 Keep in mind that when you enable SSL support, you'll need to access the
283 notebook server over ``https://``, not over plain ``http://``. The startup
284 message from the server prints this, but it's easy to overlook and think the
285 server is for some reason non-responsive.
286
287
288 Quick Howto: running a public notebook server
289 =============================================
290
291 If you want to access your notebook server remotely with just a web browser,
292 here is a quick set of instructions. Start by creating a certificate file and
293 a hashed password as explained above. Then, create a custom profile for the
294 notebook. At the command line, type::
295
296 ipython profile create nbserver
297
298 In the profile directory, edit the file ``ipython_notebook_config.py``. By
299 default the file has all fields commented, the minimum set you need to
300 uncomment and edit is here::
301
302 c = get_config()
303
304 # Kernel config
305 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
306
307 # Notebook config
308 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
309 c.NotebookApp.ip = '*'
310 c.NotebookApp.open_browser = False
311 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
312 # It's a good idea to put it on a known, fixed port
313 c.NotebookApp.port = 9999
314
315 You can then start the notebook and access it later by pointing your browser to
316 ``https://your.host.com:9999``.
317
318 .. _notebook_format:
319
320 The notebook format
321 ===================
322
323 The notebooks themselves are JSON files with an ``ipynb`` extension, formatted
324 as legibly as possible with minimal extra indentation and cell content broken
325 across lines to make them reasonably friendly to use in version-control
326 workflows. You should be very careful if you ever edit manually this JSON
327 data, as it is extremely easy to corrupt its internal structure and make the
328 file impossible to load. In general, you should consider the notebook as a
329 file meant only to be edited by IPython itself, not for hand-editing.
330
331 .. note::
332
333 Binary data such as figures are directly saved in the JSON file. This
334 provides convenient single-file portability but means the files can be
335 large and diffs of binary data aren't very meaningful. Since the binary
336 blobs are encoded in a single line they only affect one line of the diff
337 output, but they are typically very long lines. You can use the
338 'ClearAll' button to remove all output from a notebook prior to
339 committing it to version control, if this is a concern.
340
341 The notebook server can also generate a pure-python version of your notebook,
342 by clicking on the 'Download' button and selecting ``py`` as the format. This
343 file will contain all the code cells from your notebook verbatim, and all text
344 cells prepended with a comment marker. The separation between code and text
345 cells is indicated with special comments and there is a header indicating the
346 format version. All output is stripped out when exporting to python.
347
348 Here is an example of a simple notebook with one text cell and one code input
349 cell, when exported to python format::
350
351 # <nbformat>2</nbformat>
352
353 # <markdowncell>
354
355 # A text cell
356
357 # <codecell>
358
359 print "hello IPython"
360
361
257 362 Known Issues
258 363 ============
259 364
@@ -266,8 +371,5 b' In Firefox, for example, go to the Preferences panel, Advanced section,'
266 371 Network tab, click 'Settings...', and add the address of the notebook server
267 372 to the 'No proxy for' field.
268 373
269 Notebook document format
270 ========================
271
272
374
273 375 .. _Markdown: http://daringfireball.net/projects/markdown/basics
General Comments 0
You need to be logged in to leave comments. Login now