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