Show More
@@ -100,8 +100,8 b' When you open or create a new notebook, your browser tab will reflect the name o' | |||||
100 | The URL is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change in a future version of IPython. |
|
100 | The URL is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change in a future version of IPython. | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | Basic concepts in the Notebook app |
|
103 | Notebook user interface | |
104 |
----------------------- |
|
104 | ----------------------- | |
105 |
|
105 | |||
106 | When you finally start editing a notebook document in the Notebook, you will be presented with the title of the notebook, a *menu bar*, a *toolbar* and an empty *input cell*. |
|
106 | When you finally start editing a notebook document in the Notebook, you will be presented with the title of the notebook, a *menu bar*, a *toolbar* and an empty *input cell*. | |
107 |
|
107 | |||
@@ -148,6 +148,76 b' At certain moments, it may be necessary to interrupt a particularly long calcula' | |||||
148 | After a restart, all relevant cells must be re-evaluated |
|
148 | After a restart, all relevant cells must be re-evaluated | |
149 |
|
149 | |||
150 |
|
150 | |||
|
151 | Saveing a notebook | |||
|
152 | ------------------ | |||
|
153 | ||||
|
154 | The `Download` button lets you save a notebook file to the Download area | |||
|
155 | configured by your web browser (particularly useful if you are running the | |||
|
156 | notebook server on a remote host and need a file locally). | |||
|
157 | But you can always export | |||
|
158 | the input part of a notebook to a plain python script by choosing Python format | |||
|
159 | in the `Download` drop list. This removes all output and saves the text cells | |||
|
160 | in comment areas. See ref:`below <notebook_format>` for more details on the | |||
|
161 | notebook format. | |||
|
162 | ||||
|
163 | ||||
|
164 | .. warning:: | |||
|
165 | ||||
|
166 | While in simple cases you can roundtrip a notebook to Python, edit the | |||
|
167 | python file and import it back without loss of main content, this is in | |||
|
168 | general *not guaranteed to work at all*. First, there is extra metadata | |||
|
169 | saved in the notebook that may not be saved to the ``.py`` format. And as | |||
|
170 | the notebook format evolves in complexity, there will be attributes of the | |||
|
171 | notebook that will not survive a roundtrip through the Python form. You | |||
|
172 | should think of the Python format as a way to output a script version of a | |||
|
173 | notebook and the import capabilities as a way to load existing code to get a | |||
|
174 | notebook started. But the Python version is *not* an alternate notebook | |||
|
175 | format. | |||
|
176 | ||||
|
177 | ||||
|
178 | Importing or executing a notebook as a normal Python file | |||
|
179 | --------------------------------------------------------- | |||
|
180 | ||||
|
181 | The native format of the notebook, a file with a ``.ipynb`` `extension, is a | |||
|
182 | JSON container of all the input and output of the notebook, and therefore not | |||
|
183 | valid Python by itself. This means that by default, you cannot directly | |||
|
184 | import a notebook from Python, nor execute it as a normal python script. | |||
|
185 | ||||
|
186 | But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with:: | |||
|
187 | ||||
|
188 | ipython notebook --script | |||
|
189 | ||||
|
190 | or you can set this option permanently in your configuration file with:: | |||
|
191 | ||||
|
192 | c.NotebookManager.save_script=True | |||
|
193 | ||||
|
194 | This will instruct the notebook server to save the ``.py`` export of each | |||
|
195 | notebook, in addition to the ``.ipynb``, at every save. These are standard ``.py`` files, and so they can be | |||
|
196 | ``%run``, imported from regular IPython sessions or other notebooks, or | |||
|
197 | executed at the command-line. Since we export the raw | |||
|
198 | code you have typed, for these files to be importable from other code you will | |||
|
199 | have to avoid using syntax such as ``%magics`` and other IPython-specific | |||
|
200 | extensions to the language. | |||
|
201 | ||||
|
202 | In regular practice, the standard way to differentiate importable code from the | |||
|
203 | 'executable' part of a script is to put at the bottom:: | |||
|
204 | ||||
|
205 | if __name__ == '__main__': | |||
|
206 | # rest of the code... | |||
|
207 | ||||
|
208 | Since all cells in the notebook are run as top-level code, you'll need to | |||
|
209 | similarly protect *all* cells that you do not want executed when other scripts | |||
|
210 | try to import your notebook. A convenient shortand for this is to define early | |||
|
211 | on:: | |||
|
212 | ||||
|
213 | script = __name__ == '__main__' | |||
|
214 | ||||
|
215 | and then on any cell that you need to protect, use:: | |||
|
216 | ||||
|
217 | if script: | |||
|
218 | # rest of the cell... | |||
|
219 | ||||
|
220 | ||||
151 | Cell types |
|
221 | Cell types | |
152 | ---------- |
|
222 | ---------- | |
153 | Each IPython input cell has a *cell type*. |
|
223 | Each IPython input cell has a *cell type*. | |
@@ -178,7 +248,6 b' Raw cells' | |||||
178 | Raw cells provide a place to put additional information which is not evaluated by the Notebook. This can be used, for example, for extra information to be used when the notebook is exported to a certain format. |
|
248 | Raw cells provide a place to put additional information which is not evaluated by the Notebook. This can be used, for example, for extra information to be used when the notebook is exported to a certain format. | |
179 |
|
249 | |||
180 |
|
250 | |||
181 |
|
||||
182 | Magic commands |
|
251 | Magic commands | |
183 | -------------- |
|
252 | -------------- | |
184 | Magic commands, or *magics*, are one-word commands beginning with the symbol ``%``, which send commands to IPython itself (as opposed to standard Python commands which are exported to be run in a Python interpreter). |
|
253 | Magic commands, or *magics*, are one-word commands beginning with the symbol ``%``, which send commands to IPython itself (as opposed to standard Python commands which are exported to be run in a Python interpreter). | |
@@ -189,7 +258,6 b' There are two types of magics: *line magics*, which begin with a single ``%`` an' | |||||
189 |
|
258 | |||
190 | Line magics |
|
259 | Line magics | |
191 | ΛΛΛΛΛΛΛΛΛΛΛ |
|
260 | ΛΛΛΛΛΛΛΛΛΛΛ | |
192 |
|
||||
193 | Some of the available line magics are the following: |
|
261 | Some of the available line magics are the following: | |
194 |
|
262 | |||
195 | * ``%load``: |
|
263 | * ``%load``: | |
@@ -207,7 +275,17 b' Some of the available line magics are the following:' | |||||
207 | Cell magics |
|
275 | Cell magics | |
208 | ΛΛΛΛΛΛΛΛΛΛΛ |
|
276 | ΛΛΛΛΛΛΛΛΛΛΛ | |
209 |
|
277 | |||
210 |
* ``% |
|
278 | * ``%%bash``: | |
|
279 | Send the contents of the code cell to be executed by ``bash`` | |||
|
280 | ||||
|
281 | * ``%%file``: | |||
|
282 | Writes a file with with contents of the cell. *Caution*: The file is ovewritten without asking. | |||
|
283 | ||||
|
284 | * ``%%R``: | |||
|
285 | Execute the contents of the cell using the R language. | |||
|
286 | ||||
|
287 | * ``%%cython``: | |||
|
288 | Execute the contents of the cell using ``Cython``. | |||
211 |
|
289 | |||
212 |
|
290 | |||
213 |
|
291 | |||
@@ -227,87 +305,10 b' When the default ``%matplotlib`` or ``%pylab`` magics are used, the output of a ' | |||||
227 | ``%matplotlib inline`` |
|
305 | ``%matplotlib inline`` | |
228 | which captures the output inline within the notebook format. This has the benefit that the resulting plots will be stored in the notebook document. |
|
306 | which captures the output inline within the notebook format. This has the benefit that the resulting plots will be stored in the notebook document. | |
229 |
|
307 | |||
|
308 | Converting notebooks to other formats using nbconvert | |||
|
309 | ------------------------------------------------------ | |||
230 |
|
310 | |||
231 |
|
311 | |||
232 | Exporting a notebook and importing existing scripts |
|
|||
233 | --------------------------------------------------- |
|
|||
234 |
|
||||
235 | Need to talk about ipython nbconvert |
|
|||
236 |
|
||||
237 | If you want to provide others with a static HTML or PDF view of your notebook, |
|
|||
238 | use the ``Print`` button. This opens a static view of the document, which you |
|
|||
239 | can print to PDF using your operating system's facilities, or save to a file |
|
|||
240 | with your web browser's 'Save' option (note that typically, this will create |
|
|||
241 | both an html file *and* a directory called `notebook_name_files` next to it |
|
|||
242 | that contains all the necessary style information, so if you intend to share |
|
|||
243 | this, you must send the directory along with the main html file). |
|
|||
244 |
|
||||
245 | The `Download` button lets you save a notebook file to the Download area |
|
|||
246 | configured by your web browser (particularly useful if you are running the |
|
|||
247 | notebook server on a remote host and need a file locally). |
|
|||
248 | But you can always export |
|
|||
249 | the input part of a notebook to a plain python script by choosing Python format |
|
|||
250 | in the `Download` drop list. This removes all output and saves the text cells |
|
|||
251 | in comment areas. See ref:`below <notebook_format>` for more details on the |
|
|||
252 | notebook format. |
|
|||
253 |
|
||||
254 |
|
||||
255 | .. warning:: |
|
|||
256 |
|
||||
257 | While in simple cases you can roundtrip a notebook to Python, edit the |
|
|||
258 | python file and import it back without loss of main content, this is in |
|
|||
259 | general *not guaranteed to work at all*. First, there is extra metadata |
|
|||
260 | saved in the notebook that may not be saved to the ``.py`` format. And as |
|
|||
261 | the notebook format evolves in complexity, there will be attributes of the |
|
|||
262 | notebook that will not survive a roundtrip through the Python form. You |
|
|||
263 | should think of the Python format as a way to output a script version of a |
|
|||
264 | notebook and the import capabilities as a way to load existing code to get a |
|
|||
265 | notebook started. But the Python version is *not* an alternate notebook |
|
|||
266 | format. |
|
|||
267 |
|
||||
268 |
|
||||
269 | Importing or executing a notebook as a normal Python file |
|
|||
270 | --------------------------------------------------------- |
|
|||
271 |
|
||||
272 | The native format of the notebook, a file with a ``.ipynb`` `extension, is a |
|
|||
273 | JSON container of all the input and output of the notebook, and therefore not |
|
|||
274 | valid Python by itself. This means that by default, you cannot directly |
|
|||
275 | import a notebook from Python, nor execute it as a normal python script. |
|
|||
276 |
|
||||
277 | But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with:: |
|
|||
278 |
|
||||
279 | ipython notebook --script |
|
|||
280 |
|
||||
281 | or you can set this option permanently in your configuration file with:: |
|
|||
282 |
|
||||
283 | c.NotebookManager.save_script=True |
|
|||
284 |
|
||||
285 | This will instruct the notebook server to save the ``.py`` export of each |
|
|||
286 | notebook, in addition to the ``.ipynb``, at every save. These are standard ``.py`` files, and so they can be |
|
|||
287 | ``%run``, imported from regular IPython sessions or other notebooks, or |
|
|||
288 | executed at the command-line. Since we export the raw |
|
|||
289 | code you have typed, for these files to be importable from other code you will |
|
|||
290 | have to avoid using syntax such as ``%magics`` and other IPython-specific |
|
|||
291 | extensions to the language. |
|
|||
292 |
|
||||
293 | In regular practice, the standard way to differentiate importable code from the |
|
|||
294 | 'executable' part of a script is to put at the bottom:: |
|
|||
295 |
|
||||
296 | if __name__ == '__main__': |
|
|||
297 | # rest of the code... |
|
|||
298 |
|
||||
299 | Since all cells in the notebook are run as top-level code, you'll need to |
|
|||
300 | similarly protect *all* cells that you do not want executed when other scripts |
|
|||
301 | try to import your notebook. A convenient shortand for this is to define early |
|
|||
302 | on:: |
|
|||
303 |
|
||||
304 | script = __name__ == '__main__' |
|
|||
305 |
|
||||
306 | and then on any cell that you need to protect, use:: |
|
|||
307 |
|
||||
308 | if script: |
|
|||
309 | # rest of the cell... |
|
|||
310 |
|
||||
311 | Configuration |
|
312 | Configuration | |
312 | ------------- |
|
313 | ------------- | |
313 |
|
314 |
General Comments 0
You need to be logged in to leave comments.
Login now