Show More
@@ -28,7 +28,8 b' from IPython.nbformat import v1' | |||
|
28 | 28 | from IPython.nbformat.v3 import ( |
|
29 | 29 | NotebookNode, |
|
30 | 30 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
31 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat | |
|
31 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, | |
|
32 | nbformat_minor, | |
|
32 | 33 | ) |
|
33 | 34 | |
|
34 | 35 | #----------------------------------------------------------------------------- |
@@ -36,6 +37,8 b' from IPython.nbformat.v3 import (' | |||
|
36 | 37 | #----------------------------------------------------------------------------- |
|
37 | 38 | |
|
38 | 39 | current_nbformat = nbformat |
|
40 | current_nbformat_minor = nbformat_minor | |
|
41 | ||
|
39 | 42 | |
|
40 | 43 | |
|
41 | 44 | class NBFormatError(Exception): |
@@ -45,8 +48,9 b' class NBFormatError(Exception):' | |||
|
45 | 48 | def parse_json(s, **kwargs): |
|
46 | 49 | """Parse a string into a (nbformat, dict) tuple.""" |
|
47 | 50 | d = json.loads(s, **kwargs) |
|
48 | nbf = d.get('nbformat',1) | |
|
49 | return nbf, d | |
|
51 | nbf = d.get('nbformat', 1) | |
|
52 | nbm = d.get('nbformat_minor', 0) | |
|
53 | return nbf, nbm, d | |
|
50 | 54 | |
|
51 | 55 | |
|
52 | 56 | def parse_py(s, **kwargs): |
@@ -62,9 +66,7 b' def parse_py(s, **kwargs):' | |||
|
62 | 66 | |
|
63 | 67 | def reads_json(s, **kwargs): |
|
64 | 68 | """Read a JSON notebook from a string and return the NotebookNode object.""" |
|
65 | nbf, d = parse_json(s, **kwargs) | |
|
66 | # cast int for reading - 3.x notebooks should be readable on 3.0, etc. | |
|
67 | nbf = int(nbf) | |
|
69 | nbf, minor, d = parse_json(s, **kwargs) | |
|
68 | 70 | if nbf == 1: |
|
69 | 71 | nb = v1.to_notebook_json(d, **kwargs) |
|
70 | 72 | nb = v3.convert_to_this_nbformat(nb, orig_version=1) |
@@ -73,6 +75,7 b' def reads_json(s, **kwargs):' | |||
|
73 | 75 | nb = v3.convert_to_this_nbformat(nb, orig_version=2) |
|
74 | 76 | elif nbf == 3: |
|
75 | 77 | nb = v3.to_notebook_json(d, **kwargs) |
|
78 | nb = v3.convert_to_this_nbformat(nb, orig_version=3, orig_minor=minor) | |
|
76 | 79 | else: |
|
77 | 80 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbf) |
|
78 | 81 | return nb |
@@ -85,8 +88,7 b' def writes_json(nb, **kwargs):' | |||
|
85 | 88 | def reads_py(s, **kwargs): |
|
86 | 89 | """Read a .py notebook from a string and return the NotebookNode object.""" |
|
87 | 90 | nbf, s = parse_py(s, **kwargs) |
|
88 | # cast int for reading - 3.x notebooks should be readable on 3.0, etc. | |
|
89 | nbf = int(nbf) | |
|
91 | nbf = nbf | |
|
90 | 92 | if nbf == 2: |
|
91 | 93 | nb = v2.to_notebook_py(s, **kwargs) |
|
92 | 94 | elif nbf == 3: |
@@ -19,7 +19,7 b' Authors:' | |||
|
19 | 19 | from .nbbase import ( |
|
20 | 20 | NotebookNode, |
|
21 | 21 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
22 | new_metadata, new_author, new_heading_cell, nbformat | |
|
22 | new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor | |
|
23 | 23 | ) |
|
24 | 24 | |
|
25 | 25 | from .nbjson import reads as reads_json, writes as writes_json |
@@ -17,7 +17,8 b' Authors:' | |||
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from .nbbase import ( |
|
20 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output | |
|
20 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, | |
|
21 | nbformat, nbformat_minor | |
|
21 | 22 | ) |
|
22 | 23 | |
|
23 | 24 | from IPython.nbformat import v2 |
@@ -26,7 +27,7 b' from IPython.nbformat import v2' | |||
|
26 | 27 | # Code |
|
27 | 28 | #----------------------------------------------------------------------------- |
|
28 | 29 | |
|
29 | def convert_to_this_nbformat(nb, orig_version=2): | |
|
30 | def convert_to_this_nbformat(nb, orig_version=2, orig_minor=0): | |
|
30 | 31 | """Convert a notebook to the v3 format. |
|
31 | 32 | |
|
32 | 33 | Parameters |
@@ -35,16 +36,23 b' def convert_to_this_nbformat(nb, orig_version=2):' | |||
|
35 | 36 | The Python representation of the notebook to convert. |
|
36 | 37 | orig_version : int |
|
37 | 38 | The original version of the notebook to convert. |
|
39 | orig_minor : int | |
|
40 | The original minor version of the notebook to convert (only relevant for v >= 3). | |
|
38 | 41 | """ |
|
39 | 42 | if orig_version == 1: |
|
40 | 43 | nb = v2.convert_to_this_nbformat(nb) |
|
41 | 44 | orig_version = 2 |
|
42 | 45 | if orig_version == 2: |
|
43 | 46 | # Mark the original nbformat so consumers know it has been converted. |
|
44 |
nb.nbformat = |
|
|
47 | nb.nbformat = nbformat | |
|
48 | nb.nbformat_minor = nbformat_minor | |
|
49 | ||
|
45 | 50 | nb.orig_nbformat = 2 |
|
46 | 51 | return nb |
|
47 | 52 | elif orig_version == 3: |
|
53 | if orig_minor != nbformat_minor: | |
|
54 | nb.orig_nbformat_minor = orig_minor | |
|
55 | nb.nbformat_minor = nbformat_minor | |
|
48 | 56 | return nb |
|
49 | 57 | else: |
|
50 | 58 | raise ValueError('Cannot convert a notebook from v%s to v3' % orig_version) |
@@ -31,7 +31,8 b' from IPython.utils.ipstruct import Struct' | |||
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | # Change this when incrementing the nbformat version |
|
34 |
nbformat = 3 |
|
|
34 | nbformat = 3 | |
|
35 | nbformat_minor = 0 | |
|
35 | 36 | |
|
36 | 37 | class NotebookNode(Struct): |
|
37 | 38 | pass |
@@ -158,6 +159,7 b' def new_notebook(name=None, metadata=None, worksheets=None):' | |||
|
158 | 159 | """Create a notebook by name, id and a list of worksheets.""" |
|
159 | 160 | nb = NotebookNode() |
|
160 | 161 | nb.nbformat = nbformat |
|
162 | nb.nbformat_minor = nbformat_minor | |
|
161 | 163 | if worksheets is None: |
|
162 | 164 | nb.worksheets = [] |
|
163 | 165 | else: |
General Comments 0
You need to be logged in to leave comments.
Login now