Show More
@@ -5,7 +5,7 b'' | |||
|
5 | 5 | |
|
6 | 6 | from functools import wraps |
|
7 | 7 | |
|
8 |
from IPython.nbformat. |
|
|
8 | from IPython.nbformat.v4 import NotebookNode | |
|
9 | 9 | from IPython.utils.decorators import undoc |
|
10 | 10 | from IPython.utils.py3compat import string_types |
|
11 | 11 |
@@ -2,39 +2,22 b'' | |||
|
2 | 2 | see templateexporter.py. |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | 5 | |
|
17 | 6 | from __future__ import print_function, absolute_import |
|
18 | 7 | |
|
19 | # Stdlib imports | |
|
20 | 8 | import io |
|
21 | 9 | import os |
|
22 | 10 | import copy |
|
23 | 11 | import collections |
|
24 | 12 | import datetime |
|
25 | 13 | |
|
26 | ||
|
27 | # IPython imports | |
|
28 | 14 | from IPython.config.configurable import LoggingConfigurable |
|
29 | 15 | from IPython.config import Config |
|
30 |
from IPython |
|
|
16 | from IPython import nbformat | |
|
31 | 17 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List |
|
32 | 18 | from IPython.utils.importstring import import_item |
|
33 | 19 | from IPython.utils import text, py3compat |
|
34 | 20 | |
|
35 | #----------------------------------------------------------------------------- | |
|
36 | # Class | |
|
37 | #----------------------------------------------------------------------------- | |
|
38 | 21 | |
|
39 | 22 | class ResourcesDict(collections.defaultdict): |
|
40 | 23 | def __missing__(self, key): |
@@ -107,7 +90,7 b' class Exporter(LoggingConfigurable):' | |||
|
107 | 90 | Parameters |
|
108 | 91 | ---------- |
|
109 | 92 | nb : :class:`~IPython.nbformat.current.NotebookNode` |
|
110 | Notebook node | |
|
93 | Notebook node (dict-like with attr-access) | |
|
111 | 94 | resources : dict |
|
112 | 95 | Additional resources that can be accessed read/write by |
|
113 | 96 | preprocessors and filters. |
@@ -149,7 +132,7 b' class Exporter(LoggingConfigurable):' | |||
|
149 | 132 | resources['metadata']['modified_date'] = modified_date.strftime(text.date_format) |
|
150 | 133 | |
|
151 | 134 | with io.open(filename, encoding='utf-8') as f: |
|
152 |
return self.from_notebook_node(nbformat.read(f, |
|
|
135 | return self.from_notebook_node(nbformat.read(f, as_version=4), resources=resources, **kw) | |
|
153 | 136 | |
|
154 | 137 | |
|
155 | 138 | def from_file(self, file_stream, resources=None, **kw): |
@@ -161,7 +144,7 b' class Exporter(LoggingConfigurable):' | |||
|
161 | 144 | file_stream : file-like object |
|
162 | 145 | Notebook file-like object to convert. |
|
163 | 146 | """ |
|
164 |
return self.from_notebook_node(nbformat.read(file_stream, |
|
|
147 | return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw) | |
|
165 | 148 | |
|
166 | 149 | |
|
167 | 150 | def register_preprocessor(self, preprocessor, enabled=False): |
@@ -4,13 +4,13 b'' | |||
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | from .exporter import Exporter |
|
7 |
from IPython |
|
|
7 | from IPython import nbformat | |
|
8 | 8 | from IPython.utils.traitlets import Enum |
|
9 | 9 | |
|
10 | 10 | class NotebookExporter(Exporter): |
|
11 | 11 | """Exports to an IPython notebook.""" |
|
12 | 12 | |
|
13 |
nbformat_version = Enum(list( |
|
|
13 | nbformat_version = Enum(list(nbformat.versions), | |
|
14 | 14 | default_value=nbformat.current_nbformat, |
|
15 | 15 | config=True, |
|
16 | 16 | help="""The nbformat version to write. |
@@ -24,7 +24,7 b' class NotebookExporter(Exporter):' | |||
|
24 | 24 | |
|
25 | 25 | def from_notebook_node(self, nb, resources=None, **kw): |
|
26 | 26 | nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw) |
|
27 |
if self.nbformat_version != nbformat |
|
|
27 | if self.nbformat_version != nb_copy.nbformat: | |
|
28 | 28 | resources['output_suffix'] = '.v%i' % self.nbformat_version |
|
29 | 29 | else: |
|
30 | 30 | resources['output_suffix'] = '.nbconvert' |
@@ -2,29 +2,17 b'' | |||
|
2 | 2 | Module with tests for export.py |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
5 | # Copyright (c) IPython Development Team. | |
|
8 | 6 | # Distributed under the terms of the Modified BSD License. |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | 7 | |
|
17 | 8 | import os |
|
18 | 9 | |
|
19 |
from IPython |
|
|
10 | from IPython import nbformat | |
|
20 | 11 | |
|
21 | 12 | from .base import ExportersTestsBase |
|
22 | 13 | from ..export import * |
|
23 | 14 | from ..python import PythonExporter |
|
24 | 15 | |
|
25 | #----------------------------------------------------------------------------- | |
|
26 | # Class | |
|
27 | #----------------------------------------------------------------------------- | |
|
28 | 16 | |
|
29 | 17 | class TestExport(ExportersTestsBase): |
|
30 | 18 | """Contains test functions for export.py""" |
@@ -53,7 +41,7 b' class TestExport(ExportersTestsBase):' | |||
|
53 | 41 | Can a notebook be exported by a notebook node handle? |
|
54 | 42 | """ |
|
55 | 43 | with open(self._get_notebook(), 'r') as f: |
|
56 |
notebook = nbformat.read(f, |
|
|
44 | notebook = nbformat.read(f, 4) | |
|
57 | 45 | (output, resources) = export_by_name('python', notebook) |
|
58 | 46 | assert len(output) > 0 |
|
59 | 47 |
@@ -9,7 +9,8 b' import re' | |||
|
9 | 9 | |
|
10 | 10 | from .base import ExportersTestsBase |
|
11 | 11 | from ..latex import LatexExporter |
|
12 |
from IPython.nbformat import |
|
|
12 | from IPython.nbformat import write | |
|
13 | from IPython.nbformat import v4 | |
|
13 | 14 | from IPython.testing.decorators import onlyif_cmds_exist |
|
14 | 15 | from IPython.utils.tempdir import TemporaryDirectory |
|
15 | 16 | |
@@ -85,16 +86,16 b' class TestLatexExporter(ExportersTestsBase):' | |||
|
85 | 86 | large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000) |
|
86 | 87 | |
|
87 | 88 | notebook_name = "lorem_ipsum_long.ipynb" |
|
88 |
nb = |
|
|
89 | nb = v4.new_notebook( | |
|
89 | 90 | cells=[ |
|
90 |
|
|
|
91 | v4.new_markdown_cell(source=large_lorem_ipsum_text) | |
|
91 | 92 | ] |
|
92 | 93 | ) |
|
93 | 94 | |
|
94 | 95 | with TemporaryDirectory() as td: |
|
95 | 96 | nbfile = os.path.join(td, notebook_name) |
|
96 | 97 | with open(nbfile, 'w') as f: |
|
97 |
|
|
|
98 | write(f, nb, 4) | |
|
98 | 99 | |
|
99 | 100 |
(output, resources) = LatexExporter(template_file='article').from_filename(nbfile) |
|
100 | 101 | assert len(output) > 0 |
@@ -8,7 +8,7 b' import json' | |||
|
8 | 8 | from .base import ExportersTestsBase |
|
9 | 9 | from ..notebook import NotebookExporter |
|
10 | 10 | |
|
11 |
from IPython.nbformat |
|
|
11 | from IPython.nbformat import validate | |
|
12 | 12 | from IPython.testing.tools import assert_big_text_equal |
|
13 | 13 | |
|
14 | 14 | class TestNotebookExporter(ExportersTestsBase): |
@@ -1,20 +1,12 b'' | |||
|
1 | 1 | """Tests for RSTExporter""" |
|
2 | 2 | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) 2013, the IPython Development Team. | |
|
5 | # | |
|
3 | # Copyright (c) IPython Development Team. | |
|
6 | 4 | # Distributed under the terms of the Modified BSD License. |
|
7 | # | |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | ||
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Imports | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | 5 | |
|
15 | 6 | import io |
|
16 | 7 | |
|
17 |
from IPython |
|
|
8 | from IPython import nbformat | |
|
9 | from IPython.nbformat import v4 | |
|
18 | 10 | |
|
19 | 11 | from .base import ExportersTestsBase |
|
20 | 12 | from ..rst import RSTExporter |
@@ -47,14 +39,14 b' class TestRSTExporter(ExportersTestsBase):' | |||
|
47 | 39 | """No empty code cells in rst""" |
|
48 | 40 | nbname = self._get_notebook() |
|
49 | 41 | with io.open(nbname, encoding='utf8') as f: |
|
50 |
nb = |
|
|
42 | nb = nbformat.read(f, 4) | |
|
51 | 43 | |
|
52 | 44 | exporter = self.exporter_class() |
|
53 | 45 | |
|
54 | 46 | (output, resources) = exporter.from_notebook_node(nb) |
|
55 | 47 | # add an empty code cell |
|
56 | 48 | nb.cells.append( |
|
57 |
|
|
|
49 | v4.new_code_cell(source="") | |
|
58 | 50 | ) |
|
59 | 51 | (output2, resources) = exporter.from_notebook_node(nb) |
|
60 | 52 | # adding an empty code cell shouldn't change output |
@@ -4,7 +4,6 b'' | |||
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | import os |
|
7 | import sys | |
|
8 | 7 | |
|
9 | 8 | try: |
|
10 | 9 | from queue import Empty # Py 3 |
@@ -13,10 +12,11 b' except ImportError:' | |||
|
13 | 12 | |
|
14 | 13 | from IPython.utils.traitlets import List, Unicode |
|
15 | 14 | |
|
16 |
from IPython.nbformat. |
|
|
15 | from IPython.nbformat.v4 import output_from_msg | |
|
17 | 16 | from .base import Preprocessor |
|
18 | 17 | from IPython.utils.traitlets import Integer |
|
19 | 18 | |
|
19 | ||
|
20 | 20 | class ExecutePreprocessor(Preprocessor): |
|
21 | 21 | """ |
|
22 | 22 | Executes all the cells in a notebook |
@@ -3,7 +3,7 b'' | |||
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 |
from IPython.nbformat import |
|
|
6 | from IPython.nbformat import v4 as nbformat | |
|
7 | 7 | |
|
8 | 8 | from ...tests.base import TestsBase |
|
9 | 9 | from ...exporters.exporter import ResourcesDict |
@@ -5,8 +5,6 b' Module with tests for the clearoutput preprocessor.' | |||
|
5 | 5 | # Copyright (c) IPython Development Team. |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | |
|
8 | from IPython.nbformat import current as nbformat | |
|
9 | ||
|
10 | 8 | from .base import PreprocessorTestsBase |
|
11 | 9 | from ..clearoutput import ClearOutputPreprocessor |
|
12 | 10 |
@@ -3,7 +3,7 b'' | |||
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 |
from IPython.nbformat import |
|
|
6 | from IPython.nbformat import v4 as nbformat | |
|
7 | 7 | |
|
8 | 8 | from .base import PreprocessorTestsBase |
|
9 | 9 | from ..coalescestreams import coalesce_streams |
@@ -7,10 +7,11 b' Module with tests for the execute preprocessor.' | |||
|
7 | 7 | |
|
8 | 8 | import copy |
|
9 | 9 | import glob |
|
10 | import io | |
|
10 | 11 | import os |
|
11 | 12 | import re |
|
12 | 13 | |
|
13 |
from IPython |
|
|
14 | from IPython import nbformat | |
|
14 | 15 | |
|
15 | 16 | from .base import PreprocessorTestsBase |
|
16 | 17 | from ..execute import ExecutePreprocessor |
@@ -78,8 +79,8 b' class TestExecute(PreprocessorTestsBase):' | |||
|
78 | 79 | current_dir = os.path.dirname(__file__) |
|
79 | 80 | input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb')) |
|
80 | 81 | for filename in input_files: |
|
81 | with open(os.path.join(current_dir, 'files', filename)) as f: | |
|
82 |
input_nb = nbformat.read(f, |
|
|
82 | with io.open(os.path.join(current_dir, 'files', filename)) as f: | |
|
83 | input_nb = nbformat.read(f, 4) | |
|
83 | 84 | res = self.build_resources() |
|
84 | 85 | preprocessor = self.build_preprocessor() |
|
85 | 86 | cleaned_input_nb = copy.deepcopy(input_nb) |
@@ -3,7 +3,7 b'' | |||
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 |
from IPython.nbformat import |
|
|
6 | from IPython.nbformat import v4 as nbformat | |
|
7 | 7 | |
|
8 | 8 | from .base import PreprocessorTestsBase |
|
9 | 9 | from ..revealhelp import RevealHelpPreprocessor |
@@ -4,7 +4,7 b'' | |||
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | from IPython.testing import decorators as dec |
|
7 |
from IPython.nbformat import |
|
|
7 | from IPython.nbformat import v4 as nbformat | |
|
8 | 8 | |
|
9 | 9 | from .base import PreprocessorTestsBase |
|
10 | 10 | from ..svg2pdf import SVG2PDFPreprocessor |
@@ -10,7 +10,7 b' import shutil' | |||
|
10 | 10 | import unittest |
|
11 | 11 | |
|
12 | 12 | import IPython |
|
13 |
from IPython.nbformat import |
|
|
13 | from IPython.nbformat import v4, write | |
|
14 | 14 | from IPython.utils.tempdir import TemporaryWorkingDirectory |
|
15 | 15 | from IPython.utils.path import get_ipython_package_dir |
|
16 | 16 | from IPython.utils.process import get_output_error_code |
@@ -101,10 +101,9 b' class TestsBase(unittest.TestCase):' | |||
|
101 | 101 | return temp_dir |
|
102 | 102 | |
|
103 | 103 | def create_empty_notebook(self, path): |
|
104 |
nb = |
|
|
104 | nb = v4.new_notebook() | |
|
105 | 105 | with io.open(path, 'w', encoding='utf-8') as f: |
|
106 |
|
|
|
107 | ||
|
106 | write(f, nb, 4) | |
|
108 | 107 | |
|
109 | 108 | def copy_files_to(self, copy_filenames, dest='.'): |
|
110 | 109 | "Copy test files into the destination directory" |
General Comments 0
You need to be logged in to leave comments.
Login now