Show More
@@ -1,230 +1,205 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 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
9 | # Copyright (C) 2008-2011 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 | from __future__ import print_function |
|
19 | from __future__ import print_function | |
20 | import json |
|
20 | import json | |
21 | from xml.etree import ElementTree as ET |
|
21 | from xml.etree import ElementTree as ET | |
22 | import re |
|
22 | import re | |
23 |
|
23 | |||
24 | from IPython.nbformat import v2 |
|
24 | from IPython.nbformat import v2 | |
25 | from IPython.nbformat import v1 |
|
25 | from IPython.nbformat import v1 | |
26 |
|
26 | |||
27 | from IPython.nbformat.v2 import ( |
|
27 | from IPython.nbformat.v2 import ( | |
28 | NotebookNode, |
|
28 | NotebookNode, | |
29 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
29 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, | |
30 | parse_filename, new_metadata, new_author |
|
30 | parse_filename, new_metadata, new_author | |
31 | ) |
|
31 | ) | |
32 |
|
32 | |||
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 | # Code |
|
34 | # Code | |
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 |
|
36 | |||
37 | current_nbformat = 2 |
|
37 | current_nbformat = 2 | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | class NBFormatError(Exception): |
|
40 | class NBFormatError(Exception): | |
41 | pass |
|
41 | pass | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | def parse_json(s, **kwargs): |
|
44 | def parse_json(s, **kwargs): | |
45 | """Parse a string into a (nbformat, dict) tuple.""" |
|
45 | """Parse a string into a (nbformat, dict) tuple.""" | |
46 | d = json.loads(s, **kwargs) |
|
46 | d = json.loads(s, **kwargs) | |
47 | nbformat = d.get('nbformat',1) |
|
47 | nbformat = d.get('nbformat',1) | |
48 | return nbformat, d |
|
48 | return nbformat, d | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def parse_xml(s, **kwargs): |
|
|||
52 | """Parse a string into a (nbformat, etree) tuple.""" |
|
|||
53 | root = ET.fromstring(s) |
|
|||
54 | nbformat_e = root.find('nbformat') |
|
|||
55 | if nbformat_e is not None: |
|
|||
56 | nbformat = int(nbformat_e.text) |
|
|||
57 | else: |
|
|||
58 | raise NBFormatError('No nbformat version found') |
|
|||
59 | return nbformat, root |
|
|||
60 |
|
||||
61 |
|
||||
62 | def parse_py(s, **kwargs): |
|
51 | def parse_py(s, **kwargs): | |
63 | """Parse a string into a (nbformat, string) tuple.""" |
|
52 | """Parse a string into a (nbformat, string) tuple.""" | |
64 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' |
|
53 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' | |
65 | m = re.search(pattern,s) |
|
54 | m = re.search(pattern,s) | |
66 | if m is not None: |
|
55 | if m is not None: | |
67 | nbformat = int(m.group('nbformat')) |
|
56 | nbformat = int(m.group('nbformat')) | |
68 | else: |
|
57 | else: | |
69 | nbformat = 2 |
|
58 | nbformat = 2 | |
70 | return nbformat, s |
|
59 | return nbformat, s | |
71 |
|
60 | |||
72 |
|
61 | |||
73 | def reads_json(s, **kwargs): |
|
62 | def reads_json(s, **kwargs): | |
74 | """Read a JSON notebook from a string and return the NotebookNode object.""" |
|
63 | """Read a JSON notebook from a string and return the NotebookNode object.""" | |
75 | nbformat, d = parse_json(s, **kwargs) |
|
64 | nbformat, d = parse_json(s, **kwargs) | |
76 | if nbformat == 1: |
|
65 | if nbformat == 1: | |
77 | nb = v1.to_notebook_json(d, **kwargs) |
|
66 | nb = v1.to_notebook_json(d, **kwargs) | |
78 | nb = v2.convert_to_this_nbformat(nb, orig_version=1) |
|
67 | nb = v2.convert_to_this_nbformat(nb, orig_version=1) | |
79 | elif nbformat == 2: |
|
68 | elif nbformat == 2: | |
80 | nb = v2.to_notebook_json(d, **kwargs) |
|
69 | nb = v2.to_notebook_json(d, **kwargs) | |
81 | else: |
|
70 | else: | |
82 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbformat) |
|
71 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbformat) | |
83 | return nb |
|
72 | return nb | |
84 |
|
73 | |||
85 |
|
74 | |||
86 | def writes_json(nb, **kwargs): |
|
75 | def writes_json(nb, **kwargs): | |
87 | return v2.writes_json(nb, **kwargs) |
|
76 | return v2.writes_json(nb, **kwargs) | |
88 |
|
77 | |||
89 |
|
78 | |||
90 | def reads_xml(s, **kwargs): |
|
|||
91 | """Read an XML notebook from a string and return the NotebookNode object.""" |
|
|||
92 | nbformat, root = parse_xml(s, **kwargs) |
|
|||
93 | if nbformat == 2: |
|
|||
94 | nb = v2.to_notebook_xml(root, **kwargs) |
|
|||
95 | else: |
|
|||
96 | raise NBFormatError('Unsupported XML nbformat version: %i' % nbformat) |
|
|||
97 | return nb |
|
|||
98 |
|
||||
99 |
|
||||
100 | def reads_py(s, **kwargs): |
|
79 | def reads_py(s, **kwargs): | |
101 | """Read a .py notebook from a string and return the NotebookNode object.""" |
|
80 | """Read a .py notebook from a string and return the NotebookNode object.""" | |
102 | nbformat, s = parse_py(s, **kwargs) |
|
81 | nbformat, s = parse_py(s, **kwargs) | |
103 | if nbformat == 2: |
|
82 | if nbformat == 2: | |
104 | nb = v2.to_notebook_py(s, **kwargs) |
|
83 | nb = v2.to_notebook_py(s, **kwargs) | |
105 | else: |
|
84 | else: | |
106 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbformat) |
|
85 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbformat) | |
107 | return nb |
|
86 | return nb | |
108 |
|
87 | |||
109 |
|
88 | |||
110 | def writes_py(nb, **kwargs): |
|
89 | def writes_py(nb, **kwargs): | |
111 | return v2.writes_py(nb, **kwargs) |
|
90 | return v2.writes_py(nb, **kwargs) | |
112 |
|
91 | |||
113 |
|
92 | |||
114 | # High level API |
|
93 | # High level API | |
115 |
|
94 | |||
116 |
|
95 | |||
117 | def reads(s, format, **kwargs): |
|
96 | def reads(s, format, **kwargs): | |
118 | """Read a notebook from a string and return the NotebookNode object. |
|
97 | """Read a notebook from a string and return the NotebookNode object. | |
119 |
|
98 | |||
120 | This function properly handles notebooks of any version. The notebook |
|
99 | This function properly handles notebooks of any version. The notebook | |
121 | returned will always be in the current version's format. |
|
100 | returned will always be in the current version's format. | |
122 |
|
101 | |||
123 | Parameters |
|
102 | Parameters | |
124 | ---------- |
|
103 | ---------- | |
125 | s : unicode |
|
104 | s : unicode | |
126 | The raw unicode string to read the notebook from. |
|
105 | The raw unicode string to read the notebook from. | |
127 | format : (u'json', u'ipynb', u'py') |
|
106 | format : (u'json', u'ipynb', u'py') | |
128 | The format that the string is in. |
|
107 | The format that the string is in. | |
129 |
|
108 | |||
130 | Returns |
|
109 | Returns | |
131 | ------- |
|
110 | ------- | |
132 | nb : NotebookNode |
|
111 | nb : NotebookNode | |
133 | The notebook that was read. |
|
112 | The notebook that was read. | |
134 | """ |
|
113 | """ | |
135 | format = unicode(format) |
|
114 | format = unicode(format) | |
136 |
if format == u' |
|
115 | if format == u'json' or format == u'ipynb': | |
137 | return reads_xml(s, **kwargs) |
|
|||
138 | elif format == u'json' or format == u'ipynb': |
|
|||
139 | return reads_json(s, **kwargs) |
|
116 | return reads_json(s, **kwargs) | |
140 | elif format == u'py': |
|
117 | elif format == u'py': | |
141 | return reads_py(s, **kwargs) |
|
118 | return reads_py(s, **kwargs) | |
142 | else: |
|
119 | else: | |
143 | raise NBFormatError('Unsupported format: %s' % format) |
|
120 | raise NBFormatError('Unsupported format: %s' % format) | |
144 |
|
121 | |||
145 |
|
122 | |||
146 | def writes(nb, format, **kwargs): |
|
123 | def writes(nb, format, **kwargs): | |
147 | """Write a notebook to a string in a given format in the current nbformat version. |
|
124 | """Write a notebook to a string in a given format in the current nbformat version. | |
148 |
|
125 | |||
149 | This function always writes the notebook in the current nbformat version. |
|
126 | This function always writes the notebook in the current nbformat version. | |
150 |
|
127 | |||
151 | Parameters |
|
128 | Parameters | |
152 | ---------- |
|
129 | ---------- | |
153 | nb : NotebookNode |
|
130 | nb : NotebookNode | |
154 | The notebook to write. |
|
131 | The notebook to write. | |
155 | format : (u'json', u'ipynb', u'py') |
|
132 | format : (u'json', u'ipynb', u'py') | |
156 | The format to write the notebook in. |
|
133 | The format to write the notebook in. | |
157 |
|
134 | |||
158 | Returns |
|
135 | Returns | |
159 | ------- |
|
136 | ------- | |
160 | s : unicode |
|
137 | s : unicode | |
161 | The notebook string. |
|
138 | The notebook string. | |
162 | """ |
|
139 | """ | |
163 | format = unicode(format) |
|
140 | format = unicode(format) | |
164 |
if format == u' |
|
141 | if format == u'json' or format == u'ipynb': | |
165 | raise NotImplementedError('Write to XML files is not implemented.') |
|
|||
166 | elif format == u'json' or format == u'ipynb': |
|
|||
167 | return writes_json(nb, **kwargs) |
|
142 | return writes_json(nb, **kwargs) | |
168 | elif format == u'py': |
|
143 | elif format == u'py': | |
169 | return writes_py(nb, **kwargs) |
|
144 | return writes_py(nb, **kwargs) | |
170 | else: |
|
145 | else: | |
171 | raise NBFormatError('Unsupported format: %s' % format) |
|
146 | raise NBFormatError('Unsupported format: %s' % format) | |
172 |
|
147 | |||
173 |
|
148 | |||
174 | def read(fp, format, **kwargs): |
|
149 | def read(fp, format, **kwargs): | |
175 | """Read a notebook from a file and return the NotebookNode object. |
|
150 | """Read a notebook from a file and return the NotebookNode object. | |
176 |
|
151 | |||
177 | This function properly handles notebooks of any version. The notebook |
|
152 | This function properly handles notebooks of any version. The notebook | |
178 | returned will always be in the current version's format. |
|
153 | returned will always be in the current version's format. | |
179 |
|
154 | |||
180 | Parameters |
|
155 | Parameters | |
181 | ---------- |
|
156 | ---------- | |
182 | fp : file |
|
157 | fp : file | |
183 | Any file-like object with a read method. |
|
158 | Any file-like object with a read method. | |
184 | format : (u'json', u'ipynb', u'py') |
|
159 | format : (u'json', u'ipynb', u'py') | |
185 | The format that the string is in. |
|
160 | The format that the string is in. | |
186 |
|
161 | |||
187 | Returns |
|
162 | Returns | |
188 | ------- |
|
163 | ------- | |
189 | nb : NotebookNode |
|
164 | nb : NotebookNode | |
190 | The notebook that was read. |
|
165 | The notebook that was read. | |
191 | """ |
|
166 | """ | |
192 | return reads(fp.read(), format, **kwargs) |
|
167 | return reads(fp.read(), format, **kwargs) | |
193 |
|
168 | |||
194 |
|
169 | |||
195 | def write(nb, fp, format, **kwargs): |
|
170 | def write(nb, fp, format, **kwargs): | |
196 | """Write a notebook to a file in a given format in the current nbformat version. |
|
171 | """Write a notebook to a file in a given format in the current nbformat version. | |
197 |
|
172 | |||
198 | This function always writes the notebook in the current nbformat version. |
|
173 | This function always writes the notebook in the current nbformat version. | |
199 |
|
174 | |||
200 | Parameters |
|
175 | Parameters | |
201 | ---------- |
|
176 | ---------- | |
202 | nb : NotebookNode |
|
177 | nb : NotebookNode | |
203 | The notebook to write. |
|
178 | The notebook to write. | |
204 | fp : file |
|
179 | fp : file | |
205 | Any file-like object with a write method. |
|
180 | Any file-like object with a write method. | |
206 | format : (u'json', u'ipynb', u'py') |
|
181 | format : (u'json', u'ipynb', u'py') | |
207 | The format to write the notebook in. |
|
182 | The format to write the notebook in. | |
208 |
|
183 | |||
209 | Returns |
|
184 | Returns | |
210 | ------- |
|
185 | ------- | |
211 | s : unicode |
|
186 | s : unicode | |
212 | The notebook string. |
|
187 | The notebook string. | |
213 | """ |
|
188 | """ | |
214 | return fp.write(writes(nb, format, **kwargs)) |
|
189 | return fp.write(writes(nb, format, **kwargs)) | |
215 |
|
190 | |||
216 | def _convert_to_metadata(): |
|
191 | def _convert_to_metadata(): | |
217 | """Convert to a notebook having notebook metadata.""" |
|
192 | """Convert to a notebook having notebook metadata.""" | |
218 | import glob |
|
193 | import glob | |
219 | for fname in glob.glob('*.ipynb'): |
|
194 | for fname in glob.glob('*.ipynb'): | |
220 | print('Converting file:',fname) |
|
195 | print('Converting file:',fname) | |
221 | with open(fname,'r') as f: |
|
196 | with open(fname,'r') as f: | |
222 | nb = read(f,u'json') |
|
197 | nb = read(f,u'json') | |
223 | md = new_metadata() |
|
198 | md = new_metadata() | |
224 | if u'name' in nb: |
|
199 | if u'name' in nb: | |
225 | md.name = nb.name |
|
200 | md.name = nb.name | |
226 | del nb[u'name'] |
|
201 | del nb[u'name'] | |
227 | nb.metadata = md |
|
202 | nb.metadata = md | |
228 | with open(fname,'w') as f: |
|
203 | with open(fname,'w') as f: | |
229 | write(nb, f, u'json') |
|
204 | write(nb, f, u'json') | |
230 |
|
205 |
@@ -1,78 +1,74 b'' | |||||
1 | """The main API for the v2 notebook format. |
|
1 | """The main API for the v2 notebook format. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
9 | # Copyright (C) 2008-2011 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 | 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 |
|
22 | new_metadata, new_author | |
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 | |
26 | from .nbjson import reads as read_json, writes as write_json |
|
26 | from .nbjson import reads as read_json, writes as write_json | |
27 | from .nbjson import to_notebook as to_notebook_json |
|
27 | from .nbjson import to_notebook as to_notebook_json | |
28 |
|
28 | |||
29 | from .nbxml import reads as reads_xml |
|
|||
30 | from .nbxml import reads as read_xml |
|
|||
31 | from .nbxml import to_notebook as to_notebook_xml |
|
|||
32 |
|
||||
33 | from .nbpy import reads as reads_py, writes as writes_py |
|
29 | from .nbpy import reads as reads_py, writes as writes_py | |
34 | from .nbpy import reads as read_py, writes as write_py |
|
30 | from .nbpy import reads as read_py, writes as write_py | |
35 | from .nbpy import to_notebook as to_notebook_py |
|
31 | from .nbpy import to_notebook as to_notebook_py | |
36 |
|
32 | |||
37 | from .convert import convert_to_this_nbformat |
|
33 | from .convert import convert_to_this_nbformat | |
38 |
|
34 | |||
39 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
40 | # Code |
|
36 | # Code | |
41 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
42 |
|
38 | |||
43 | def parse_filename(fname): |
|
39 | def parse_filename(fname): | |
44 | """Parse a notebook filename. |
|
40 | """Parse a notebook filename. | |
45 |
|
41 | |||
46 | This function takes a notebook filename and returns the notebook |
|
42 | This function takes a notebook filename and returns the notebook | |
47 | format (json/py) and the notebook name. This logic can be |
|
43 | format (json/py) and the notebook name. This logic can be | |
48 | summarized as follows: |
|
44 | summarized as follows: | |
49 |
|
45 | |||
50 | * notebook.ipynb -> (notebook.ipynb, notebook, json) |
|
46 | * notebook.ipynb -> (notebook.ipynb, notebook, json) | |
51 | * notebook.json -> (notebook.json, notebook, json) |
|
47 | * notebook.json -> (notebook.json, notebook, json) | |
52 | * notebook.py -> (notebook.py, notebook, py) |
|
48 | * notebook.py -> (notebook.py, notebook, py) | |
53 | * notebook -> (notebook.ipynb, notebook, json) |
|
49 | * notebook -> (notebook.ipynb, notebook, json) | |
54 |
|
50 | |||
55 | Parameters |
|
51 | Parameters | |
56 | ---------- |
|
52 | ---------- | |
57 | fname : unicode |
|
53 | fname : unicode | |
58 | The notebook filename. The filename can use a specific filename |
|
54 | The notebook filename. The filename can use a specific filename | |
59 | extention (.ipynb, .json, .py) or none, in which case .ipynb will |
|
55 | extention (.ipynb, .json, .py) or none, in which case .ipynb will | |
60 | be assumed. |
|
56 | be assumed. | |
61 |
|
57 | |||
62 | Returns |
|
58 | Returns | |
63 | ------- |
|
59 | ------- | |
64 | (fname, name, format) : (unicode, unicode, unicode) |
|
60 | (fname, name, format) : (unicode, unicode, unicode) | |
65 | The filename, notebook name and format. |
|
61 | The filename, notebook name and format. | |
66 | """ |
|
62 | """ | |
67 | if fname.endswith(u'.ipynb'): |
|
63 | if fname.endswith(u'.ipynb'): | |
68 | format = u'json' |
|
64 | format = u'json' | |
69 | elif fname.endswith(u'.json'): |
|
65 | elif fname.endswith(u'.json'): | |
70 | format = u'json' |
|
66 | format = u'json' | |
71 | elif fname.endswith(u'.py'): |
|
67 | elif fname.endswith(u'.py'): | |
72 | format = u'py' |
|
68 | format = u'py' | |
73 | else: |
|
69 | else: | |
74 | fname = fname + u'.ipynb' |
|
70 | fname = fname + u'.ipynb' | |
75 | format = u'json' |
|
71 | format = u'json' | |
76 | name = fname.split('.')[0] |
|
72 | name = fname.split('.')[0] | |
77 | return fname, name, format |
|
73 | return fname, name, format | |
78 |
|
74 |
General Comments 0
You need to be logged in to leave comments.
Login now