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