# ipython/ipython

### Subversion checkout URL

You can clone with
or
.

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

Merged
merged 4 commits into from
+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): """) save_script = Bool(False, config=True, - help="""Also save notebooks as a Python script. + help="""Automaticall create a Python script when saving the notebook.
 Owner takluyver added a note Dec 17, 2011 Automatically - it's missing the y Owner fperez added a note Dec 17, 2011 Thanks, fixed! to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/html/notebook/notebookmanager.py
 ((13 lines not shown)) #----------------------------------------------------------------------------- +manager_flags =boolean_flag('script', 'NotebookManager.save_script',
 Owner takluyver added a note Dec 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 Dec 17, 2011 That object is actually a dict that contains two flags, '--script', and '--no-script' 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)) # the flags that are specific to the frontend # these must be scrubbed before being passed to the kernel, # or it will raise an error on unrecognized flags -notebook_flags = ['no-browser', 'no-mathjax', 'read-only'] +notebook_flags = ['no-browser', 'no-mathjax', 'read-only', 'script']
 Owner minrk added a note Dec 18, 2011 need to include 'no-script' in notebook_flags, so it won't be relayed to kernel Owner fperez added a note Dec 18, 2011 Fixing it now. 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.

merged commit e953107 into ipython:master
referenced this pull request from a commit
 Commit has since been removed from the repository and is no longer available.
Commits on Dec 17, 2011
1. fperez authored
2. fperez authored
3. fperez authored
Commits on Dec 18, 2011
1. fperez authored
 @@ -27,12 +27,10 @@ from IPython.nbformat import current from IPython.utils.traitlets import Unicode, List, Dict, Bool - #----------------------------------------------------------------------------- -# Code +# Classes #----------------------------------------------------------------------------- - class NotebookManager(LoggingConfigurable): notebook_dir = Unicode(os.getcwd(), config=True, help=""" @@ -40,10 +38,12 @@ class NotebookManager(LoggingConfigurable): """) save_script = Bool(False, config=True, - help="""Also save notebooks as a Python script. + help="""Automatically create a Python script when saving the notebook. - For easier use of import/%loadpy across notebooks, a .py - script will be created next to any .ipynb on each save. + For easier use of import, %run and %loadpy across notebooks, a + .py script will be created next to any + .ipynb on each save. This can also be set with the + short --script flag. """ )
 @@ -187,15 +187,7 @@ entire contents of the file will be loaded into a single code cell. But if prior to import, you manually add the # 2 marker at the start and then add separators for text/code cells, you can get a cleaner import with the file broken into individual cells. - -If you want use notebooks as scripts a lot, then you can set:: - - c.NotebookManager.save_script=True - -which will instruct the notebook server to save the .py export of each -notebook adjacent to the .ipynb at every save. Then these can be %run -or imported from regular IPython sessions or other notebooks. - + .. warning:: 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. notebook started. But the Python version is *not* an alternate notebook format. + +Importing or executing a notebook as a normal Python file +--------------------------------------------------------- + +The native format of the notebook, a file with a .ipynb extension, is a +JSON container of all the input and output of the notebook, and therefore not +valid Python by itself. This means that by default, you can not import a +notebook or execute it as a normal python script. But if you want use +notebooks as regular Python files, you can start the notebook server with:: + + ipython notebook --script + +or you can set this option permanently in your configuration file with:: + + c.NotebookManager.save_script=True + +This will instruct the notebook server to save the .py export of each +notebook adjacent to the .ipynb at every save. These files can be +%run, imported from regular IPython sessions or other notebooks, or +executed at the command-line as normal Python files. Since we export the raw +code you have typed, for these files to be importable from other code you will +have to avoid using syntax such as %magics and other IPython-specific +extensions to the language. + +In regular practice, the standard way to differentiate importable code from the +'executable' part of a script is to put at the bottom:: + + if __name__ == '__main__': + # rest of the code... + +Since all cells in the notebook are run as top-level code, you'll need to +similarly protect *all* cells that you do not want executed when other scripts +try to import your notebook. A convenient shortand for this is to define early +on:: + + script = __name__ == '__main__': + +and then on any cell that you need to protect, use:: + + if script: + # rest of the cell... + Keyboard use ------------