Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

allow saving notebook.py next to notebook.ipynb #1146

Merged
merged 3 commits into from

2 participants

@minrk
Owner

PR made at request of @fperez

This simply adds a flag for optionally saving .py version of notebooks alongside .ipynb.

enable with:

NotebookManager.save_script=True
@minrk minrk allow saving notebook.py next to notebook.ipynb
enable with:

    NotebookManager.save_script=True
44c21de
IPython/frontend/html/notebook/notebookmanager.py
@@ -199,10 +208,23 @@ def save_notebook_object(self, notebook_id, nb):
current.write(nb, f, u'json')
except:
raise web.HTTPError(400, u'Unexpected error while saving notebook')
+ # save .py script as well
+ if self.save_script:
+ pypath = os.path.splitext(path)[0] + '.py'
+ try:
+ with open(pypath,'w') as f:
+ current.write(nb, f, u'py')
+ except:
+ raise web.HTTPError(400, u'Unexpected error while saving notebook as script')
@fperez Owner
fperez added a note

Do you think we should at least put the exception message here? I think it would both help users and us if we ever have to debug a user-side problem.

@minrk Owner
minrk added a note

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fperez
Owner

This is great; I only have one comment on the code I made inline. Otherwise, before merging I just think this feature deserves a little note in the notebook section of the docs, as it may not be noticed by users normally and can be very useful in many circumstances. If you're too swamped I can add that note before merging, let me know.

@minrk
Owner

doc note added

@fperez
Owner

Excellent, merging now.

@fperez fperez merged commit 1184eb4 into ipython:master
@fperez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 8, 2011
  1. @minrk

    allow saving notebook.py next to notebook.ipynb

    minrk authored
    enable with:
    
        NotebookManager.save_script=True
Commits on Dec 13, 2011
  1. @minrk
  2. @minrk
This page is out of date. Refresh to see the latest.
View
28 IPython/frontend/html/notebook/notebookmanager.py
@@ -25,7 +25,7 @@
from IPython.config.configurable import LoggingConfigurable
from IPython.nbformat import current
-from IPython.utils.traitlets import Unicode, List, Dict
+from IPython.utils.traitlets import Unicode, List, Dict, Bool
#-----------------------------------------------------------------------------
@@ -38,6 +38,15 @@ class NotebookManager(LoggingConfigurable):
notebook_dir = Unicode(os.getcwd(), config=True, help="""
The directory to use for notebooks.
""")
+
+ save_script = Bool(False, config=True,
+ help="""Also save notebooks as a Python script.
+
+ For easier use of import/%loadpy across notebooks, a <notebook-name>.py
+ script will be created next to any <notebook-name>.ipynb on each save.
+ """
+ )
+
filename_ext = Unicode(u'.ipynb')
allowed_formats = List([u'json',u'py'])
@@ -197,12 +206,25 @@ def save_notebook_object(self, notebook_id, nb):
try:
with open(path,'w') as f:
current.write(nb, f, u'json')
- except:
- raise web.HTTPError(400, u'Unexpected error while saving notebook')
+ except Exception as e:
+ raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)
+ # save .py script as well
+ if self.save_script:
+ pypath = os.path.splitext(path)[0] + '.py'
+ try:
+ with open(pypath,'w') as f:
+ current.write(nb, f, u'py')
+ except Exception as e:
+ raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % e)
+
if old_name != new_name:
old_path = self.get_path_by_name(old_name)
if os.path.isfile(old_path):
os.unlink(old_path)
+ if self.save_script:
+ old_pypath = os.path.splitext(old_path)[0] + '.py'
+ if os.path.isfile(old_pypath):
+ os.unlink(old_pypath)
self.mapping[notebook_id] = new_name
self.rev_mapping[new_name] = notebook_id
View
8 docs/source/interactive/htmlnotebook.txt
@@ -188,6 +188,14 @@ prior to import, you manually add the ``# <nbformat>2</nbformat>`` 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
Something went wrong with that request. Please try again.