##// END OF EJS Templates
support downgrading notebooks with nbconvert...
MinRK -
Show More
@@ -4,6 +4,7 b' from .slides import SlidesExporter'
4 from .templateexporter import TemplateExporter
4 from .templateexporter import TemplateExporter
5 from .latex import LatexExporter
5 from .latex import LatexExporter
6 from .markdown import MarkdownExporter
6 from .markdown import MarkdownExporter
7 from .notebook import NotebookExporter
7 from .pdf import PDFExporter
8 from .pdf import PDFExporter
8 from .python import PythonExporter
9 from .python import PythonExporter
9 from .rst import RSTExporter
10 from .rst import RSTExporter
@@ -5,11 +5,18 b''
5
5
6 from .exporter import Exporter
6 from .exporter import Exporter
7 from IPython.nbformat import current as nbformat
7 from IPython.nbformat import current as nbformat
8 from IPython.utils.traitlets import Enum
8
9
9 class NotebookExporter(Exporter):
10 class NotebookExporter(Exporter):
11 """Exports to an IPython notebook."""
12
13 nbformat_version = Enum(list(range(2, nbformat.current_nbformat + 1)),
14 default_value=nbformat.current_nbformat,
15 config=True,
16 help="""The nbformat version to write.
17 Use this to downgrade notebooks.
10 """
18 """
11 Exports an IPython notebook.
19 )
12 """
13 def _file_extension_default(self):
20 def _file_extension_default(self):
14 return 'ipynb'
21 return 'ipynb'
15
22
@@ -17,5 +24,9 b' class NotebookExporter(Exporter):'
17
24
18 def from_notebook_node(self, nb, resources=None, **kw):
25 def from_notebook_node(self, nb, resources=None, **kw):
19 nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
26 nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
20 output = nbformat.writes_json(nb_copy)
27 if self.nbformat_version != nbformat.current_nbformat:
28 resources['output_suffix'] = '.v%i' % self.nbformat_version
29 else:
30 resources['output_suffix'] = '.nbconvert'
31 output = nbformat.writes(nb_copy, version=self.nbformat_version)
21 return output, resources
32 return output, resources
@@ -3,6 +3,8 b''
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import json
7
6 from .base import ExportersTestsBase
8 from .base import ExportersTestsBase
7 from ..notebook import NotebookExporter
9 from ..notebook import NotebookExporter
8
10
@@ -17,6 +19,18 b' class TestNotebookExporter(ExportersTestsBase):'
17 """
19 """
18 with open(self._get_notebook()) as f:
20 with open(self._get_notebook()) as f:
19 file_contents = f.read()
21 file_contents = f.read()
20 (output, resources) = NotebookExporter().from_filename(self._get_notebook())
22 (output, resources) = self.exporter_class().from_filename(self._get_notebook())
21 assert len(output) > 0
23 assert len(output) > 0
22 assert output == file_contents
24 self.assertEqual(output, file_contents)
25
26 def test_downgrade_3(self):
27 exporter = self.exporter_class(nbformat_version=3)
28 (output, resources) = exporter.from_filename(self._get_notebook())
29 nb = json.loads(output)
30 self.assertEqual(nb['nbformat'], 3)
31
32 def test_downgrade_2(self):
33 exporter = self.exporter_class(nbformat_version=2)
34 (output, resources) = exporter.from_filename(self._get_notebook())
35 nb = json.loads(output)
36 self.assertEqual(nb['nbformat'], 2)
@@ -53,6 +53,7 b' nbconvert_aliases.update({'
53 'post': 'NbConvertApp.postprocessor_class',
53 'post': 'NbConvertApp.postprocessor_class',
54 'output': 'NbConvertApp.output_base',
54 'output': 'NbConvertApp.output_base',
55 'reveal-prefix': 'RevealHelpPreprocessor.url_prefix',
55 'reveal-prefix': 'RevealHelpPreprocessor.url_prefix',
56 'nbformat': 'NotebookExporter.nbformat_version',
56 })
57 })
57
58
58 nbconvert_flags = {}
59 nbconvert_flags = {}
@@ -92,7 +93,7 b' class NbConvertApp(BaseIPythonApplication):'
92 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
93 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
93
94
94 output_base = Unicode('', config=True, help='''overwrite base name use for output files.
95 output_base = Unicode('', config=True, help='''overwrite base name use for output files.
95 can only be use when converting one notebook at a time.
96 can only be used when converting one notebook at a time.
96 ''')
97 ''')
97
98
98 examples = Unicode(u"""
99 examples = Unicode(u"""
@@ -298,6 +299,8 b' class NbConvertApp(BaseIPythonApplication):'
298 exc_info=True)
299 exc_info=True)
299 self.exit(1)
300 self.exit(1)
300 else:
301 else:
302 if 'output_suffix' in resources and not self.output_base:
303 notebook_name += resources['output_suffix']
301 write_results = self.writer.write(output, resources, notebook_name=notebook_name)
304 write_results = self.writer.write(output, resources, notebook_name=notebook_name)
302
305
303 #Post-process if post processor has been defined.
306 #Post-process if post processor has been defined.
General Comments 0
You need to be logged in to leave comments. Login now