# public ipython /ipython

### Subversion checkout URL

You can clone with HTTPS or Subversion.

# Add --script flag as shorthand for notebook save_script option.#1168

Merged
merged 4 commits into from about 2 years ago
 +56 17

### 3 participants

Owner

Also expand the section of the docs dealing with notebook imports.

 fperez Add --script flag as shorthand for the script autosave notebook option. 38ef72e fperez Update nb docs on --script flag and notebook import practices. 8ac6b0c
 IPython/frontend/html/notebook/notebookmanager.py 
 @@ -40,10 +47,12 @@ class NotebookManager(LoggingConfigurable): 40 47  """) 41 48   42 49  save_script = Bool(False, config=True, 43 - help="""Also save notebooks as a Python script. 50 + help="""Automaticall create a Python script when saving the notebook.
 IPython/frontend/html/notebook/notebookmanager.py 
 ((13 lines not shown)) 33 33  #----------------------------------------------------------------------------- 34 34   35 +manager_flags =boolean_flag('script', 'NotebookManager.save_script',
 2 Collaborator takluyver added a note December 17, 2011 I'm not so familiar with our command line parsing, but it looks odd that we're assigning a single ...flag item to a plural ...flags name. Should it be wrapped in a container of some sort? Owner fperez added a note December 17, 2011 That object is actually a dict that contains two flags, '--script', and '--no-script' Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
 fperez Fix typo in help string 0d62fc2
 IPython/frontend/html/notebook/notebookapp.py 
 ((6 lines not shown)) 160 162  # the flags that are specific to the frontend 161 163  # these must be scrubbed before being passed to the kernel, 162 164  # or it will raise an error on unrecognized flags 163 -notebook_flags = ['no-browser', 'no-mathjax', 'read-only'] 165 +notebook_flags = ['no-browser', 'no-mathjax', 'read-only', 'script']
 2 Owner minrk added a note December 17, 2011 need to include 'no-script' in notebook_flags, so it won't be relayed to kernel Owner fperez added a note December 17, 2011 Fixing it now. Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Owner

Typically we have defined flags in the same file as the application that uses them, but I don't actually see anything wrong with doing it this way.

Owner

OK, I'll move it over for consistency's sake. Not having done it before, I did what seemed most obvious immediately, but I think it's better to keep a consistent pattern.

 fperez Define flags in application that's going to use them. 031cdc5
Owner

Pushed, let me know how it looks and we'll merge when ready. Thanks for the review!

Owner

thanks, looks good. Go ahead and merge.

Owner

Great, thanks. Merging now.

referenced this pull request from a commit
 fperez Merge pull request #1168 from fperez/nbscript Add --script/--no-script flags as shorthands for the NotebookManager.save_script option. e953107
merged commit e953107 into from
closed this
referenced this pull request from a commit
 Commit has since been removed from the repository and is no longer available.

Showing 4 unique commits by 1 author.

Dec 17, 2011
Add --script flag as shorthand for the script autosave notebook option. 38ef72e
Update nb docs on --script flag and notebook import practices. 8ac6b0c
Fix typo in help string 0d62fc2
Define flags in application that's going to use them. 031cdc5
 @@ -52,7 +52,7 @@ 52 52  ) 53 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 56  from IPython.core.application import BaseIPythonApplication 57 57  from IPython.core.profiledir import ProfileDir 58 58  from IPython.lib.kernel import swallow_argv @@ -157,10 +157,15 @@ def __init__(self, ipython_app, kernel_manager, notebook_manager, log, settings_ 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 165  # the flags that are specific to the frontend 161 166  # these must be scrubbed before being passed to the kernel, 162 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 170  aliases = dict(ipkernel_aliases) 166 171 
 @@ -27,12 +27,10 @@ 27 27  from IPython.nbformat import current 28 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 34  class NotebookManager(LoggingConfigurable): 37 35   38 36  notebook_dir = Unicode(os.getcwd(), config=True, help=""" @@ -40,10 +38,12 @@ class NotebookManager(LoggingConfigurable): 40 38  """) 41 39   42 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 .py 46 - script will be created next to any .ipynb on each save. 43 + For easier use of import, %run and %loadpy across notebooks, a 44 + .py script will be created next to any 45 + .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 @@ entire contents of the file will be loaded into a single code cell. But if 187 187  prior to import, you manually add the # 2 marker at 188 188  the start and then add separators for text/code cells, you can get a cleaner 189 189  import with the file broken into individual cells. 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 - 190 +  199 191  .. warning:: 200 192   201 193  While in simple cases you can roundtrip a notebook to Python, edit the @@ -209,6 +201,48 @@ or imported from regular IPython sessions or other notebooks. 209 201  notebook started. But the Python version is *not* an alternate notebook 210 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 247  Keyboard use 214 248  ------------