##// END OF EJS Templates
Merge pull request #1168 from fperez/nbscript...
Fernando Perez -
r5763:e953107c merge
parent child Browse files
Show More
@@ -52,7 +52,7 b' from .handlers import (LoginHandler, LogoutHandler,'
52 )
52 )
53 from .notebookmanager import NotebookManager
53 from .notebookmanager import NotebookManager
54
54
55 from IPython.config.application import catch_config_error
55 from IPython.config.application import catch_config_error, boolean_flag
56 from IPython.core.application import BaseIPythonApplication
56 from IPython.core.application import BaseIPythonApplication
57 from IPython.core.profiledir import ProfileDir
57 from IPython.core.profiledir import ProfileDir
58 from IPython.lib.kernel import swallow_argv
58 from IPython.lib.kernel import swallow_argv
@@ -157,10 +157,15 b" flags['read-only'] = ("
157 """
157 """
158 )
158 )
159
159
160 # Add notebook manager flags
161 flags.update(boolean_flag('script', 'NotebookManager.save_script',
162 'Auto-save a .py script everytime the .ipynb notebook is saved',
163 'Do not auto-save .py scripts for every notebook'))
164
160 # the flags that are specific to the frontend
165 # the flags that are specific to the frontend
161 # these must be scrubbed before being passed to the kernel,
166 # these must be scrubbed before being passed to the kernel,
162 # or it will raise an error on unrecognized flags
167 # or it will raise an error on unrecognized flags
163 notebook_flags = ['no-browser', 'no-mathjax', 'read-only']
168 notebook_flags = ['no-browser', 'no-mathjax', 'read-only', 'script', 'no-script']
164
169
165 aliases = dict(ipkernel_aliases)
170 aliases = dict(ipkernel_aliases)
166
171
@@ -27,12 +27,10 b' from IPython.config.configurable import LoggingConfigurable'
27 from IPython.nbformat import current
27 from IPython.nbformat import current
28 from IPython.utils.traitlets import Unicode, List, Dict, Bool
28 from IPython.utils.traitlets import Unicode, List, Dict, Bool
29
29
30
31 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
32 # Code
31 # Classes
33 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
34
33
35
36 class NotebookManager(LoggingConfigurable):
34 class NotebookManager(LoggingConfigurable):
37
35
38 notebook_dir = Unicode(os.getcwd(), config=True, help="""
36 notebook_dir = Unicode(os.getcwd(), config=True, help="""
@@ -40,10 +38,12 b' class NotebookManager(LoggingConfigurable):'
40 """)
38 """)
41
39
42 save_script = Bool(False, config=True,
40 save_script = Bool(False, config=True,
43 help="""Also save notebooks as a Python script.
41 help="""Automatically create a Python script when saving the notebook.
44
42
45 For easier use of import/%loadpy across notebooks, a <notebook-name>.py
43 For easier use of import, %run and %loadpy across notebooks, a
46 script will be created next to any <notebook-name>.ipynb on each save.
44 <notebook-name>.py script will be created next to any
45 <notebook-name>.ipynb on each save. This can also be set with the
46 short `--script` flag.
47 """
47 """
48 )
48 )
49
49
@@ -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