Show More
@@ -1,146 +1,150 b'' | |||
|
1 | 1 | """The IPython notebook format |
|
2 | 2 | |
|
3 | 3 | Use this module to read or write notebook files as particular nbformat versions. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | # Copyright (c) IPython Development Team. |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | |
|
9 | 9 | from IPython.utils.log import get_logger |
|
10 | 10 | |
|
11 | 11 | from . import v1 |
|
12 | 12 | from . import v2 |
|
13 | 13 | from . import v3 |
|
14 | 14 | from . import v4 |
|
15 | 15 | |
|
16 | __all__ = ['versions', 'validate', 'ValidationError', 'convert', 'from_dict', | |
|
17 | 'NotebookNode', 'current_nbformat', 'current_nbformat_minor', | |
|
18 | 'NBFormatError', 'NO_CONVERT', 'reads', 'read', 'writes', 'write'] | |
|
19 | ||
|
16 | 20 | versions = { |
|
17 | 21 | 1: v1, |
|
18 | 22 | 2: v2, |
|
19 | 23 | 3: v3, |
|
20 | 24 | 4: v4, |
|
21 | 25 | } |
|
22 | 26 | |
|
23 | 27 | from .validator import validate, ValidationError |
|
24 | 28 | from .converter import convert |
|
25 | 29 | from . import reader |
|
26 | 30 | from .notebooknode import from_dict, NotebookNode |
|
27 | 31 | |
|
28 | 32 | from .v4 import ( |
|
29 | 33 | nbformat as current_nbformat, |
|
30 | 34 | nbformat_minor as current_nbformat_minor, |
|
31 | 35 | ) |
|
32 | 36 | |
|
33 | 37 | class NBFormatError(ValueError): |
|
34 | 38 | pass |
|
35 | 39 | |
|
36 | 40 | # no-conversion singleton |
|
37 | 41 | NO_CONVERT = object() |
|
38 | 42 | |
|
39 | 43 | def reads(s, as_version, **kwargs): |
|
40 | 44 | """Read a notebook from a string and return the NotebookNode object as the given version. |
|
41 | 45 | |
|
42 | 46 | The string can contain a notebook of any version. |
|
43 | 47 | The notebook will be returned `as_version`, converting, if necessary. |
|
44 | 48 | |
|
45 | 49 | Notebook format errors will be logged. |
|
46 | 50 | |
|
47 | 51 | Parameters |
|
48 | 52 | ---------- |
|
49 | 53 | s : unicode |
|
50 | 54 | The raw unicode string to read the notebook from. |
|
51 | 55 | as_version : int |
|
52 | 56 | The version of the notebook format to return. |
|
53 | 57 | The notebook will be converted, if necessary. |
|
54 | 58 | Pass nbformat.NO_CONVERT to prevent conversion. |
|
55 | 59 | |
|
56 | 60 | Returns |
|
57 | 61 | ------- |
|
58 | 62 | nb : NotebookNode |
|
59 | 63 | The notebook that was read. |
|
60 | 64 | """ |
|
61 | 65 | nb = reader.reads(s, **kwargs) |
|
62 | 66 | if as_version is not NO_CONVERT: |
|
63 | 67 | nb = convert(nb, as_version) |
|
64 | 68 | try: |
|
65 | 69 | validate(nb) |
|
66 | 70 | except ValidationError as e: |
|
67 | 71 | get_logger().error("Notebook JSON is invalid: %s", e) |
|
68 | 72 | return nb |
|
69 | 73 | |
|
70 | 74 | |
|
71 | 75 | def writes(nb, version=NO_CONVERT, **kwargs): |
|
72 | 76 | """Write a notebook to a string in a given format in the given nbformat version. |
|
73 | 77 | |
|
74 | 78 | Any notebook format errors will be logged. |
|
75 | 79 | |
|
76 | 80 | Parameters |
|
77 | 81 | ---------- |
|
78 | 82 | nb : NotebookNode |
|
79 | 83 | The notebook to write. |
|
80 | 84 | version : int, optional |
|
81 | 85 | The nbformat version to write. |
|
82 | 86 | If unspecified, or specified as nbformat.NO_CONVERT, |
|
83 | 87 | the notebook's own version will be used and no conversion performed. |
|
84 | 88 | |
|
85 | 89 | Returns |
|
86 | 90 | ------- |
|
87 | 91 | s : unicode |
|
88 | 92 | The notebook as a JSON string. |
|
89 | 93 | """ |
|
90 | 94 | if version is not NO_CONVERT: |
|
91 | 95 | nb = convert(nb, version) |
|
92 | 96 | else: |
|
93 | 97 | version, _ = reader.get_version(nb) |
|
94 | 98 | try: |
|
95 | 99 | validate(nb) |
|
96 | 100 | except ValidationError as e: |
|
97 | 101 | get_logger().error("Notebook JSON is invalid: %s", e) |
|
98 | 102 | return versions[version].writes_json(nb, **kwargs) |
|
99 | 103 | |
|
100 | 104 | |
|
101 | 105 | def read(fp, as_version, **kwargs): |
|
102 | 106 | """Read a notebook from a file as a NotebookNode of the given version. |
|
103 | 107 | |
|
104 | 108 | The string can contain a notebook of any version. |
|
105 | 109 | The notebook will be returned `as_version`, converting, if necessary. |
|
106 | 110 | |
|
107 | 111 | Notebook format errors will be logged. |
|
108 | 112 | |
|
109 | 113 | Parameters |
|
110 | 114 | ---------- |
|
111 | 115 | fp : file |
|
112 | 116 | Any file-like object with a read method. |
|
113 | 117 | as_version: int |
|
114 | 118 | The version of the notebook format to return. |
|
115 | 119 | The notebook will be converted, if necessary. |
|
116 | 120 | Pass nbformat.NO_CONVERT to prevent conversion. |
|
117 | 121 | |
|
118 | 122 | Returns |
|
119 | 123 | ------- |
|
120 | 124 | nb : NotebookNode |
|
121 | 125 | The notebook that was read. |
|
122 | 126 | """ |
|
123 | 127 | return reads(fp.read(), as_version, **kwargs) |
|
124 | 128 | |
|
125 | 129 | |
|
126 | 130 | def write(nb, fp, version=NO_CONVERT, **kwargs): |
|
127 | 131 | """Write a notebook to a file in a given nbformat version. |
|
128 | 132 | |
|
129 | 133 | The file-like object must accept unicode input. |
|
130 | 134 | |
|
131 | 135 | Parameters |
|
132 | 136 | ---------- |
|
133 | 137 | nb : NotebookNode |
|
134 | 138 | The notebook to write. |
|
135 | 139 | fp : file |
|
136 | 140 | Any file-like object with a write method that accepts unicode. |
|
137 | 141 | version : int, optional |
|
138 | 142 | The nbformat version to write. |
|
139 | 143 | If nb is not this version, it will be converted. |
|
140 | 144 | If unspecified, or specified as nbformat.NO_CONVERT, |
|
141 | 145 | the notebook's own version will be used and no conversion performed. |
|
142 | 146 | """ |
|
143 | 147 | s = writes(nb, version, **kwargs) |
|
144 | 148 | if isinstance(s, bytes): |
|
145 | 149 | s = s.decode('utf8') |
|
146 | 150 | return fp.write(s) |
@@ -1,77 +1,70 b'' | |||
|
1 | 1 | """The main API for the v3 notebook format. |
|
2 | ||
|
3 | Authors: | |
|
4 | ||
|
5 | * Brian Granger | |
|
6 | 2 | """ |
|
7 | 3 | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
10 | # | |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
|
12 | # the file COPYING, distributed as part of this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) IPython Development Team. | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
14 | 6 | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
7 | __all__ = ['NotebookNode', 'new_code_cell', 'new_text_cell', 'new_notebook', | |
|
8 | 'new_output', 'new_worksheet', 'new_metadata', 'new_author', | |
|
9 | 'new_heading_cell', 'nbformat', 'nbformat_minor', 'nbformat_schema', | |
|
10 | 'reads_json', 'writes_json', 'read_json', 'write_json', | |
|
11 | 'to_notebook_json', 'reads_py', 'writes_py', 'read_py', 'write_py', | |
|
12 | 'to_notebook_py', 'downgrade', 'upgrade', 'parse_filename' | |
|
13 | ] | |
|
18 | 14 | |
|
19 | 15 | import os |
|
20 | 16 | |
|
21 | 17 | from .nbbase import ( |
|
22 | 18 | NotebookNode, |
|
23 | 19 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
24 | 20 | new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor, |
|
25 | 21 | nbformat_schema |
|
26 | 22 | ) |
|
27 | 23 | |
|
28 | 24 | from .nbjson import reads as reads_json, writes as writes_json |
|
29 | 25 | from .nbjson import reads as read_json, writes as write_json |
|
30 | 26 | from .nbjson import to_notebook as to_notebook_json |
|
31 | 27 | |
|
32 | 28 | from .nbpy import reads as reads_py, writes as writes_py |
|
33 | 29 | from .nbpy import reads as read_py, writes as write_py |
|
34 | 30 | from .nbpy import to_notebook as to_notebook_py |
|
35 | 31 | |
|
36 | 32 | from .convert import downgrade, upgrade |
|
37 | 33 | |
|
38 | #----------------------------------------------------------------------------- | |
|
39 | # Code | |
|
40 | #----------------------------------------------------------------------------- | |
|
41 | 34 | |
|
42 | 35 | def parse_filename(fname): |
|
43 | 36 | """Parse a notebook filename. |
|
44 | 37 | |
|
45 | 38 | This function takes a notebook filename and returns the notebook |
|
46 | 39 | format (json/py) and the notebook name. This logic can be |
|
47 | 40 | summarized as follows: |
|
48 | 41 | |
|
49 | 42 | * notebook.ipynb -> (notebook.ipynb, notebook, json) |
|
50 | 43 | * notebook.json -> (notebook.json, notebook, json) |
|
51 | 44 | * notebook.py -> (notebook.py, notebook, py) |
|
52 | 45 | * notebook -> (notebook.ipynb, notebook, json) |
|
53 | 46 | |
|
54 | 47 | Parameters |
|
55 | 48 | ---------- |
|
56 | 49 | fname : unicode |
|
57 | 50 | The notebook filename. The filename can use a specific filename |
|
58 | 51 | extention (.ipynb, .json, .py) or none, in which case .ipynb will |
|
59 | 52 | be assumed. |
|
60 | 53 | |
|
61 | 54 | Returns |
|
62 | 55 | ------- |
|
63 | 56 | (fname, name, format) : (unicode, unicode, unicode) |
|
64 | 57 | The filename, notebook name and format. |
|
65 | 58 | """ |
|
66 | 59 | basename, ext = os.path.splitext(fname) |
|
67 | 60 | if ext == u'.ipynb': |
|
68 | 61 | format = u'json' |
|
69 | 62 | elif ext == u'.json': |
|
70 | 63 | format = u'json' |
|
71 | 64 | elif ext == u'.py': |
|
72 | 65 | format = u'py' |
|
73 | 66 | else: |
|
74 | 67 | basename = fname |
|
75 | 68 | fname = fname + u'.ipynb' |
|
76 | 69 | format = u'json' |
|
77 | 70 | return fname, basename, format |
@@ -1,19 +1,23 b'' | |||
|
1 | 1 | """The main API for the v4 notebook format.""" |
|
2 | 2 | |
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | __all__ = ['nbformat', 'nbformat_minor', 'nbformat_schema', 'new_code_cell', | |
|
7 | 'new_markdown_cell', 'new_notebook', 'new_output', 'output_from_msg', | |
|
8 | 'reads', 'writes', 'to_notebook', 'downgrade', 'upgrade'] | |
|
9 | ||
|
6 | 10 | from .nbbase import ( |
|
7 | 11 | nbformat, nbformat_minor, nbformat_schema, |
|
8 | 12 | new_code_cell, new_markdown_cell, new_notebook, |
|
9 | 13 | new_output, output_from_msg, |
|
10 | 14 | ) |
|
11 | 15 | |
|
12 | 16 | from .nbjson import reads, writes, to_notebook |
|
13 | 17 | reads_json = reads |
|
14 | 18 | writes_json = writes |
|
15 | 19 | to_notebook_json = to_notebook |
|
16 | 20 | |
|
17 | 21 | from .convert import downgrade, upgrade |
|
18 | 22 | |
|
19 | 23 |
@@ -1,70 +1,79 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """Script to auto-generate our API docs. |
|
3 | 3 | """ |
|
4 | 4 | # stdlib imports |
|
5 | 5 | import os |
|
6 | 6 | import sys |
|
7 | 7 | |
|
8 | 8 | # local imports |
|
9 | 9 | sys.path.append(os.path.abspath('sphinxext')) |
|
10 | 10 | from apigen import ApiDocWriter |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | if __name__ == '__main__': |
|
14 | 14 | pjoin = os.path.join |
|
15 | 15 | package = 'IPython' |
|
16 | 16 | outdir = pjoin('source','api','generated') |
|
17 | 17 | docwriter = ApiDocWriter(package,rst_extension='.rst') |
|
18 | 18 | # You have to escape the . here because . is a special char for regexps. |
|
19 | 19 | # You must do make clean if you change this! |
|
20 | 20 | docwriter.package_skip_patterns += [r'\.external$', |
|
21 | 21 | # Extensions are documented elsewhere. |
|
22 | 22 | r'\.extensions', |
|
23 | 23 | r'\.config\.profile', |
|
24 |
# |
|
|
25 |
r'\.nbformat\.v |
|
|
24 | # Old nbformat versions | |
|
25 | r'\.nbformat\.v[1-2]', | |
|
26 | 26 | # Public API for this is in kernel.zmq.eventloops |
|
27 | 27 | r'\.kernel\.zmq\.gui', |
|
28 | 28 | # Magics are documented separately |
|
29 | 29 | r'\.core\.magics', |
|
30 | 30 | ] |
|
31 | 31 | |
|
32 | 32 | # The inputhook* modules often cause problems on import, such as trying to |
|
33 | 33 | # load incompatible Qt bindings. It's easiest to leave them all out. The |
|
34 | 34 | # main API is in the inputhook module, which is documented. |
|
35 | 35 | docwriter.module_skip_patterns += [ r'\.lib\.inputhook.+', |
|
36 | 36 | r'\.ipdoctest', |
|
37 | 37 | r'\.testing\.plugin', |
|
38 | 38 | # This just prints a deprecation msg: |
|
39 | 39 | r'\.frontend$', |
|
40 | 40 | # Deprecated: |
|
41 | 41 | r'\.core\.magics\.deprecated', |
|
42 | 42 | # We document this manually. |
|
43 | 43 | r'\.utils\.py3compat', |
|
44 |
# These are exposed by nbformat |
|
|
44 | # These are exposed by nbformat | |
|
45 | 45 | r'\.nbformat\.convert', |
|
46 | 46 | r'\.nbformat\.validator', |
|
47 | r'\.nbformat\.notebooknode', | |
|
48 | # Deprecated | |
|
49 | r'\.nbformat\.current', | |
|
50 | # Exposed by nbformat.vN | |
|
51 | r'\.nbformat\.v[3-4]\.nbbase', | |
|
47 | 52 | # These are exposed in display |
|
48 | 53 | r'\.core\.display', |
|
49 | 54 | r'\.lib\.display', |
|
50 | 55 | # This isn't actually a module |
|
51 | 56 | r'\.html\.tasks', |
|
57 | # This is private | |
|
58 | r'\.html\.allow76' | |
|
52 | 59 | ] |
|
53 | 60 | |
|
54 | 61 | # These modules import functions and classes from other places to expose |
|
55 | 62 | # them as part of the public API. They must have __all__ defined. The |
|
56 | 63 | # non-API modules they import from should be excluded by the skip patterns |
|
57 | 64 | # above. |
|
58 | 65 | docwriter.names_from__all__.update({ |
|
59 |
'IPython.nbformat |
|
|
66 | 'IPython.nbformat', | |
|
67 | 'IPython.nbformat.v3', | |
|
68 | 'IPython.nbformat.v4', | |
|
60 | 69 | 'IPython.display', |
|
61 | 70 | }) |
|
62 | 71 | |
|
63 | 72 | # Now, generate the outputs |
|
64 | 73 | docwriter.write_api_docs(outdir) |
|
65 | 74 | # Write index with .txt extension - we can include it, but Sphinx won't try |
|
66 | 75 | # to compile it |
|
67 | 76 | docwriter.write_index(outdir, 'gen.txt', |
|
68 | 77 | relative_to = pjoin('source','api') |
|
69 | 78 | ) |
|
70 | 79 | print ('%d files written' % len(docwriter.written_modules)) |
General Comments 0
You need to be logged in to leave comments.
Login now