Show More
@@ -19,31 +19,34 b' from IPython.utils.io import atomic_writing' | |||||
19 | from IPython.utils.importstring import import_item |
|
19 | from IPython.utils.importstring import import_item | |
20 | from IPython.utils.path import ensure_dir_exists |
|
20 | from IPython.utils.path import ensure_dir_exists | |
21 | from IPython.utils.traitlets import Any, Unicode, Bool, TraitError |
|
21 | from IPython.utils.traitlets import Any, Unicode, Bool, TraitError | |
22 | from IPython.utils.py3compat import getcwd, str_to_unicode |
|
22 | from IPython.utils.py3compat import getcwd, str_to_unicode, string_types | |
23 | from IPython.utils import tz |
|
23 | from IPython.utils import tz | |
24 | from IPython.html.utils import is_hidden, to_os_path, to_api_path |
|
24 | from IPython.html.utils import is_hidden, to_os_path, to_api_path | |
25 |
|
25 | |||
26 | _script_exporter = None |
|
26 | _script_exporter = None | |
|
27 | ||||
27 | def _post_save_script(model, os_path, contents_manager, **kwargs): |
|
28 | def _post_save_script(model, os_path, contents_manager, **kwargs): | |
28 | """convert notebooks to Python script after save with nbconvert |
|
29 | """convert notebooks to Python script after save with nbconvert | |
29 |
|
30 | |||
30 | replaces `ipython notebook --script` |
|
31 | replaces `ipython notebook --script` | |
31 | """ |
|
32 | """ | |
32 |
from IPython.nbconvert.exporters.p |
|
33 | from IPython.nbconvert.exporters.script import ScriptExporter | |
33 |
|
34 | |||
34 | if model['type'] != 'notebook': |
|
35 | if model['type'] != 'notebook': | |
35 | return |
|
36 | return | |
|
37 | ||||
36 | global _script_exporter |
|
38 | global _script_exporter | |
37 | if _script_exporter is None: |
|
39 | if _script_exporter is None: | |
38 |
_script_exporter = |
|
40 | _script_exporter = ScriptExporter(parent=contents_manager) | |
39 | log = contents_manager.log |
|
41 | log = contents_manager.log | |
40 |
|
42 | |||
41 | base, ext = os.path.splitext(os_path) |
|
43 | base, ext = os.path.splitext(os_path) | |
42 | py_fname = base + '.py' |
|
44 | py_fname = base + '.py' | |
43 | log.info("Writing %s", py_fname) |
|
45 | script, resources = _script_exporter.from_filename(os_path) | |
44 | py, resources = _script_exporter.from_filename(os_path) |
|
46 | script_fname = base + resources.get('output_extension', '.txt') | |
45 | with io.open(py_fname, 'w', encoding='utf-8') as f: |
|
47 | log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir)) | |
46 | f.write(py) |
|
48 | with io.open(script_fname, 'w', encoding='utf-8') as f: | |
|
49 | f.write(script) | |||
47 |
|
50 | |||
48 | class FileContentsManager(ContentsManager): |
|
51 | class FileContentsManager(ContentsManager): | |
49 |
|
52 | |||
@@ -96,9 +99,9 b' class FileContentsManager(ContentsManager):' | |||||
96 |
|
99 | |||
97 | A post-save hook has been registered that calls: |
|
100 | A post-save hook has been registered that calls: | |
98 |
|
101 | |||
99 |
ipython nbconvert --to p |
|
102 | ipython nbconvert --to script [notebook] | |
100 |
|
103 | |||
101 | which behaves similar to `--script`. |
|
104 | which behaves similarly to `--script`. | |
102 | """) |
|
105 | """) | |
103 |
|
106 | |||
104 | self.post_save_hook = _post_save_script |
|
107 | self.post_save_hook = _post_save_script |
General Comments 0
You need to be logged in to leave comments.
Login now