##// END OF EJS Templates
add nbformat_minor for minor revisions to nbformat
MinRK -
Show More
@@ -1,215 +1,217 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 v3
24 from IPython.nbformat import v3
25 from IPython.nbformat import v2
25 from IPython.nbformat import v2
26 from IPython.nbformat import v1
26 from IPython.nbformat import v1
27
27
28 from IPython.nbformat.v3 import (
28 from IPython.nbformat.v3 import (
29 NotebookNode,
29 NotebookNode,
30 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
30 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
31 parse_filename, new_metadata, new_author, new_heading_cell, nbformat
31 parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
32 nbformat_minor,
32 )
33 )
33
34
34 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
35 # Code
36 # Code
36 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
37
38
38 current_nbformat = nbformat
39 current_nbformat = nbformat
40 current_nbformat_minor = nbformat_minor
41
39
42
40
43
41 class NBFormatError(Exception):
44 class NBFormatError(Exception):
42 pass
45 pass
43
46
44
47
45 def parse_json(s, **kwargs):
48 def parse_json(s, **kwargs):
46 """Parse a string into a (nbformat, dict) tuple."""
49 """Parse a string into a (nbformat, dict) tuple."""
47 d = json.loads(s, **kwargs)
50 d = json.loads(s, **kwargs)
48 nbf = d.get('nbformat',1)
51 nbf = d.get('nbformat', 1)
49 return nbf, d
52 nbm = d.get('nbformat_minor', 0)
53 return nbf, nbm, d
50
54
51
55
52 def parse_py(s, **kwargs):
56 def parse_py(s, **kwargs):
53 """Parse a string into a (nbformat, string) tuple."""
57 """Parse a string into a (nbformat, string) tuple."""
54 pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>'
58 pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>'
55 m = re.search(pattern,s)
59 m = re.search(pattern,s)
56 if m is not None:
60 if m is not None:
57 nbf = int(m.group('nbformat'))
61 nbf = int(m.group('nbformat'))
58 else:
62 else:
59 nbf = current_nbformat
63 nbf = current_nbformat
60 return nbf, s
64 return nbf, s
61
65
62
66
63 def reads_json(s, **kwargs):
67 def reads_json(s, **kwargs):
64 """Read a JSON notebook from a string and return the NotebookNode object."""
68 """Read a JSON notebook from a string and return the NotebookNode object."""
65 nbf, d = parse_json(s, **kwargs)
69 nbf, minor, d = parse_json(s, **kwargs)
66 # cast int for reading - 3.x notebooks should be readable on 3.0, etc.
67 nbf = int(nbf)
68 if nbf == 1:
70 if nbf == 1:
69 nb = v1.to_notebook_json(d, **kwargs)
71 nb = v1.to_notebook_json(d, **kwargs)
70 nb = v3.convert_to_this_nbformat(nb, orig_version=1)
72 nb = v3.convert_to_this_nbformat(nb, orig_version=1)
71 elif nbf == 2:
73 elif nbf == 2:
72 nb = v2.to_notebook_json(d, **kwargs)
74 nb = v2.to_notebook_json(d, **kwargs)
73 nb = v3.convert_to_this_nbformat(nb, orig_version=2)
75 nb = v3.convert_to_this_nbformat(nb, orig_version=2)
74 elif nbf == 3:
76 elif nbf == 3:
75 nb = v3.to_notebook_json(d, **kwargs)
77 nb = v3.to_notebook_json(d, **kwargs)
78 nb = v3.convert_to_this_nbformat(nb, orig_version=3, orig_minor=minor)
76 else:
79 else:
77 raise NBFormatError('Unsupported JSON nbformat version: %i' % nbf)
80 raise NBFormatError('Unsupported JSON nbformat version: %i' % nbf)
78 return nb
81 return nb
79
82
80
83
81 def writes_json(nb, **kwargs):
84 def writes_json(nb, **kwargs):
82 return v3.writes_json(nb, **kwargs)
85 return v3.writes_json(nb, **kwargs)
83
86
84
87
85 def reads_py(s, **kwargs):
88 def reads_py(s, **kwargs):
86 """Read a .py notebook from a string and return the NotebookNode object."""
89 """Read a .py notebook from a string and return the NotebookNode object."""
87 nbf, s = parse_py(s, **kwargs)
90 nbf, s = parse_py(s, **kwargs)
88 # cast int for reading - 3.x notebooks should be readable on 3.0, etc.
91 nbf = nbf
89 nbf = int(nbf)
90 if nbf == 2:
92 if nbf == 2:
91 nb = v2.to_notebook_py(s, **kwargs)
93 nb = v2.to_notebook_py(s, **kwargs)
92 elif nbf == 3:
94 elif nbf == 3:
93 nb = v3.to_notebook_py(s, **kwargs)
95 nb = v3.to_notebook_py(s, **kwargs)
94 else:
96 else:
95 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
97 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
96 return nb
98 return nb
97
99
98
100
99 def writes_py(nb, **kwargs):
101 def writes_py(nb, **kwargs):
100 return v3.writes_py(nb, **kwargs)
102 return v3.writes_py(nb, **kwargs)
101
103
102
104
103 # High level API
105 # High level API
104
106
105
107
106 def reads(s, format, **kwargs):
108 def reads(s, format, **kwargs):
107 """Read a notebook from a string and return the NotebookNode object.
109 """Read a notebook from a string and return the NotebookNode object.
108
110
109 This function properly handles notebooks of any version. The notebook
111 This function properly handles notebooks of any version. The notebook
110 returned will always be in the current version's format.
112 returned will always be in the current version's format.
111
113
112 Parameters
114 Parameters
113 ----------
115 ----------
114 s : unicode
116 s : unicode
115 The raw unicode string to read the notebook from.
117 The raw unicode string to read the notebook from.
116 format : (u'json', u'ipynb', u'py')
118 format : (u'json', u'ipynb', u'py')
117 The format that the string is in.
119 The format that the string is in.
118
120
119 Returns
121 Returns
120 -------
122 -------
121 nb : NotebookNode
123 nb : NotebookNode
122 The notebook that was read.
124 The notebook that was read.
123 """
125 """
124 format = unicode(format)
126 format = unicode(format)
125 if format == u'json' or format == u'ipynb':
127 if format == u'json' or format == u'ipynb':
126 return reads_json(s, **kwargs)
128 return reads_json(s, **kwargs)
127 elif format == u'py':
129 elif format == u'py':
128 return reads_py(s, **kwargs)
130 return reads_py(s, **kwargs)
129 else:
131 else:
130 raise NBFormatError('Unsupported format: %s' % format)
132 raise NBFormatError('Unsupported format: %s' % format)
131
133
132
134
133 def writes(nb, format, **kwargs):
135 def writes(nb, format, **kwargs):
134 """Write a notebook to a string in a given format in the current nbformat version.
136 """Write a notebook to a string in a given format in the current nbformat version.
135
137
136 This function always writes the notebook in the current nbformat version.
138 This function always writes the notebook in the current nbformat version.
137
139
138 Parameters
140 Parameters
139 ----------
141 ----------
140 nb : NotebookNode
142 nb : NotebookNode
141 The notebook to write.
143 The notebook to write.
142 format : (u'json', u'ipynb', u'py')
144 format : (u'json', u'ipynb', u'py')
143 The format to write the notebook in.
145 The format to write the notebook in.
144
146
145 Returns
147 Returns
146 -------
148 -------
147 s : unicode
149 s : unicode
148 The notebook string.
150 The notebook string.
149 """
151 """
150 format = unicode(format)
152 format = unicode(format)
151 if format == u'json' or format == u'ipynb':
153 if format == u'json' or format == u'ipynb':
152 return writes_json(nb, **kwargs)
154 return writes_json(nb, **kwargs)
153 elif format == u'py':
155 elif format == u'py':
154 return writes_py(nb, **kwargs)
156 return writes_py(nb, **kwargs)
155 else:
157 else:
156 raise NBFormatError('Unsupported format: %s' % format)
158 raise NBFormatError('Unsupported format: %s' % format)
157
159
158
160
159 def read(fp, format, **kwargs):
161 def read(fp, format, **kwargs):
160 """Read a notebook from a file and return the NotebookNode object.
162 """Read a notebook from a file and return the NotebookNode object.
161
163
162 This function properly handles notebooks of any version. The notebook
164 This function properly handles notebooks of any version. The notebook
163 returned will always be in the current version's format.
165 returned will always be in the current version's format.
164
166
165 Parameters
167 Parameters
166 ----------
168 ----------
167 fp : file
169 fp : file
168 Any file-like object with a read method.
170 Any file-like object with a read method.
169 format : (u'json', u'ipynb', u'py')
171 format : (u'json', u'ipynb', u'py')
170 The format that the string is in.
172 The format that the string is in.
171
173
172 Returns
174 Returns
173 -------
175 -------
174 nb : NotebookNode
176 nb : NotebookNode
175 The notebook that was read.
177 The notebook that was read.
176 """
178 """
177 return reads(fp.read(), format, **kwargs)
179 return reads(fp.read(), format, **kwargs)
178
180
179
181
180 def write(nb, fp, format, **kwargs):
182 def write(nb, fp, format, **kwargs):
181 """Write a notebook to a file in a given format in the current nbformat version.
183 """Write a notebook to a file in a given format in the current nbformat version.
182
184
183 This function always writes the notebook in the current nbformat version.
185 This function always writes the notebook in the current nbformat version.
184
186
185 Parameters
187 Parameters
186 ----------
188 ----------
187 nb : NotebookNode
189 nb : NotebookNode
188 The notebook to write.
190 The notebook to write.
189 fp : file
191 fp : file
190 Any file-like object with a write method.
192 Any file-like object with a write method.
191 format : (u'json', u'ipynb', u'py')
193 format : (u'json', u'ipynb', u'py')
192 The format to write the notebook in.
194 The format to write the notebook in.
193
195
194 Returns
196 Returns
195 -------
197 -------
196 s : unicode
198 s : unicode
197 The notebook string.
199 The notebook string.
198 """
200 """
199 return fp.write(writes(nb, format, **kwargs))
201 return fp.write(writes(nb, format, **kwargs))
200
202
201 def _convert_to_metadata():
203 def _convert_to_metadata():
202 """Convert to a notebook having notebook metadata."""
204 """Convert to a notebook having notebook metadata."""
203 import glob
205 import glob
204 for fname in glob.glob('*.ipynb'):
206 for fname in glob.glob('*.ipynb'):
205 print('Converting file:',fname)
207 print('Converting file:',fname)
206 with open(fname,'r') as f:
208 with open(fname,'r') as f:
207 nb = read(f,u'json')
209 nb = read(f,u'json')
208 md = new_metadata()
210 md = new_metadata()
209 if u'name' in nb:
211 if u'name' in nb:
210 md.name = nb.name
212 md.name = nb.name
211 del nb[u'name']
213 del nb[u'name']
212 nb.metadata = md
214 nb.metadata = md
213 with open(fname,'w') as f:
215 with open(fname,'w') as f:
214 write(nb, f, u'json')
216 write(nb, f, u'json')
215
217
@@ -1,74 +1,74 b''
1 """The main API for the v3 notebook format.
1 """The main API for the v3 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, new_heading_cell, nbformat
22 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor
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 .nbpy import reads as reads_py, writes as writes_py
29 from .nbpy import reads as reads_py, writes as writes_py
30 from .nbpy import reads as read_py, writes as write_py
30 from .nbpy import reads as read_py, writes as write_py
31 from .nbpy import to_notebook as to_notebook_py
31 from .nbpy import to_notebook as to_notebook_py
32
32
33 from .convert import convert_to_this_nbformat
33 from .convert import convert_to_this_nbformat
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Code
36 # Code
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38
38
39 def parse_filename(fname):
39 def parse_filename(fname):
40 """Parse a notebook filename.
40 """Parse a notebook filename.
41
41
42 This function takes a notebook filename and returns the notebook
42 This function takes a notebook filename and returns the notebook
43 format (json/py) and the notebook name. This logic can be
43 format (json/py) and the notebook name. This logic can be
44 summarized as follows:
44 summarized as follows:
45
45
46 * notebook.ipynb -> (notebook.ipynb, notebook, json)
46 * notebook.ipynb -> (notebook.ipynb, notebook, json)
47 * notebook.json -> (notebook.json, notebook, json)
47 * notebook.json -> (notebook.json, notebook, json)
48 * notebook.py -> (notebook.py, notebook, py)
48 * notebook.py -> (notebook.py, notebook, py)
49 * notebook -> (notebook.ipynb, notebook, json)
49 * notebook -> (notebook.ipynb, notebook, json)
50
50
51 Parameters
51 Parameters
52 ----------
52 ----------
53 fname : unicode
53 fname : unicode
54 The notebook filename. The filename can use a specific filename
54 The notebook filename. The filename can use a specific filename
55 extention (.ipynb, .json, .py) or none, in which case .ipynb will
55 extention (.ipynb, .json, .py) or none, in which case .ipynb will
56 be assumed.
56 be assumed.
57
57
58 Returns
58 Returns
59 -------
59 -------
60 (fname, name, format) : (unicode, unicode, unicode)
60 (fname, name, format) : (unicode, unicode, unicode)
61 The filename, notebook name and format.
61 The filename, notebook name and format.
62 """
62 """
63 if fname.endswith(u'.ipynb'):
63 if fname.endswith(u'.ipynb'):
64 format = u'json'
64 format = u'json'
65 elif fname.endswith(u'.json'):
65 elif fname.endswith(u'.json'):
66 format = u'json'
66 format = u'json'
67 elif fname.endswith(u'.py'):
67 elif fname.endswith(u'.py'):
68 format = u'py'
68 format = u'py'
69 else:
69 else:
70 fname = fname + u'.ipynb'
70 fname = fname + u'.ipynb'
71 format = u'json'
71 format = u'json'
72 name = fname.split('.')[0]
72 name = fname.split('.')[0]
73 return fname, name, format
73 return fname, name, format
74
74
@@ -1,51 +1,59 b''
1 """Code for converting notebooks to and from the v2 format.
1 """Code for converting notebooks to and from the v2 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 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
20 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
21 nbformat, nbformat_minor
21 )
22 )
22
23
23 from IPython.nbformat import v2
24 from IPython.nbformat import v2
24
25
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26 # Code
27 # Code
27 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
28
29
29 def convert_to_this_nbformat(nb, orig_version=2):
30 def convert_to_this_nbformat(nb, orig_version=2, orig_minor=0):
30 """Convert a notebook to the v3 format.
31 """Convert a notebook to the v3 format.
31
32
32 Parameters
33 Parameters
33 ----------
34 ----------
34 nb : NotebookNode
35 nb : NotebookNode
35 The Python representation of the notebook to convert.
36 The Python representation of the notebook to convert.
36 orig_version : int
37 orig_version : int
37 The original version of the notebook to convert.
38 The original version of the notebook to convert.
39 orig_minor : int
40 The original minor version of the notebook to convert (only relevant for v >= 3).
38 """
41 """
39 if orig_version == 1:
42 if orig_version == 1:
40 nb = v2.convert_to_this_nbformat(nb)
43 nb = v2.convert_to_this_nbformat(nb)
41 orig_version = 2
44 orig_version = 2
42 if orig_version == 2:
45 if orig_version == 2:
43 # Mark the original nbformat so consumers know it has been converted.
46 # Mark the original nbformat so consumers know it has been converted.
44 nb.nbformat = 3
47 nb.nbformat = nbformat
48 nb.nbformat_minor = nbformat_minor
49
45 nb.orig_nbformat = 2
50 nb.orig_nbformat = 2
46 return nb
51 return nb
47 elif orig_version == 3:
52 elif orig_version == 3:
53 if orig_minor != nbformat_minor:
54 nb.orig_nbformat_minor = orig_minor
55 nb.nbformat_minor = nbformat_minor
48 return nb
56 return nb
49 else:
57 else:
50 raise ValueError('Cannot convert a notebook from v%s to v3' % orig_version)
58 raise ValueError('Cannot convert a notebook from v%s to v3' % orig_version)
51
59
@@ -1,204 +1,206 b''
1 """The basic dict based notebook format.
1 """The basic dict based notebook format.
2
2
3 The Python representation of a notebook is a nested structure of
3 The Python representation of a notebook is a nested structure of
4 dictionary subclasses that support attribute access
4 dictionary subclasses that support attribute access
5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
6 helpers to build the structs in the right form.
6 helpers to build the structs in the right form.
7
7
8 Authors:
8 Authors:
9
9
10 * Brian Granger
10 * Brian Granger
11 """
11 """
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Copyright (C) 2008-2011 The IPython Development Team
14 # Copyright (C) 2008-2011 The IPython Development Team
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Imports
21 # Imports
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 import pprint
24 import pprint
25 import uuid
25 import uuid
26
26
27 from IPython.utils.ipstruct import Struct
27 from IPython.utils.ipstruct import Struct
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Code
30 # Code
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 # Change this when incrementing the nbformat version
33 # Change this when incrementing the nbformat version
34 nbformat = 3.0
34 nbformat = 3
35 nbformat_minor = 0
35
36
36 class NotebookNode(Struct):
37 class NotebookNode(Struct):
37 pass
38 pass
38
39
39
40
40 def from_dict(d):
41 def from_dict(d):
41 if isinstance(d, dict):
42 if isinstance(d, dict):
42 newd = NotebookNode()
43 newd = NotebookNode()
43 for k,v in d.items():
44 for k,v in d.items():
44 newd[k] = from_dict(v)
45 newd[k] = from_dict(v)
45 return newd
46 return newd
46 elif isinstance(d, (tuple, list)):
47 elif isinstance(d, (tuple, list)):
47 return [from_dict(i) for i in d]
48 return [from_dict(i) for i in d]
48 else:
49 else:
49 return d
50 return d
50
51
51
52
52 def new_output(output_type=None, output_text=None, output_png=None,
53 def new_output(output_type=None, output_text=None, output_png=None,
53 output_html=None, output_svg=None, output_latex=None, output_json=None,
54 output_html=None, output_svg=None, output_latex=None, output_json=None,
54 output_javascript=None, output_jpeg=None, prompt_number=None,
55 output_javascript=None, output_jpeg=None, prompt_number=None,
55 etype=None, evalue=None, traceback=None):
56 etype=None, evalue=None, traceback=None):
56 """Create a new code cell with input and output"""
57 """Create a new code cell with input and output"""
57 output = NotebookNode()
58 output = NotebookNode()
58 if output_type is not None:
59 if output_type is not None:
59 output.output_type = unicode(output_type)
60 output.output_type = unicode(output_type)
60
61
61 if output_type != 'pyerr':
62 if output_type != 'pyerr':
62 if output_text is not None:
63 if output_text is not None:
63 output.text = unicode(output_text)
64 output.text = unicode(output_text)
64 if output_png is not None:
65 if output_png is not None:
65 output.png = bytes(output_png)
66 output.png = bytes(output_png)
66 if output_jpeg is not None:
67 if output_jpeg is not None:
67 output.jpeg = bytes(output_jpeg)
68 output.jpeg = bytes(output_jpeg)
68 if output_html is not None:
69 if output_html is not None:
69 output.html = unicode(output_html)
70 output.html = unicode(output_html)
70 if output_svg is not None:
71 if output_svg is not None:
71 output.svg = unicode(output_svg)
72 output.svg = unicode(output_svg)
72 if output_latex is not None:
73 if output_latex is not None:
73 output.latex = unicode(output_latex)
74 output.latex = unicode(output_latex)
74 if output_json is not None:
75 if output_json is not None:
75 output.json = unicode(output_json)
76 output.json = unicode(output_json)
76 if output_javascript is not None:
77 if output_javascript is not None:
77 output.javascript = unicode(output_javascript)
78 output.javascript = unicode(output_javascript)
78
79
79 if output_type == u'pyout':
80 if output_type == u'pyout':
80 if prompt_number is not None:
81 if prompt_number is not None:
81 output.prompt_number = int(prompt_number)
82 output.prompt_number = int(prompt_number)
82
83
83 if output_type == u'pyerr':
84 if output_type == u'pyerr':
84 if etype is not None:
85 if etype is not None:
85 output.etype = unicode(etype)
86 output.etype = unicode(etype)
86 if evalue is not None:
87 if evalue is not None:
87 output.evalue = unicode(evalue)
88 output.evalue = unicode(evalue)
88 if traceback is not None:
89 if traceback is not None:
89 output.traceback = [unicode(frame) for frame in list(traceback)]
90 output.traceback = [unicode(frame) for frame in list(traceback)]
90
91
91 return output
92 return output
92
93
93
94
94 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 language=u'python', collapsed=False, metadata=None):
96 language=u'python', collapsed=False, metadata=None):
96 """Create a new code cell with input and output"""
97 """Create a new code cell with input and output"""
97 cell = NotebookNode()
98 cell = NotebookNode()
98 cell.cell_type = u'code'
99 cell.cell_type = u'code'
99 if language is not None:
100 if language is not None:
100 cell.language = unicode(language)
101 cell.language = unicode(language)
101 if input is not None:
102 if input is not None:
102 cell.input = unicode(input)
103 cell.input = unicode(input)
103 if prompt_number is not None:
104 if prompt_number is not None:
104 cell.prompt_number = int(prompt_number)
105 cell.prompt_number = int(prompt_number)
105 if outputs is None:
106 if outputs is None:
106 cell.outputs = []
107 cell.outputs = []
107 else:
108 else:
108 cell.outputs = outputs
109 cell.outputs = outputs
109 if collapsed is not None:
110 if collapsed is not None:
110 cell.collapsed = bool(collapsed)
111 cell.collapsed = bool(collapsed)
111 cell.metadata = NotebookNode(metadata or {})
112 cell.metadata = NotebookNode(metadata or {})
112
113
113 return cell
114 return cell
114
115
115 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
116 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
116 """Create a new text cell."""
117 """Create a new text cell."""
117 cell = NotebookNode()
118 cell = NotebookNode()
118 # VERSIONHACK: plaintext -> raw
119 # VERSIONHACK: plaintext -> raw
119 # handle never-released plaintext name for raw cells
120 # handle never-released plaintext name for raw cells
120 if cell_type == 'plaintext':
121 if cell_type == 'plaintext':
121 cell_type = 'raw'
122 cell_type = 'raw'
122 if source is not None:
123 if source is not None:
123 cell.source = unicode(source)
124 cell.source = unicode(source)
124 if rendered is not None:
125 if rendered is not None:
125 cell.rendered = unicode(rendered)
126 cell.rendered = unicode(rendered)
126 cell.metadata = NotebookNode(metadata or {})
127 cell.metadata = NotebookNode(metadata or {})
127 cell.cell_type = cell_type
128 cell.cell_type = cell_type
128 return cell
129 return cell
129
130
130
131
131 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
132 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
132 """Create a new section cell with a given integer level."""
133 """Create a new section cell with a given integer level."""
133 cell = NotebookNode()
134 cell = NotebookNode()
134 cell.cell_type = u'heading'
135 cell.cell_type = u'heading'
135 if source is not None:
136 if source is not None:
136 cell.source = unicode(source)
137 cell.source = unicode(source)
137 if rendered is not None:
138 if rendered is not None:
138 cell.rendered = unicode(rendered)
139 cell.rendered = unicode(rendered)
139 cell.level = int(level)
140 cell.level = int(level)
140 cell.metadata = NotebookNode(metadata or {})
141 cell.metadata = NotebookNode(metadata or {})
141 return cell
142 return cell
142
143
143
144
144 def new_worksheet(name=None, cells=None, metadata=None):
145 def new_worksheet(name=None, cells=None, metadata=None):
145 """Create a worksheet by name with with a list of cells."""
146 """Create a worksheet by name with with a list of cells."""
146 ws = NotebookNode()
147 ws = NotebookNode()
147 if name is not None:
148 if name is not None:
148 ws.name = unicode(name)
149 ws.name = unicode(name)
149 if cells is None:
150 if cells is None:
150 ws.cells = []
151 ws.cells = []
151 else:
152 else:
152 ws.cells = list(cells)
153 ws.cells = list(cells)
153 ws.metadata = NotebookNode(metadata or {})
154 ws.metadata = NotebookNode(metadata or {})
154 return ws
155 return ws
155
156
156
157
157 def new_notebook(name=None, metadata=None, worksheets=None):
158 def new_notebook(name=None, metadata=None, worksheets=None):
158 """Create a notebook by name, id and a list of worksheets."""
159 """Create a notebook by name, id and a list of worksheets."""
159 nb = NotebookNode()
160 nb = NotebookNode()
160 nb.nbformat = nbformat
161 nb.nbformat = nbformat
162 nb.nbformat_minor = nbformat_minor
161 if worksheets is None:
163 if worksheets is None:
162 nb.worksheets = []
164 nb.worksheets = []
163 else:
165 else:
164 nb.worksheets = list(worksheets)
166 nb.worksheets = list(worksheets)
165 if metadata is None:
167 if metadata is None:
166 nb.metadata = new_metadata()
168 nb.metadata = new_metadata()
167 else:
169 else:
168 nb.metadata = NotebookNode(metadata)
170 nb.metadata = NotebookNode(metadata)
169 if name is not None:
171 if name is not None:
170 nb.metadata.name = unicode(name)
172 nb.metadata.name = unicode(name)
171 return nb
173 return nb
172
174
173
175
174 def new_metadata(name=None, authors=None, license=None, created=None,
176 def new_metadata(name=None, authors=None, license=None, created=None,
175 modified=None, gistid=None):
177 modified=None, gistid=None):
176 """Create a new metadata node."""
178 """Create a new metadata node."""
177 metadata = NotebookNode()
179 metadata = NotebookNode()
178 if name is not None:
180 if name is not None:
179 metadata.name = unicode(name)
181 metadata.name = unicode(name)
180 if authors is not None:
182 if authors is not None:
181 metadata.authors = list(authors)
183 metadata.authors = list(authors)
182 if created is not None:
184 if created is not None:
183 metadata.created = unicode(created)
185 metadata.created = unicode(created)
184 if modified is not None:
186 if modified is not None:
185 metadata.modified = unicode(modified)
187 metadata.modified = unicode(modified)
186 if license is not None:
188 if license is not None:
187 metadata.license = unicode(license)
189 metadata.license = unicode(license)
188 if gistid is not None:
190 if gistid is not None:
189 metadata.gistid = unicode(gistid)
191 metadata.gistid = unicode(gistid)
190 return metadata
192 return metadata
191
193
192 def new_author(name=None, email=None, affiliation=None, url=None):
194 def new_author(name=None, email=None, affiliation=None, url=None):
193 """Create a new author."""
195 """Create a new author."""
194 author = NotebookNode()
196 author = NotebookNode()
195 if name is not None:
197 if name is not None:
196 author.name = unicode(name)
198 author.name = unicode(name)
197 if email is not None:
199 if email is not None:
198 author.email = unicode(email)
200 author.email = unicode(email)
199 if affiliation is not None:
201 if affiliation is not None:
200 author.affiliation = unicode(affiliation)
202 author.affiliation = unicode(affiliation)
201 if url is not None:
203 if url is not None:
202 author.url = unicode(url)
204 author.url = unicode(url)
203 return author
205 return author
204
206
General Comments 0
You need to be logged in to leave comments. Login now