Show More
@@ -23,6 +23,27 b' from IPython.utils.py3compat import getcwd, str_to_unicode' | |||||
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 | |||
|
27 | def _post_save_script(model, os_path, contents_manager, **kwargs): | |||
|
28 | """convert notebooks to Python script after save with nbconvert | |||
|
29 | ||||
|
30 | replaces `ipython notebook --script` | |||
|
31 | """ | |||
|
32 | from IPython.nbconvert.exporters.python import PythonExporter | |||
|
33 | ||||
|
34 | if model['type'] != 'notebook': | |||
|
35 | return | |||
|
36 | global _script_exporter | |||
|
37 | if _script_exporter is None: | |||
|
38 | _script_exporter = PythonExporter(parent=contents_manager) | |||
|
39 | log = contents_manager.log | |||
|
40 | ||||
|
41 | base, ext = os.path.splitext(os_path) | |||
|
42 | py_fname = base + '.py' | |||
|
43 | log.info("Writing %s", py_fname) | |||
|
44 | py, resources = _script_exporter.from_filename(os_path) | |||
|
45 | with io.open(py_fname, 'w', encoding='utf-8') as f: | |||
|
46 | f.write(py) | |||
26 |
|
47 | |||
27 | class FileContentsManager(ContentsManager): |
|
48 | class FileContentsManager(ContentsManager): | |
28 |
|
49 | |||
@@ -65,13 +86,23 b' class FileContentsManager(ContentsManager):' | |||||
65 | with atomic_writing(os_path, *args, **kwargs) as f: |
|
86 | with atomic_writing(os_path, *args, **kwargs) as f: | |
66 | yield f |
|
87 | yield f | |
67 |
|
88 | |||
68 |
save_script = Bool(False, config=True, help='DEPRECATED, |
|
89 | save_script = Bool(False, config=True, help='DEPRECATED, use post_save_hook') | |
69 | def _save_script_changed(self): |
|
90 | def _save_script_changed(self): | |
70 | self.log.warn(""" |
|
91 | self.log.warn(""" | |
71 | Automatically saving notebooks as scripts has been removed. |
|
92 | `--script` is deprecated. You can trigger nbconvert via pre- or post-save hooks: | |
72 | Use `ipython nbconvert --to python [notebook]` instead. |
|
93 | ||
|
94 | ContentsManager.pre_save_hook | |||
|
95 | FileContentsManager.post_save_hook | |||
|
96 | ||||
|
97 | A post-save hook has been registered that calls: | |||
|
98 | ||||
|
99 | ipython nbconvert --to python [notebook] | |||
|
100 | ||||
|
101 | which behaves similar to `--script`. | |||
73 | """) |
|
102 | """) | |
74 |
|
103 | |||
|
104 | self.post_save_hook = _post_save_script | |||
|
105 | ||||
75 | post_save_hook = Any(None, config=True, |
|
106 | post_save_hook = Any(None, config=True, | |
76 | help="""Python callable or importstring thereof |
|
107 | help="""Python callable or importstring thereof | |
77 |
|
108 |
General Comments 0
You need to be logged in to leave comments.
Login now