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