Show More
@@ -1,199 +1,199 b'' | |||||
1 | """The official API for working with notebooks in the current format version. |
|
1 | """The official API for working with notebooks in the current format version. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | * Jonathan Frederic |
|
6 | * Jonathan Frederic | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | # Copyright (C) 2008-2011 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | from __future__ import print_function |
|
20 | from __future__ import print_function | |
21 |
|
21 | |||
22 | from xml.etree import ElementTree as ET |
|
22 | from xml.etree import ElementTree as ET | |
23 | import re |
|
23 | import re | |
24 |
|
24 | |||
25 | from IPython.nbformat.v3 import ( |
|
25 | from IPython.nbformat.v3 import ( | |
26 | NotebookNode, |
|
26 | NotebookNode, | |
27 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
27 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, | |
28 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, |
|
28 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, | |
29 | nbformat_minor, |
|
29 | nbformat_minor, | |
30 | ) |
|
30 | ) | |
31 |
|
31 | |||
32 | import reader |
|
32 | import reader | |
33 | from .convert import convert |
|
33 | from .convert import convert | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Code |
|
36 | # Code | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | current_nbformat = nbformat |
|
39 | current_nbformat = nbformat | |
40 | current_nbformat_minor = nbformat_minor |
|
40 | current_nbformat_minor = nbformat_minor | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | class NBFormatError(ValueError): |
|
43 | class NBFormatError(ValueError): | |
44 | pass |
|
44 | pass | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | def parse_py(s, **kwargs): |
|
47 | def parse_py(s, **kwargs): | |
48 | """Parse a string into a (nbformat, string) tuple.""" |
|
48 | """Parse a string into a (nbformat, string) tuple.""" | |
49 | nbf = current_nbformat |
|
49 | nbf = current_nbformat | |
50 | nbm = current_nbformat_minor |
|
50 | nbm = current_nbformat_minor | |
51 |
|
51 | |||
52 | pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>' |
|
52 | pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>' | |
53 | m = re.search(pattern,s) |
|
53 | m = re.search(pattern,s) | |
54 | if m is not None: |
|
54 | if m is not None: | |
55 | digits = m.group('nbformat').split('.') |
|
55 | digits = m.group('nbformat').split('.') | |
56 | nbf = int(digits[0]) |
|
56 | nbf = int(digits[0]) | |
57 | if len(digits) > 1: |
|
57 | if len(digits) > 1: | |
58 | nbm = int(digits[1]) |
|
58 | nbm = int(digits[1]) | |
59 |
|
59 | |||
60 | return nbf, nbm, s |
|
60 | return nbf, nbm, s | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | def reads_json(s, **kwargs): |
|
63 | def reads_json(s, **kwargs): | |
64 | """Read a JSON notebook from a string and return the NotebookNode object.""" |
|
64 | """Read a JSON notebook from a string and return the NotebookNode object.""" | |
65 |
return convert(reader.reads( |
|
65 | return convert(reader.reads(s), current_nbformat) | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | def writes_json(nb, **kwargs): |
|
68 | def writes_json(nb, **kwargs): | |
69 | return reader.versions[current_nbformat].writes_json(nb, **kwargs) |
|
69 | return reader.versions[current_nbformat].writes_json(nb, **kwargs) | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | def reads_py(s, **kwargs): |
|
72 | def reads_py(s, **kwargs): | |
73 | """Read a .py notebook from a string and return the NotebookNode object.""" |
|
73 | """Read a .py notebook from a string and return the NotebookNode object.""" | |
74 | nbf, nbm, s = parse_py(s, **kwargs) |
|
74 | nbf, nbm, s = parse_py(s, **kwargs) | |
75 | if nbf == 2 or nbf == 3: |
|
75 | if nbf == 2 or nbf == 3: | |
76 | nb = reader.versions[nbf].to_notebook_py(s, **kwargs) |
|
76 | nb = reader.versions[nbf].to_notebook_py(s, **kwargs) | |
77 | else: |
|
77 | else: | |
78 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbf) |
|
78 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbf) | |
79 | return nb |
|
79 | return nb | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | def writes_py(nb, **kwargs): |
|
82 | def writes_py(nb, **kwargs): | |
83 | # nbformat 3 is the latest format that supports py |
|
83 | # nbformat 3 is the latest format that supports py | |
84 | return reader.versions[3].writes_py(nb, **kwargs) |
|
84 | return reader.versions[3].writes_py(nb, **kwargs) | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | # High level API |
|
87 | # High level API | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | def reads(s, format, **kwargs): |
|
90 | def reads(s, format, **kwargs): | |
91 | """Read a notebook from a string and return the NotebookNode object. |
|
91 | """Read a notebook from a string and return the NotebookNode object. | |
92 |
|
92 | |||
93 | This function properly handles notebooks of any version. The notebook |
|
93 | This function properly handles notebooks of any version. The notebook | |
94 | returned will always be in the current version's format. |
|
94 | returned will always be in the current version's format. | |
95 |
|
95 | |||
96 | Parameters |
|
96 | Parameters | |
97 | ---------- |
|
97 | ---------- | |
98 | s : unicode |
|
98 | s : unicode | |
99 | The raw unicode string to read the notebook from. |
|
99 | The raw unicode string to read the notebook from. | |
100 | format : (u'json', u'ipynb', u'py') |
|
100 | format : (u'json', u'ipynb', u'py') | |
101 | The format that the string is in. |
|
101 | The format that the string is in. | |
102 |
|
102 | |||
103 | Returns |
|
103 | Returns | |
104 | ------- |
|
104 | ------- | |
105 | nb : NotebookNode |
|
105 | nb : NotebookNode | |
106 | The notebook that was read. |
|
106 | The notebook that was read. | |
107 | """ |
|
107 | """ | |
108 | format = unicode(format) |
|
108 | format = unicode(format) | |
109 | if format == u'json' or format == u'ipynb': |
|
109 | if format == u'json' or format == u'ipynb': | |
110 | return reads_json(s, **kwargs) |
|
110 | return reads_json(s, **kwargs) | |
111 | elif format == u'py': |
|
111 | elif format == u'py': | |
112 | return reads_py(s, **kwargs) |
|
112 | return reads_py(s, **kwargs) | |
113 | else: |
|
113 | else: | |
114 | raise NBFormatError('Unsupported format: %s' % format) |
|
114 | raise NBFormatError('Unsupported format: %s' % format) | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | def writes(nb, format, **kwargs): |
|
117 | def writes(nb, format, **kwargs): | |
118 | """Write a notebook to a string in a given format in the current nbformat version. |
|
118 | """Write a notebook to a string in a given format in the current nbformat version. | |
119 |
|
119 | |||
120 | This function always writes the notebook in the current nbformat version. |
|
120 | This function always writes the notebook in the current nbformat version. | |
121 |
|
121 | |||
122 | Parameters |
|
122 | Parameters | |
123 | ---------- |
|
123 | ---------- | |
124 | nb : NotebookNode |
|
124 | nb : NotebookNode | |
125 | The notebook to write. |
|
125 | The notebook to write. | |
126 | format : (u'json', u'ipynb', u'py') |
|
126 | format : (u'json', u'ipynb', u'py') | |
127 | The format to write the notebook in. |
|
127 | The format to write the notebook in. | |
128 |
|
128 | |||
129 | Returns |
|
129 | Returns | |
130 | ------- |
|
130 | ------- | |
131 | s : unicode |
|
131 | s : unicode | |
132 | The notebook string. |
|
132 | The notebook string. | |
133 | """ |
|
133 | """ | |
134 | format = unicode(format) |
|
134 | format = unicode(format) | |
135 | if format == u'json' or format == u'ipynb': |
|
135 | if format == u'json' or format == u'ipynb': | |
136 | return writes_json(nb, **kwargs) |
|
136 | return writes_json(nb, **kwargs) | |
137 | elif format == u'py': |
|
137 | elif format == u'py': | |
138 | return writes_py(nb, **kwargs) |
|
138 | return writes_py(nb, **kwargs) | |
139 | else: |
|
139 | else: | |
140 | raise NBFormatError('Unsupported format: %s' % format) |
|
140 | raise NBFormatError('Unsupported format: %s' % format) | |
141 |
|
141 | |||
142 |
|
142 | |||
143 | def read(fp, format, **kwargs): |
|
143 | def read(fp, format, **kwargs): | |
144 | """Read a notebook from a file and return the NotebookNode object. |
|
144 | """Read a notebook from a file and return the NotebookNode object. | |
145 |
|
145 | |||
146 | This function properly handles notebooks of any version. The notebook |
|
146 | This function properly handles notebooks of any version. The notebook | |
147 | returned will always be in the current version's format. |
|
147 | returned will always be in the current version's format. | |
148 |
|
148 | |||
149 | Parameters |
|
149 | Parameters | |
150 | ---------- |
|
150 | ---------- | |
151 | fp : file |
|
151 | fp : file | |
152 | Any file-like object with a read method. |
|
152 | Any file-like object with a read method. | |
153 | format : (u'json', u'ipynb', u'py') |
|
153 | format : (u'json', u'ipynb', u'py') | |
154 | The format that the string is in. |
|
154 | The format that the string is in. | |
155 |
|
155 | |||
156 | Returns |
|
156 | Returns | |
157 | ------- |
|
157 | ------- | |
158 | nb : NotebookNode |
|
158 | nb : NotebookNode | |
159 | The notebook that was read. |
|
159 | The notebook that was read. | |
160 | """ |
|
160 | """ | |
161 | return reads(fp.read(), format, **kwargs) |
|
161 | return reads(fp.read(), format, **kwargs) | |
162 |
|
162 | |||
163 |
|
163 | |||
164 | def write(nb, fp, format, **kwargs): |
|
164 | def write(nb, fp, format, **kwargs): | |
165 | """Write a notebook to a file in a given format in the current nbformat version. |
|
165 | """Write a notebook to a file in a given format in the current nbformat version. | |
166 |
|
166 | |||
167 | This function always writes the notebook in the current nbformat version. |
|
167 | This function always writes the notebook in the current nbformat version. | |
168 |
|
168 | |||
169 | Parameters |
|
169 | Parameters | |
170 | ---------- |
|
170 | ---------- | |
171 | nb : NotebookNode |
|
171 | nb : NotebookNode | |
172 | The notebook to write. |
|
172 | The notebook to write. | |
173 | fp : file |
|
173 | fp : file | |
174 | Any file-like object with a write method. |
|
174 | Any file-like object with a write method. | |
175 | format : (u'json', u'ipynb', u'py') |
|
175 | format : (u'json', u'ipynb', u'py') | |
176 | The format to write the notebook in. |
|
176 | The format to write the notebook in. | |
177 |
|
177 | |||
178 | Returns |
|
178 | Returns | |
179 | ------- |
|
179 | ------- | |
180 | s : unicode |
|
180 | s : unicode | |
181 | The notebook string. |
|
181 | The notebook string. | |
182 | """ |
|
182 | """ | |
183 | return fp.write(writes(nb, format, **kwargs)) |
|
183 | return fp.write(writes(nb, format, **kwargs)) | |
184 |
|
184 | |||
185 | def _convert_to_metadata(): |
|
185 | def _convert_to_metadata(): | |
186 | """Convert to a notebook having notebook metadata.""" |
|
186 | """Convert to a notebook having notebook metadata.""" | |
187 | import glob |
|
187 | import glob | |
188 | for fname in glob.glob('*.ipynb'): |
|
188 | for fname in glob.glob('*.ipynb'): | |
189 | print('Converting file:',fname) |
|
189 | print('Converting file:',fname) | |
190 | with open(fname,'r') as f: |
|
190 | with open(fname,'r') as f: | |
191 | nb = read(f,u'json') |
|
191 | nb = read(f,u'json') | |
192 | md = new_metadata() |
|
192 | md = new_metadata() | |
193 | if u'name' in nb: |
|
193 | if u'name' in nb: | |
194 | md.name = nb.name |
|
194 | md.name = nb.name | |
195 | del nb[u'name'] |
|
195 | del nb[u'name'] | |
196 | nb.metadata = md |
|
196 | nb.metadata = md | |
197 | with open(fname,'w') as f: |
|
197 | with open(fname,'w') as f: | |
198 | write(nb, f, u'json') |
|
198 | write(nb, f, u'json') | |
199 |
|
199 |
@@ -1,100 +1,106 b'' | |||||
1 | """API for reading notebooks. |
|
1 | """API for reading notebooks. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Jonathan Frederic |
|
5 | * Jonathan Frederic | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2013 The IPython Development Team |
|
9 | # Copyright (C) 2013 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import json |
|
19 | import json | |
20 |
|
20 | |||
21 | versions = {} |
|
21 | import v1 | |
22 | for i in range(3): |
|
22 | import v2 | |
23 | versions[i+1] = __import__('v{0}'.format(i+1)) |
|
23 | import v3 | |
|
24 | ||||
|
25 | versions = { | |||
|
26 | 1: v1, | |||
|
27 | 2: v2, | |||
|
28 | 3: v3, | |||
|
29 | } | |||
24 |
|
30 | |||
25 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
26 | # Code |
|
32 | # Code | |
27 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
28 |
|
34 | |||
29 | class NotJSONError(ValueError): |
|
35 | class NotJSONError(ValueError): | |
30 | pass |
|
36 | pass | |
31 |
|
37 | |||
32 | def parse_json(s, **kwargs): |
|
38 | def parse_json(s, **kwargs): | |
33 | """Parse a JSON string into a dict.""" |
|
39 | """Parse a JSON string into a dict.""" | |
34 | try: |
|
40 | try: | |
35 | nb_dict = json.loads(s, **kwargs) |
|
41 | nb_dict = json.loads(s, **kwargs) | |
36 | except ValueError: |
|
42 | except ValueError: | |
37 | raise NotJSONError("Notebook does not appear to be JSON: %r" % s[:16]) |
|
43 | raise NotJSONError("Notebook does not appear to be JSON: %r" % s[:16]) | |
38 | return nb_dict |
|
44 | return nb_dict | |
39 |
|
45 | |||
40 | # High level API |
|
46 | # High level API | |
41 |
|
47 | |||
42 | def get_version(nb): |
|
48 | def get_version(nb): | |
43 | """Get the version of a notebook. |
|
49 | """Get the version of a notebook. | |
44 |
|
50 | |||
45 | Parameters |
|
51 | Parameters | |
46 | ---------- |
|
52 | ---------- | |
47 | nb : dict |
|
53 | nb : dict | |
48 | NotebookNode or dict containing notebook data. |
|
54 | NotebookNode or dict containing notebook data. | |
49 |
|
55 | |||
50 | Returns |
|
56 | Returns | |
51 | ------- |
|
57 | ------- | |
52 | Tuple containing major (int) and minor (int) version numbers |
|
58 | Tuple containing major (int) and minor (int) version numbers | |
53 | """ |
|
59 | """ | |
54 | major = nb.get('nbformat', 1) |
|
60 | major = nb.get('nbformat', 1) | |
55 | minor = nb.get('nbformat_minor', 0) |
|
61 | minor = nb.get('nbformat_minor', 0) | |
56 | return (major, minor) |
|
62 | return (major, minor) | |
57 |
|
63 | |||
58 |
|
64 | |||
59 | def reads(s, format='ipynb', **kwargs): |
|
65 | def reads(s, format='ipynb', **kwargs): | |
60 | """Read a notebook from a 'ipynb' (json) string and return the |
|
66 | """Read a notebook from a 'ipynb' (json) string and return the | |
61 | NotebookNode object. |
|
67 | NotebookNode object. | |
62 |
|
68 | |||
63 | This function properly reads notebooks of any version. No version |
|
69 | This function properly reads notebooks of any version. No version | |
64 | conversion is performed. |
|
70 | conversion is performed. | |
65 |
|
71 | |||
66 | Parameters |
|
72 | Parameters | |
67 | ---------- |
|
73 | ---------- | |
68 | s : unicode |
|
74 | s : unicode | |
69 | The raw unicode string to read the notebook from. |
|
75 | The raw unicode string to read the notebook from. | |
70 |
|
76 | |||
71 | Returns |
|
77 | Returns | |
72 | ------- |
|
78 | ------- | |
73 | nb : NotebookNode |
|
79 | nb : NotebookNode | |
74 | The notebook that was read. |
|
80 | The notebook that was read. | |
75 | """ |
|
81 | """ | |
76 | nb_dict = parse_json(s, **kwargs) |
|
82 | nb_dict = parse_json(s, **kwargs) | |
77 | (major, minor) = get_version(nb_dict) |
|
83 | (major, minor) = get_version(nb_dict) | |
78 | if major in versions: |
|
84 | if major in versions: | |
79 | return versions[major].to_notebook_json(nb, minor=minor) |
|
85 | return versions[major].to_notebook_json(nb_dict, minor=minor) | |
80 | else: |
|
86 | else: | |
81 | raise NBFormatError('Unsupported nbformat version %s' % major) |
|
87 | raise NBFormatError('Unsupported nbformat version %s' % major) | |
82 |
|
88 | |||
83 |
|
89 | |||
84 | def read(fp, **kwargs): |
|
90 | def read(fp, **kwargs): | |
85 | """Read a notebook from a file and return the NotebookNode object. |
|
91 | """Read a notebook from a file and return the NotebookNode object. | |
86 |
|
92 | |||
87 | This function properly reads notebooks of any version. No version |
|
93 | This function properly reads notebooks of any version. No version | |
88 | conversion is performed. |
|
94 | conversion is performed. | |
89 |
|
95 | |||
90 | Parameters |
|
96 | Parameters | |
91 | ---------- |
|
97 | ---------- | |
92 | fp : file |
|
98 | fp : file | |
93 | Any file-like object with a read method. |
|
99 | Any file-like object with a read method. | |
94 |
|
100 | |||
95 | Returns |
|
101 | Returns | |
96 | ------- |
|
102 | ------- | |
97 | nb : NotebookNode |
|
103 | nb : NotebookNode | |
98 | The notebook that was read. |
|
104 | The notebook that was read. | |
99 | """ |
|
105 | """ | |
100 | return reads(fp.read(), format, **kwargs) |
|
106 | return reads(fp.read(), format, **kwargs) |
@@ -1,23 +1,23 b'' | |||||
1 | """The main module for the v1 notebook format.""" |
|
1 | """The main module for the v1 notebook format.""" | |
2 |
|
2 | |||
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (C) 2008-2011 The IPython Development Team |
|
4 | # Copyright (C) 2008-2011 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | from .nbbase import ( |
|
14 | from .nbbase import ( | |
15 | NotebookNode, |
|
15 | NotebookNode, | |
16 | new_code_cell, new_text_cell, new_notebook |
|
16 | new_code_cell, new_text_cell, new_notebook | |
17 | ) |
|
17 | ) | |
18 |
|
18 | |||
19 | from .nbjson import reads as reads_json, writes as writes_json |
|
19 | from .nbjson import reads as reads_json, writes as writes_json | |
20 | from .nbjson import reads as read_json, writes as write_json |
|
20 | from .nbjson import reads as read_json, writes as write_json | |
21 | from .nbjson import to_notebook as to_notebook_json |
|
21 | from .nbjson import to_notebook as to_notebook_json | |
22 |
|
22 | |||
23 |
from .convert import |
|
23 | from .convert import upgrade |
General Comments 0
You need to be logged in to leave comments.
Login now