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