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