Show More
@@ -1,218 +1,234 b'' | |||||
1 | .. _htmlnotebook: |
|
1 | .. _htmlnotebook: | |
2 |
|
2 | |||
3 | ========================= |
|
3 | ========================= | |
4 | An HTML Notebook IPython |
|
4 | An HTML Notebook IPython | |
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 | * An JSON based Notebook document format for recording and distributing | |
14 | Python code and rich text. |
|
14 | Python code and rich text. | |
15 | * A web-based user interface for authoring and running notebook documents. |
|
15 | * A web-based user interface for authoring and running notebook documents. | |
16 |
|
16 | |||
17 | The Notebook can be used by starting the Notebook server with the |
|
17 | The Notebook can be used by starting the Notebook server with the | |
18 | command:: |
|
18 | command:: | |
19 |
|
19 | |||
20 | $ ipython notebook |
|
20 | $ ipython notebook | |
21 |
|
21 | |||
22 | Note that by default, the notebook doesn't load pylab, it's just a normal |
|
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:: |
|
23 | IPython session like any other. If you want pylab support, you must use:: | |
24 |
|
24 | |||
25 | $ ipython notebook --pylab |
|
25 | $ ipython notebook --pylab | |
26 |
|
26 | |||
27 | which will behave similar to the terminal and Qt console versions, using your |
|
27 | which will behave similar to the terminal and Qt console versions, using your | |
28 | default matplotlib backend and providing floating interactive plot windows. If |
|
28 | default matplotlib backend and providing floating interactive plot windows. If | |
29 | you want inline figures, you must manually select the ``inline`` backend:: |
|
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 | You can start the notebook to communicate via a secure protocol mode using a |
|
33 | You can start the notebook to communicate via a secure protocol mode using a | |
34 | self-signed certificate by typing:: |
|
34 | self-signed certificate by typing:: | |
35 |
|
35 | |||
36 | $ ipython notebook --certfile=mycert.pem |
|
36 | $ ipython notebook --certfile=mycert.pem | |
37 |
|
37 | |||
38 | .. note:: |
|
38 | .. note:: | |
39 |
|
39 | |||
40 | A self-signed certificate can be generated with openssl. For example: |
|
40 | A self-signed certificate can be generated with openssl. For example: | |
41 |
|
41 | |||
42 | openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem |
|
42 | openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem | |
43 |
|
43 | |||
44 | This server uses the same ZeroMQ-based two process kernel architecture as |
|
44 | This server uses the same ZeroMQ-based two process kernel architecture as | |
45 | the QT Console as well Tornado for serving HTTP/S requests. Some of the main |
|
45 | the QT Console as well Tornado for serving HTTP/S requests. Some of the main | |
46 | features of the Notebook include: |
|
46 | features of the Notebook include: | |
47 |
|
47 | |||
48 | * Display rich data (png/html/latex/svg) in the browser as a result of |
|
48 | * Display rich data (png/html/latex/svg) in the browser as a result of | |
49 | computations. |
|
49 | computations. | |
50 | * Compose text cells using HTML and Markdown. |
|
50 | * Compose text cells using HTML and Markdown. | |
51 | * Import and export notebook documents in range of formats (.ipynb, .py). |
|
51 | * Import and export notebook documents in range of formats (.ipynb, .py). | |
52 | * In browser syntax highlighting, tab completion and autoindentation. |
|
52 | * In browser syntax highlighting, tab completion and autoindentation. | |
53 | * Inline matplotlib plots that can be stored in Notebook documents and opened |
|
53 | * Inline matplotlib plots that can be stored in Notebook documents and opened | |
54 | later. |
|
54 | later. | |
55 |
|
55 | |||
56 | See :ref:`our installation documentation <install_index>` for directions on |
|
56 | See :ref:`our installation documentation <install_index>` for directions on | |
57 | how to install the notebook and its dependencies. |
|
57 | how to install the notebook and its dependencies. | |
58 |
|
58 | |||
59 | .. note:: |
|
59 | .. note:: | |
60 |
|
60 | |||
61 | You can start more than one notebook server at the same time, if you want to |
|
61 | You can start more than one notebook server at the same time, if you want to | |
62 | work on notebooks in different directories. By default the first notebook |
|
62 | work on notebooks in different directories. By default the first notebook | |
63 | server starts in port 8888, later notebooks search for random ports near |
|
63 | server starts in port 8888, later notebooks search for random ports near | |
64 | that one. You can also manually specify the port with the ``--port`` |
|
64 | that one. You can also manually specify the port with the ``--port`` | |
65 | option. |
|
65 | option. | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | Basic Usage |
|
68 | Basic Usage | |
69 | =========== |
|
69 | =========== | |
70 |
|
70 | |||
71 | The landing page of the notebook server application, which we call the IPython |
|
71 | The landing page of the notebook server application, which we call the IPython | |
72 | Notebook *dashboard*, shows the notebooks currently available in the directory |
|
72 | Notebook *dashboard*, shows the notebooks currently available in the directory | |
73 | in which the application was started, and allows you to create new notebooks. |
|
73 | in which the application was started, and allows you to create new notebooks. | |
74 |
|
74 | |||
75 | A notebook is a combination of two things: |
|
75 | A notebook is a combination of two things: | |
76 |
|
76 | |||
77 | 1. An interactive session connected to an IPython kernel, controlled by a web |
|
77 | 1. An interactive session connected to an IPython kernel, controlled by a web | |
78 | application that can send input to the console and display many types of output |
|
78 | application that can send input to the console and display many types of output | |
79 | (text, graphics, mathematics and more). This is the same kernel used by the |
|
79 | (text, graphics, mathematics and more). This is the same kernel used by the | |
80 | :ref:`Qt console <qtconsole>`, but in this case the web console sends input in |
|
80 | :ref:`Qt console <qtconsole>`, but in this case the web console sends input in | |
81 | persistent cells that you can edit in-place instead of the vertically scrolling |
|
81 | persistent cells that you can edit in-place instead of the vertically scrolling | |
82 | terminal style used by the Qt console. |
|
82 | terminal style used by the Qt console. | |
83 |
|
83 | |||
84 | 2. A document that can save the inputs and outputs of the session as well as |
|
84 | 2. A document that can save the inputs and outputs of the session as well as | |
85 | additional text that accompanies the code but is not meant for execution. In |
|
85 | additional text that accompanies the code but is not meant for execution. In | |
86 | this way, notebook files serve as a complete computational record of a session |
|
86 | this way, notebook files serve as a complete computational record of a session | |
87 | including explanatory text and mathematics, code and resulting figures. These |
|
87 | including explanatory text and mathematics, code and resulting figures. These | |
88 | documents are internally JSON files and are saved with the ``.ipynb`` |
|
88 | documents are internally JSON files and are saved with the ``.ipynb`` | |
89 | extension. |
|
89 | extension. | |
90 |
|
90 | |||
91 | If you have ever used the Mathematica or Sage notebooks (the latter is also |
|
91 | If you have ever used the Mathematica or Sage notebooks (the latter is also | |
92 | web-based__) you should feel right at home. If you have not, you should be |
|
92 | web-based__) you should feel right at home. If you have not, you should be | |
93 | able to learn how to use it in just a few minutes. |
|
93 | able to learn how to use it in just a few minutes. | |
94 |
|
94 | |||
95 | .. __: http://sagenb.org |
|
95 | .. __: http://sagenb.org | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | Creating and editing notebooks |
|
98 | Creating and editing notebooks | |
99 | ------------------------------ |
|
99 | ------------------------------ | |
100 |
|
100 | |||
101 | You can create new notebooks from the dashboard with the ``New Notebook`` |
|
101 | You can create new notebooks from the dashboard with the ``New Notebook`` | |
102 | button or open existing ones by clicking on their name. Once in a notebook, |
|
102 | button or open existing ones by clicking on their name. Once in a notebook, | |
103 | your browser tab will reflect the name of that notebook (prefixed with "IPy:"). |
|
103 | your browser tab will reflect the name of that notebook (prefixed with "IPy:"). | |
104 | The URL for that notebook is not meant to be human-readable and is *not* |
|
104 | The URL for that notebook is not meant to be human-readable and is *not* | |
105 | persistent across invocations of the notebook server. |
|
105 | persistent across invocations of the notebook server. | |
106 |
|
106 | |||
107 | You can also drag and drop into the area listing files any python file: it |
|
107 | You can also drag and drop into the area listing files any python file: it | |
108 | will be imported into a notebook with the same name (but ``.ipynb`` extension) |
|
108 | will be imported into a notebook with the same name (but ``.ipynb`` extension) | |
109 | located in the directory where the notebook server was started. This notebook |
|
109 | located in the directory where the notebook server was started. This notebook | |
110 | will consist of a single cell with all the code in the file, which you can |
|
110 | will consist of a single cell with all the code in the file, which you can | |
111 | later manually partition into individual cells for gradual execution, add text |
|
111 | later manually partition into individual cells for gradual execution, add text | |
112 | and graphics, etc. |
|
112 | and graphics, etc. | |
113 |
|
113 | |||
114 | Workflow and limitations |
|
114 | Workflow and limitations | |
115 | ------------------------ |
|
115 | ------------------------ | |
116 |
|
116 | |||
117 | The normal workflow in a notebook is quite similar to a normal IPython session, |
|
117 | The normal workflow in a notebook is quite similar to a normal IPython session, | |
118 | with the difference that you can edit a cell in-place multiple times until you |
|
118 | with the difference that you can edit a cell in-place multiple times until you | |
119 | obtain the desired results rather than having to rerun separate scripts with |
|
119 | obtain the desired results rather than having to rerun separate scripts with | |
120 | the ``%run`` magic (though magics also work in the notebook). Typically |
|
120 | the ``%run`` magic (though magics also work in the notebook). Typically | |
121 | you'll work on a problem in pieces, organizing related pieces into cells and |
|
121 | you'll work on a problem in pieces, organizing related pieces into cells and | |
122 | moving forward as previous parts work correctly. This is much more convenient |
|
122 | moving forward as previous parts work correctly. This is much more convenient | |
123 | for interactive exploration than breaking up a computation into scripts that |
|
123 | for interactive exploration than breaking up a computation into scripts that | |
124 | must be executed together, especially if parts of them take a long time to run |
|
124 | must be executed together, especially if parts of them take a long time to run | |
125 | (In the traditional terminal-based IPython, you can use tricks with namespaces |
|
125 | (In the traditional terminal-based IPython, you can use tricks with namespaces | |
126 | and ``%run -i`` to achieve this capability, but we think the notebook is a more |
|
126 | and ``%run -i`` to achieve this capability, but we think the notebook is a more | |
127 | natural solution for that kind of problem). |
|
127 | natural solution for that kind of problem). | |
128 |
|
128 | |||
129 | The only significant limitation the notebook currently has, compared to the qt |
|
129 | The only significant limitation the notebook currently has, compared to the qt | |
130 | console, is that it can not run any code that expects input from the kernel |
|
130 | console, is that it can not run any code that expects input from the kernel | |
131 | (such as scripts that call :func:`raw_input`). Very importantly, this means |
|
131 | (such as scripts that call :func:`raw_input`). Very importantly, this means | |
132 | that the ``%debug`` magic does *not* work in the notebook! We intend to |
|
132 | that the ``%debug`` magic does *not* work in the notebook! We intend to | |
133 | correct this limitation, but in the meantime, there is a way to debug problems |
|
133 | correct this limitation, but in the meantime, there is a way to debug problems | |
134 | in the notebook: you can attach a Qt console to your existing notebook kernel, |
|
134 | in the notebook: you can attach a Qt console to your existing notebook kernel, | |
135 |
and run ``%debug`` from the Qt console. |
|
135 | and run ``%debug`` from the Qt console. If your notebook is running on a local | |
136 | terminal where you started the kernel that read something like:: |
|
136 | computer (i.e. if you are accessing it via your localhost address at | |
|
137 | 127.0.0.1), you can just type ``%qtconsole`` in the notebook and a Qt console | |||
|
138 | will open up connected to that same kernel. | |||
|
139 | ||||
|
140 | In general, the notebook server prints the full details of how to connect to | |||
|
141 | each kernel at the terminal, with lines like: | |||
137 |
|
142 | |||
138 | [IPKernelApp] To connect another client to this kernel, use: |
|
143 | [IPKernelApp] To connect another client to this kernel, use: | |
139 | [IPKernelApp] --existing --shell=53328 --iopub=53817 --stdin=34736 --hb=45543 |
|
144 | [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json | |
|
145 | ||||
|
146 | This is the name of a JSON file that contains all the port and validation | |||
|
147 | information necessary to connect to the kernel. You can manually start a | |||
|
148 | qt console with:: | |||
|
149 | ||||
|
150 | ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json | |||
|
151 | ||||
|
152 | and if you only have a single kernel running, simply typing:: | |||
140 |
|
153 | |||
141 | and then start a qt console pointing to that kernel:: |
|
154 | ipython qtconsole --existing | |
142 |
|
155 | |||
143 | ipython qtconsole --existing --shell=53328 --iopub=53817 --stdin=34736 --hb=45543 |
|
156 | will automatically find it (it will always find the most recently started | |
|
157 | kernel if there is more than one). You can also request this connection data | |||
|
158 | by typing ``%connect_info``; this will print the same file information as well | |||
|
159 | as the content of the JSON data structure it contains. | |||
144 |
|
160 | |||
145 |
|
161 | |||
146 | Text input |
|
162 | Text input | |
147 | ---------- |
|
163 | ---------- | |
148 |
|
164 | |||
149 | In addition to code cells and the output they produce (such as figures), you |
|
165 | In addition to code cells and the output they produce (such as figures), you | |
150 | can also type text not meant for execution. To type text, change the type of a |
|
166 | can also type text not meant for execution. To type text, change the type of a | |
151 | cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m` |
|
167 | cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m` | |
152 | keybinding (see below). You can then type any text in Markdown_ syntax, as |
|
168 | keybinding (see below). You can then type any text in Markdown_ syntax, as | |
153 | well as mathematical expressions if you use ``$...$`` for inline math or |
|
169 | well as mathematical expressions if you use ``$...$`` for inline math or | |
154 | ``$$...$$`` for displayed math. |
|
170 | ``$$...$$`` for displayed math. | |
155 |
|
171 | |||
156 | Exporting a notebook |
|
172 | Exporting a notebook | |
157 | -------------------- |
|
173 | -------------------- | |
158 |
|
174 | |||
159 | If you want to provide others with a static HTML or PDF view of your notebook, |
|
175 | If you want to provide others with a static HTML or PDF view of your notebook, | |
160 | use the ``Print`` button. This opens a static view of the document, which you |
|
176 | use the ``Print`` button. This opens a static view of the document, which you | |
161 | can print to PDF using your operating system's facilities, or save to a file |
|
177 | can print to PDF using your operating system's facilities, or save to a file | |
162 | with your web browser's 'Save' option (note that typically, this will create |
|
178 | with your web browser's 'Save' option (note that typically, this will create | |
163 | both an html file *and* a directory called `notebook_name_files` next to it |
|
179 | both an html file *and* a directory called `notebook_name_files` next to it | |
164 | that contains all the necessary style information, so if you intend to share |
|
180 | that contains all the necessary style information, so if you intend to share | |
165 | this, you must send the directory along with the main html file). |
|
181 | this, you must send the directory along with the main html file). | |
166 |
|
182 | |||
167 | The `Download` button lets you save a notebook file to the Download area |
|
183 | The `Download` button lets you save a notebook file to the Download area | |
168 | configured by your web browser (particularly useful if you are running the |
|
184 | configured by your web browser (particularly useful if you are running the | |
169 | notebook server on a remote host and need a file locally). The notebook is |
|
185 | notebook server on a remote host and need a file locally). The notebook is | |
170 | saved by default with the ``.ipynb`` extension and the files contain JSON data |
|
186 | saved by default with the ``.ipynb`` extension and the files contain JSON data | |
171 | that is not meant for human editing or consumption. But you can always export |
|
187 | that is not meant for human editing or consumption. But you can always export | |
172 | the input part of a notebook to a plain python script by choosing Python format |
|
188 | the input part of a notebook to a plain python script by choosing Python format | |
173 | in the `Download` drop list. This removes all output and saves the text cells |
|
189 | in the `Download` drop list. This removes all output and saves the text cells | |
174 | in comment areas. |
|
190 | in comment areas. | |
175 |
|
191 | |||
176 | .. warning:: |
|
192 | .. warning:: | |
177 |
|
193 | |||
178 | While in simple cases you can roundtrip a notebook to Python, edit the |
|
194 | While in simple cases you can roundtrip a notebook to Python, edit the | |
179 | python file and import it back without loss, this is in general *not |
|
195 | python file and import it back without loss, this is in general *not | |
180 | guaranteed to work at all*. As the notebook format evolves in complexity, |
|
196 | guaranteed to work at all*. As the notebook format evolves in complexity, | |
181 | there will be attributes of the notebook that will not survive a roundtrip |
|
197 | there will be attributes of the notebook that will not survive a roundtrip | |
182 | through the Python form. You should think of the Python format as a way to |
|
198 | through the Python form. You should think of the Python format as a way to | |
183 | output a script version of a notebook and the import capabilities as a way |
|
199 | output a script version of a notebook and the import capabilities as a way | |
184 | to load existing code to get a notebook started. But the Python version is |
|
200 | to load existing code to get a notebook started. But the Python version is | |
185 | *not* an alternate notebook format. |
|
201 | *not* an alternate notebook format. | |
186 |
|
202 | |||
187 |
|
203 | |||
188 | Keyboard use |
|
204 | Keyboard use | |
189 | ------------ |
|
205 | ------------ | |
190 |
|
206 | |||
191 | All actions in the notebook can be achieved with the mouse, but we have also |
|
207 | All actions in the notebook can be achieved with the mouse, but we have also | |
192 | added keyboard shortcuts for the most common ones, so that productive use of |
|
208 | added keyboard shortcuts for the most common ones, so that productive use of | |
193 | the notebook can be achieved with minimal mouse intervention. The main |
|
209 | the notebook can be achieved with minimal mouse intervention. The main | |
194 | key bindings you need to remember are: |
|
210 | key bindings you need to remember are: | |
195 |
|
211 | |||
196 | * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console), |
|
212 | * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console), | |
197 | show output (if any) and create a new cell below. Note that in the notebook, |
|
213 | show output (if any) and create a new cell below. Note that in the notebook, | |
198 | simply using :kbd:`Enter` *never* forces execution, it simply inserts a new |
|
214 | simply using :kbd:`Enter` *never* forces execution, it simply inserts a new | |
199 | line in the current cell. Therefore, in the notebook you must always use |
|
215 | line in the current cell. Therefore, in the notebook you must always use | |
200 | :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run |
|
216 | :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run | |
201 | Selected`` button). |
|
217 | Selected`` button). | |
202 |
|
218 | |||
203 | * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any |
|
219 | * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any | |
204 | output is shown but the cursor stays in the current cell, whose input |
|
220 | output is shown but the cursor stays in the current cell, whose input | |
205 | area is flushed empty. This is convenient to do quick in-place experiments |
|
221 | area is flushed empty. This is convenient to do quick in-place experiments | |
206 | or query things like filesystem content without creating additional cells you |
|
222 | or query things like filesystem content without creating additional cells you | |
207 | may not want saved in your notebook. |
|
223 | may not want saved in your notebook. | |
208 |
|
224 | |||
209 | * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist |
|
225 | * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist | |
210 | of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole |
|
226 | of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole | |
211 | letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining |
|
227 | letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining | |
212 | available keybindings. |
|
228 | available keybindings. | |
213 |
|
229 | |||
214 |
|
230 | |||
215 | Notebook document format |
|
231 | Notebook document format | |
216 | ======================== |
|
232 | ======================== | |
217 |
|
233 | |||
218 |
|
234 |
General Comments 0
You need to be logged in to leave comments.
Login now