Skip to content
This repository
Browse code

Merge pull request #1146 from minrk/savescript

Allow saving notebook.py next to notebook.ipynb.  This allows notebooks to be imported or %run via their .py companion file.
  • Loading branch information...
commit 1184eb4f23d813b1f5fbe5aa26be1dc57b6bb236 2 parents d6447c1 + ef61763
Fernando Perez fperez authored
28 IPython/frontend/html/notebook/notebookmanager.py
@@ -25,7 +25,7 @@
25 25
26 26 from IPython.config.configurable import LoggingConfigurable
27 27 from IPython.nbformat import current
28   -from IPython.utils.traitlets import Unicode, List, Dict
  28 +from IPython.utils.traitlets import Unicode, List, Dict, Bool
29 29
30 30
31 31 #-----------------------------------------------------------------------------
@@ -38,6 +38,15 @@ class NotebookManager(LoggingConfigurable):
38 38 notebook_dir = Unicode(os.getcwd(), config=True, help="""
39 39 The directory to use for notebooks.
40 40 """)
  41 +
  42 + save_script = Bool(False, config=True,
  43 + help="""Also save notebooks as a Python script.
  44 +
  45 + For easier use of import/%loadpy across notebooks, a <notebook-name>.py
  46 + script will be created next to any <notebook-name>.ipynb on each save.
  47 + """
  48 + )
  49 +
41 50 filename_ext = Unicode(u'.ipynb')
42 51 allowed_formats = List([u'json',u'py'])
43 52
@@ -197,12 +206,25 @@ def save_notebook_object(self, notebook_id, nb):
197 206 try:
198 207 with open(path,'w') as f:
199 208 current.write(nb, f, u'json')
200   - except:
201   - raise web.HTTPError(400, u'Unexpected error while saving notebook')
  209 + except Exception as e:
  210 + raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)
  211 + # save .py script as well
  212 + if self.save_script:
  213 + pypath = os.path.splitext(path)[0] + '.py'
  214 + try:
  215 + with open(pypath,'w') as f:
  216 + current.write(nb, f, u'py')
  217 + except Exception as e:
  218 + raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % e)
  219 +
202 220 if old_name != new_name:
203 221 old_path = self.get_path_by_name(old_name)
204 222 if os.path.isfile(old_path):
205 223 os.unlink(old_path)
  224 + if self.save_script:
  225 + old_pypath = os.path.splitext(old_path)[0] + '.py'
  226 + if os.path.isfile(old_pypath):
  227 + os.unlink(old_pypath)
206 228 self.mapping[notebook_id] = new_name
207 229 self.rev_mapping[new_name] = notebook_id
208 230
8 docs/source/interactive/htmlnotebook.txt
@@ -188,6 +188,14 @@ prior to import, you manually add the ``# <nbformat>2</nbformat>`` 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 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 +
191 199 .. warning::
192 200
193 201 While in simple cases you can roundtrip a notebook to Python, edit the

0 comments on commit 1184eb4

Please sign in to comment.
Something went wrong with that request. Please try again.