Show More
@@ -187,15 +187,7 b' 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 | If you want use notebooks as scripts a lot, then you can set:: |
|
|||
192 |
|
||||
193 | c.NotebookManager.save_script=True |
|
|||
194 |
|
||||
195 | which will instruct the notebook server to save the ``.py`` export of each |
|
|||
196 | notebook adjacent to the ``.ipynb`` at every save. Then these can be ``%run`` |
|
|||
197 | or imported from regular IPython sessions or other notebooks. |
|
|||
198 |
|
||||
199 | .. warning:: |
|
191 | .. warning:: | |
200 |
|
192 | |||
201 | 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 | |
@@ -209,6 +201,48 b' or imported from regular IPython sessions or other notebooks.' | |||||
209 | notebook started. But the Python version is *not* an alternate notebook |
|
201 | notebook started. But the Python version is *not* an alternate notebook | |
210 | format. |
|
202 | format. | |
211 |
|
203 | |||
|
204 | ||||
|
205 | Importing or executing a notebook as a normal Python file | |||
|
206 | --------------------------------------------------------- | |||
|
207 | ||||
|
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 | |||
|
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 | |||
|
212 | notebooks as regular Python files, you can start the notebook server with:: | |||
|
213 | ||||
|
214 | ipython notebook --script | |||
|
215 | ||||
|
216 | or you can set this option permanently in your configuration file with:: | |||
|
217 | ||||
|
218 | c.NotebookManager.save_script=True | |||
|
219 | ||||
|
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 | |||
|
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 | |||
|
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 | |||
|
226 | extensions to the language. | |||
|
227 | ||||
|
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:: | |||
|
230 | ||||
|
231 | if __name__ == '__main__': | |||
|
232 | # rest of the code... | |||
|
233 | ||||
|
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 | |||
|
236 | try to import your notebook. A convenient shortand for this is to define early | |||
|
237 | on:: | |||
|
238 | ||||
|
239 | script = __name__ == '__main__': | |||
|
240 | ||||
|
241 | and then on any cell that you need to protect, use:: | |||
|
242 | ||||
|
243 | if script: | |||
|
244 | # rest of the cell... | |||
|
245 | ||||
212 |
|
246 | |||
213 | Keyboard use |
|
247 | Keyboard use | |
214 | ------------ |
|
248 | ------------ |
General Comments 0
You need to be logged in to leave comments.
Login now