##// END OF EJS Templates
allow saving notebook.py next to notebook.ipynb...
MinRK -
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
@@ -199,10 +208,23 b' class NotebookManager(LoggingConfigurable):'
199 208 current.write(nb, f, u'json')
200 209 except:
201 210 raise web.HTTPError(400, u'Unexpected error while saving notebook')
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:
218 raise web.HTTPError(400, u'Unexpected error while saving notebook as script')
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
General Comments 0
You need to be logged in to leave comments. Login now