##// END OF EJS Templates
Making JSON the default .ipynb format.
Brian E. Granger -
Show More
@@ -3526,7 +3526,7 b' Defaulting color scheme to \'NoColor\'"""'
3526 3526 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
3527 3527 To export the history to "foo.py" do "%notebook -e foo.py". To convert
3528 3528 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
3529 formats include (xml/ipynb, json, py).
3529 formats include (json/ipynb, py).
3530 3530 """
3531 3531 args = magic_arguments.parse_argstring(self.magic_notebook, s)
3532 3532
@@ -3544,17 +3544,21 b' Defaulting color scheme to \'NoColor\'"""'
3544 3544 elif args.format is not None:
3545 3545 old_fname, old_name, old_format = current.parse_filename(args.filename)
3546 3546 new_format = args.format
3547 if new_format == u'xml' or new_format == u'ipynb':
3547 if new_format == u'xml':
3548 raise ValueError('Notebooks cannot be written as xml.')
3549 elif new_format == u'ipynb' or new_format == u'json':
3548 3550 new_fname = old_name + u'.ipynb'
3549 new_format = u'xml'
3551 new_format = u'json'
3550 3552 elif new_format == u'py':
3551 3553 new_fname = old_name + u'.py'
3552 elif new_format == u'json':
3553 new_fname = old_name + u'.json'
3554 3554 else:
3555 raise ValueError('Invalid notebook format: %s' % newformat)
3555 raise ValueError('Invalid notebook format: %s' % new_format)
3556 3556 with open(old_fname, 'r') as f:
3557 nb = current.read(f, old_format)
3557 s = f.read()
3558 try:
3559 nb = current.reads(s, old_format)
3560 except:
3561 nb = current.reads(s, u'xml')
3558 3562 with open(new_fname, 'w') as f:
3559 3563 current.write(nb, f, new_format)
3560 3564
@@ -282,9 +282,6 b' class NotebookHandler(web.RequestHandler):'
282 282 if format == u'json':
283 283 self.set_header('Content-Type', 'application/json')
284 284 self.set_header('Content-Disposition','attachment; filename="%s.json"' % name)
285 elif format == u'xml':
286 self.set_header('Content-Type', 'application/xml')
287 self.set_header('Content-Disposition','attachment; filename="%s.ipynb"' % name)
288 285 elif format == u'py':
289 286 self.set_header('Content-Type', 'application/x-python')
290 287 self.set_header('Content-Disposition','attachment; filename="%s.py"' % name)
@@ -39,7 +39,7 b' class NotebookManager(LoggingConfigurable):'
39 39 The directory to use for notebooks.
40 40 """)
41 41 filename_ext = Unicode(u'.ipynb')
42 allowed_formats = List([u'json',u'xml',u'py'])
42 allowed_formats = List([u'json',u'py'])
43 43
44 44 # Map notebook_ids to notebook names
45 45 mapping = Dict()
@@ -120,19 +120,15 b' class NotebookManager(LoggingConfigurable):'
120 120 raise web.HTTPError(404)
121 121 info = os.stat(path)
122 122 last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime)
123 try:
124 123 with open(path,'r') as f:
125 124 s = f.read()
126 125 try:
127 # v2 and later have xml in the .ipynb files.
128 nb = current.reads(s, 'xml')
129 except:
130 # v1 had json in the .ipynb files.
131 nb = current.reads(s, 'json')
132 # v1 notebooks don't have a name field, so use the filename.
133 nb.name = os.path.split(path)[-1].split(u'.')[0]
126 # v1 and v2 and json in the .ipynb files.
127 nb = current.reads(s, u'json')
134 128 except:
135 129 raise web.HTTPError(404)
130 if 'name' not in nb:
131 nb.name = os.path.split(path)[-1].split(u'.')[0]
136 132 return last_modified, nb
137 133
138 134 def save_new_notebook(self, data, name=None, format=u'json'):
@@ -147,13 +143,6 b' class NotebookManager(LoggingConfigurable):'
147 143 try:
148 144 nb = current.reads(data, format)
149 145 except:
150 if format == u'xml':
151 # v1 notebooks might come in with a format='xml' but be json.
152 try:
153 nb = current.reads(data, u'json')
154 except:
155 raise web.HTTPError(400)
156 else:
157 146 raise web.HTTPError(400)
158 147
159 148 if name is None:
@@ -175,13 +164,6 b' class NotebookManager(LoggingConfigurable):'
175 164 try:
176 165 nb = current.reads(data, format)
177 166 except:
178 if format == u'xml':
179 # v1 notebooks might come in with a format='xml' but be json.
180 try:
181 nb = current.reads(data, u'json')
182 except:
183 raise web.HTTPError(400)
184 else:
185 167 raise web.HTTPError(400)
186 168
187 169 if name is not None:
@@ -200,7 +182,7 b' class NotebookManager(LoggingConfigurable):'
200 182 path = self.get_path_by_name(new_name)
201 183 try:
202 184 with open(path,'w') as f:
203 current.write(nb, f, u'xml')
185 current.write(nb, f, u'json')
204 186 except:
205 187 raise web.HTTPError(400)
206 188 if old_name != new_name:
@@ -231,6 +213,6 b' class NotebookManager(LoggingConfigurable):'
231 213 notebook_id = self.new_notebook_id(name)
232 214 nb = current.new_notebook(name=name)
233 215 with open(path,'w') as f:
234 current.write(nb, f, u'xml')
216 current.write(nb, f, u'json')
235 217 return notebook_id
236 218
@@ -39,8 +39,8 b' var IPython = (function (IPython) {'
39 39 var fname = f.name.split('.');
40 40 var nbname = fname[0];
41 41 var nbformat = fname[1];
42 if (nbformat === 'ipynb') {nbformat = 'xml';};
43 if (nbformat === 'xml' || nbformat === 'py' || nbformat === 'json') {
42 if (nbformat === 'ipynb') {nbformat = 'json';};
43 if (nbformat === 'py' || nbformat === 'json') {
44 44 var item = that.new_notebook_item(0);
45 45 that.add_name_input(nbname, item);
46 46 item.data('nbformat', nbformat);
@@ -198,9 +198,7 b' var IPython = (function (IPython) {'
198 198 var nbformat = item.data('nbformat');
199 199 var nbdata = item.data('nbdata');
200 200 var content_type = 'text/plain';
201 if (nbformat === 'xml') {
202 content_type = 'application/xml';
203 } else if (nbformat === 'json') {
201 if (nbformat === 'json') {
204 202 content_type = 'application/json';
205 203 } else if (nbformat === 'py') {
206 204 content_type = 'application/x-python';
@@ -62,7 +62,6 b''
62 62 <div class="section_row">
63 63 <span>
64 64 <select id="download_format">
65 <option value="xml">xml</option>
66 65 <option value="json">json</option>
67 66 <option value="py">py</option>
68 67 </select>
@@ -72,7 +71,7 b''
72 71 <button id="print_notebook">Print</button>
73 72 </span>
74 73
75 <button id="download_notebook">Export</button>
74 <button id="download_notebook">Download</button>
76 75 </span>
77 76 </div>
78 77 </div>
@@ -127,7 +127,7 b' def reads(s, format, **kwargs):'
127 127 ----------
128 128 s : str
129 129 The raw string to read the notebook from.
130 format : ('xml','json','py')
130 format : ('json','py')
131 131 The format that the string is in.
132 132
133 133 Returns
@@ -154,7 +154,7 b' def writes(nb, format, **kwargs):'
154 154 ----------
155 155 nb : NotebookNode
156 156 The notebook to write.
157 format : ('xml','json','py')
157 format : ('json','py')
158 158 The format to write the notebook in.
159 159
160 160 Returns
@@ -182,7 +182,7 b' def read(fp, format, **kwargs):'
182 182 ----------
183 183 fp : file
184 184 Any file-like object with a read method.
185 format : ('xml','json','py')
185 format : ('json','py')
186 186 The format that the string is in.
187 187
188 188 Returns
@@ -204,7 +204,7 b' def write(nb, fp, format, **kwargs):'
204 204 The notebook to write.
205 205 fp : file
206 206 Any file-like object with a write method.
207 format : ('xml','json','py')
207 format : ('json','py')
208 208 The format to write the notebook in.
209 209
210 210 Returns
@@ -43,13 +43,13 b' def parse_filename(fname):'
43 43 """Parse a notebook filename.
44 44
45 45 This function takes a notebook filename and returns the notebook
46 format (xml/json/py) and the notebook name. This logic can be
46 format (json/py) and the notebook name. This logic can be
47 47 summarized as follows:
48 48
49 * notebook.ipynb -> (notebook.ipynb, notebook, xml)
49 * notebook.ipynb -> (notebook.ipynb, notebook, json)
50 50 * notebook.json -> (notebook.json, notebook, json)
51 51 * notebook.py -> (notebook.py, notebook, py)
52 * notebook -> (notebook.ipynb, notebook, xml)
52 * notebook -> (notebook.ipynb, notebook, json)
53 53
54 54 Parameters
55 55 ----------
@@ -64,14 +64,14 b' def parse_filename(fname):'
64 64 The filename, notebook name and format.
65 65 """
66 66 if fname.endswith(u'.ipynb'):
67 format = u'xml'
67 format = u'json'
68 68 elif fname.endswith(u'.json'):
69 69 format = u'json'
70 70 elif fname.endswith(u'.py'):
71 71 format = u'py'
72 72 else:
73 73 fname = fname + u'.ipynb'
74 format = u'xml'
74 format = u'json'
75 75 name = fname.split('.')[0]
76 76 return fname, name, format
77 77
@@ -17,6 +17,7 b' Authors:'
17 17 #-----------------------------------------------------------------------------
18 18
19 19 from base64 import encodestring, decodestring
20 import warnings
20 21 from xml.etree import ElementTree as ET
21 22
22 23 from .rwbase import NotebookReader, NotebookWriter
@@ -110,6 +111,8 b' class XMLReader(NotebookReader):'
110 111 return self.to_notebook(root, **kwargs)
111 112
112 113 def to_notebook(self, root, **kwargs):
114 warnings.warn('The XML notebook format is no longer supported, '
115 'please convert your notebooks to JSON.', DeprecationWarning)
113 116 nbname = _get_text(root,u'name')
114 117 nbauthor = _get_text(root,u'author')
115 118 nbemail = _get_text(root,u'email')
@@ -179,6 +182,8 b' class XMLReader(NotebookReader):'
179 182 class XMLWriter(NotebookWriter):
180 183
181 184 def writes(self, nb, **kwargs):
185 warnings.warn('The XML notebook format is no longer supported, '
186 'please convert your notebooks to JSON.', DeprecationWarning)
182 187 nb_e = ET.Element(u'notebook')
183 188 _set_text(nb,u'name',nb_e,u'name')
184 189 _set_text(nb,u'author',nb_e,u'author')
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now