Show More
@@ -0,0 +1,19 | |||
|
1 | import copy | |
|
2 | ||
|
3 | from IPython.nbformat.current import validate | |
|
4 | from .. import convert | |
|
5 | ||
|
6 | from . import nbexamples | |
|
7 | from IPython.nbformat.v3.tests import nbexamples as v3examples | |
|
8 | ||
|
9 | def test_upgrade_notebook(): | |
|
10 | nb03 = copy.deepcopy(v3examples.nb0) | |
|
11 | validate(nb03) | |
|
12 | nb04 = convert.upgrade(nb03) | |
|
13 | validate(nb04) | |
|
14 | ||
|
15 | def test_downgrade_notebook(): | |
|
16 | nb04 = copy.deepcopy(nbexamples.nb0) | |
|
17 | validate(nb04) | |
|
18 | nb03 = convert.downgrade(nb04) | |
|
19 | validate(nb03) |
@@ -1,159 +1,200 | |||
|
1 | 1 | """Code for converting notebooks to and from v3.""" |
|
2 | 2 | |
|
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 .nbbase import ( |
|
7 | 9 | nbformat, nbformat_minor, |
|
10 | NotebookNode, | |
|
8 | 11 | ) |
|
9 | 12 | |
|
10 | 13 | from IPython.nbformat import v3 |
|
11 | 14 | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Code | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | 15 | |
|
16 | 16 | def upgrade(nb, from_version=3, from_minor=0): |
|
17 | 17 | """Convert a notebook to v4. |
|
18 | 18 | |
|
19 | 19 | Parameters |
|
20 | 20 | ---------- |
|
21 | 21 | nb : NotebookNode |
|
22 | 22 | The Python representation of the notebook to convert. |
|
23 | 23 | from_version : int |
|
24 | 24 | The original version of the notebook to convert. |
|
25 | 25 | from_minor : int |
|
26 | 26 | The original minor version of the notebook to convert (only relevant for v >= 3). |
|
27 | 27 | """ |
|
28 | from IPython.nbformat.current import validate | |
|
28 | 29 | if from_version == 3: |
|
30 | # Validate the notebook before conversion | |
|
31 | validate(nb, version=from_version) | |
|
32 | ||
|
29 | 33 | # Mark the original nbformat so consumers know it has been converted. |
|
34 | nb.metadata.orig_nbformat = 3 | |
|
35 | # Mark the new format | |
|
30 | 36 | nb.nbformat = nbformat |
|
31 | 37 | nb.nbformat_minor = nbformat_minor |
|
32 | 38 | |
|
33 | nb.orig_nbformat = 3 | |
|
34 | 39 | # remove worksheet(s) |
|
35 | 40 | nb['cells'] = cells = [] |
|
36 | 41 | # In the unlikely event of multiple worksheets, |
|
37 | 42 | # they will be flattened |
|
38 | 43 | for ws in nb.pop('worksheets', []): |
|
39 | 44 | # upgrade each cell |
|
40 | 45 | for cell in ws['cells']: |
|
41 | 46 | cells.append(upgrade_cell(cell)) |
|
42 |
# upgrade metadata |
|
|
47 | # upgrade metadata | |
|
43 | 48 | nb.metadata.pop('name', '') |
|
49 | # Validate the converted notebook before returning it | |
|
50 | validate(nb, version=nbformat) | |
|
44 | 51 | return nb |
|
45 | 52 | elif from_version == 4: |
|
46 | 53 | # nothing to do |
|
47 | 54 | if from_minor != nbformat_minor: |
|
48 | nb.orig_nbformat_minor = from_minor | |
|
55 | nb.metadata.orig_nbformat_minor = from_minor | |
|
49 | 56 | nb.nbformat_minor = nbformat_minor |
|
57 | ||
|
50 | 58 | return nb |
|
51 | 59 | else: |
|
52 | 60 | raise ValueError('Cannot convert a notebook directly from v%s to v4. ' \ |
|
53 | 61 | 'Try using the IPython.nbformat.convert module.' % from_version) |
|
54 | 62 | |
|
55 | 63 | def upgrade_cell(cell): |
|
56 | 64 | """upgrade a cell from v3 to v4 |
|
57 | 65 | |
|
58 | 66 | code cell: |
|
59 | 67 | - remove language metadata |
|
60 | 68 | - cell.input -> cell.source |
|
61 | 69 | - update outputs |
|
62 | 70 | """ |
|
71 | cell.setdefault('metadata', NotebookNode()) | |
|
63 | 72 | if cell.cell_type == 'code': |
|
64 |
cell |
|
|
73 | cell.pop('language', '') | |
|
74 | cell.metadata.collapsed = cell.pop('collapsed') | |
|
65 | 75 | cell.source = cell.pop('input') |
|
66 | cell.outputs = upgrade_outputs(cell) | |
|
76 | cell.setdefault('prompt_number', None) | |
|
77 | cell.outputs = upgrade_outputs(cell.outputs) | |
|
78 | elif cell.cell_type == 'html': | |
|
79 | # Technically, this exists. It will never happen in practice. | |
|
80 | cell.cell_type = 'markdown' | |
|
67 | 81 | return cell |
|
68 | 82 | |
|
69 | 83 | def downgrade_cell(cell): |
|
70 | 84 | if cell.cell_type == 'code': |
|
71 | cell.input = cell.pop('source') | |
|
72 | cell.outputs = downgrade_outputs(cell) | |
|
85 | cell.language = 'python' | |
|
86 | cell.input = cell.pop('source', '') | |
|
87 | cell.collapsed = cell.metadata.pop('collapsed', False) | |
|
88 | cell.outputs = downgrade_outputs(cell.outputs) | |
|
73 | 89 | return cell |
|
74 | 90 | |
|
75 | 91 | _mime_map = { |
|
76 | 92 | "text" : "text/plain", |
|
77 | 93 | "html" : "text/html", |
|
78 | 94 | "svg" : "image/svg+xml", |
|
79 | 95 | "png" : "image/png", |
|
80 | 96 | "jpeg" : "image/jpeg", |
|
81 | 97 | "latex" : "text/latex", |
|
82 | 98 | "json" : "application/json", |
|
83 | 99 | "javascript" : "application/javascript", |
|
84 | 100 | }; |
|
85 | 101 | |
|
86 | 102 | def to_mime_key(d): |
|
87 | 103 | """convert dict with v3 aliases to plain mime-type keys""" |
|
88 | 104 | for alias, mime in _mime_map.items(): |
|
89 | 105 | if alias in d: |
|
90 | 106 | d[mime] = d.pop(alias) |
|
91 | 107 | return d |
|
92 | 108 | |
|
93 | 109 | def from_mime_key(d): |
|
94 | 110 | """convert dict with mime-type keys to v3 aliases""" |
|
95 | 111 | for alias, mime in _mime_map.items(): |
|
96 | 112 | if mime in d: |
|
97 | 113 | d[alias] = d.pop(mime) |
|
98 | 114 | return d |
|
99 | 115 | |
|
100 | 116 | def upgrade_output(output): |
|
101 | 117 | """upgrade a single code cell output from v3 to v4 |
|
102 | 118 | |
|
103 | 119 | - pyout -> execute_result |
|
104 | 120 | - pyerr -> error |
|
105 | 121 | - mime-type keys |
|
122 | - stream.stream -> stream.name | |
|
106 | 123 | """ |
|
124 | output.setdefault('metadata', NotebookNode()) | |
|
125 | if output['output_type'] in {'pyout', 'display_data'}: | |
|
107 | 126 | if output['output_type'] == 'pyout': |
|
108 | 127 | output['output_type'] = 'execute_result' |
|
109 | 128 | to_mime_key(output) |
|
110 |
to_mime_key(output. |
|
|
129 | to_mime_key(output.metadata) | |
|
130 | if 'application/json' in output: | |
|
131 | output['application/json'] = json.loads(output['application/json']) | |
|
132 | # promote ascii bytes (from v2) to unicode | |
|
133 | for key in ('image/png', 'image/jpeg'): | |
|
134 | if key in output and isinstance(output[key], bytes): | |
|
135 | output[key] = output[key].decode('ascii') | |
|
111 | 136 | elif output['output_type'] == 'pyerr': |
|
112 | 137 | output['output_type'] = 'error' |
|
113 |
elif output['output_type'] == ' |
|
|
114 | to_mime_key(output) | |
|
115 | to_mime_key(output.get('metadata', {})) | |
|
138 | elif output['output_type'] == 'stream': | |
|
139 | output['name'] = output.pop('stream') | |
|
116 | 140 | return output |
|
117 | 141 | |
|
118 | 142 | def downgrade_output(output): |
|
119 | 143 | """downgrade a single code cell output to v3 from v4 |
|
120 | 144 | |
|
121 | 145 | - pyout <- execute_result |
|
122 | 146 | - pyerr <- error |
|
123 | 147 | - un-mime-type keys |
|
148 | - stream.stream <- stream.name | |
|
124 | 149 | """ |
|
125 | 150 | if output['output_type'] == 'execute_result': |
|
126 | 151 | output['output_type'] = 'pyout' |
|
152 | if 'application/json' in output: | |
|
153 | output['application/json'] = json.dumps(output['application/json']) | |
|
127 | 154 | from_mime_key(output) |
|
128 | 155 | from_mime_key(output.get('metadata', {})) |
|
129 | 156 | elif output['output_type'] == 'error': |
|
130 | 157 | output['output_type'] = 'pyerr' |
|
131 | 158 | elif output['output_type'] == 'display_data': |
|
159 | if 'application/json' in output: | |
|
160 | output['application/json'] = json.dumps(output['application/json']) | |
|
132 | 161 | from_mime_key(output) |
|
133 | 162 | from_mime_key(output.get('metadata', {})) |
|
163 | elif output['output_type'] == 'stream': | |
|
164 | output['stream'] = output.pop('name') | |
|
165 | output.pop('metadata') | |
|
134 | 166 | return output |
|
135 | 167 | |
|
136 | 168 | def upgrade_outputs(outputs): |
|
137 | 169 | """upgrade outputs of a code cell from v3 to v4""" |
|
138 | 170 | return [upgrade_output(op) for op in outputs] |
|
139 | 171 | |
|
140 | 172 | def downgrade_outputs(outputs): |
|
141 | 173 | """downgrade outputs of a code cell to v3 from v4""" |
|
142 | 174 | return [downgrade_output(op) for op in outputs] |
|
143 | 175 | |
|
144 | 176 | def downgrade(nb): |
|
145 | 177 | """Convert a v4 notebook to v3. |
|
146 | 178 | |
|
147 | 179 | Parameters |
|
148 | 180 | ---------- |
|
149 | 181 | nb : NotebookNode |
|
150 | 182 | The Python representation of the notebook to convert. |
|
151 | 183 | """ |
|
184 | from IPython.nbformat.current import validate | |
|
185 | # Validate the notebook before conversion | |
|
186 | validate(nb, version=nbformat) | |
|
187 | ||
|
152 | 188 | if nb.nbformat != 4: |
|
153 | 189 | return nb |
|
154 | 190 | nb.nbformat = v3.nbformat |
|
155 | 191 | nb.nbformat_minor = v3.nbformat_minor |
|
156 | cells = [ downgrade_cell(cell) for cell in nb.cells ] | |
|
192 | cells = [ downgrade_cell(cell) for cell in nb.pop('cells') ] | |
|
157 | 193 | nb.worksheets = [v3.new_worksheet(cells=cells)] |
|
158 | 194 | nb.metadata.setdefault('name', '') |
|
195 | nb.metadata.pop('orig_nbformat', None) | |
|
196 | nb.metadata.pop('orig_nbformat_minor', None) | |
|
197 | ||
|
198 | # Validate the converted notebook before returning it | |
|
199 | validate(nb, version=v3.nbformat) | |
|
159 | 200 | return nb |
General Comments 0
You need to be logged in to leave comments.
Login now