##// END OF EJS Templates
support downgrading notebooks with nbconvert...
MinRK -
Show More
@@ -4,6 +4,7 b' from .slides import SlidesExporter'
4 4 from .templateexporter import TemplateExporter
5 5 from .latex import LatexExporter
6 6 from .markdown import MarkdownExporter
7 from .notebook import NotebookExporter
7 8 from .pdf import PDFExporter
8 9 from .python import PythonExporter
9 10 from .rst import RSTExporter
@@ -5,11 +5,18 b''
5 5
6 6 from .exporter import Exporter
7 7 from IPython.nbformat import current as nbformat
8 from IPython.utils.traitlets import Enum
8 9
9 10 class NotebookExporter(Exporter):
10 """
11 Exports an IPython notebook.
12 """
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.
18 """
19 )
13 20 def _file_extension_default(self):
14 21 return 'ipynb'
15 22
@@ -17,5 +24,9 b' class NotebookExporter(Exporter):'
17 24
18 25 def from_notebook_node(self, nb, resources=None, **kw):
19 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 32 return output, resources
@@ -3,6 +3,8 b''
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 import json
7
6 8 from .base import ExportersTestsBase
7 9 from ..notebook import NotebookExporter
8 10
@@ -17,6 +19,18 b' class TestNotebookExporter(ExportersTestsBase):'
17 19 """
18 20 with open(self._get_notebook()) as f:
19 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 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 53 'post': 'NbConvertApp.postprocessor_class',
54 54 'output': 'NbConvertApp.output_base',
55 55 'reveal-prefix': 'RevealHelpPreprocessor.url_prefix',
56 'nbformat': 'NotebookExporter.nbformat_version',
56 57 })
57 58
58 59 nbconvert_flags = {}
@@ -92,7 +93,7 b' class NbConvertApp(BaseIPythonApplication):'
92 93 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
93 94
94 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 99 examples = Unicode(u"""
@@ -298,6 +299,8 b' class NbConvertApp(BaseIPythonApplication):'
298 299 exc_info=True)
299 300 self.exit(1)
300 301 else:
302 if 'output_suffix' in resources and not self.output_base:
303 notebook_name += resources['output_suffix']
301 304 write_results = self.writer.write(output, resources, notebook_name=notebook_name)
302 305
303 306 #Post-process if post processor has been defined.
General Comments 0
You need to be logged in to leave comments. Login now