##// END OF EJS Templates
Merge pull request #1146 from minrk/savescript...
Fernando Perez -
r5711:1184eb4f merge
parent child Browse files
Show More
@@ -25,7 +25,7 b' from tornado import web'
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 b' 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 b' class NotebookManager(LoggingConfigurable):'
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
@@ -188,6 +188,14 b' 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
General Comments 0
You need to be logged in to leave comments. Login now