##// END OF EJS Templates
test and update conversion between v3 <-> v4
MinRK -
Show More
@@ -0,0 +1,19 b''
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)
@@ -3,15 +3,15 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 .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.
@@ -25,12 +25,17 b' def upgrade(nb, from_version=3, from_minor=0):'
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,
@@ -39,14 +44,17 b' def upgrade(nb, from_version=3, from_minor=0):'
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. ' \
@@ -60,16 +68,24 b' def upgrade_cell(cell):'
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.metadata.pop('language', '')
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 = {
@@ -103,16 +119,24 b' def upgrade_output(output):'
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.get('metadata', {}))
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'] == 'display_data':
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):
@@ -121,16 +145,24 b' def downgrade_output(output):'
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):
@@ -149,11 +181,20 b' def downgrade(nb):'
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