Show More
@@ -1,195 +1,195 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """NBConvert is a utility for conversion of IPYNB files. |
|
3 | 3 | |
|
4 | 4 | Commandline interface for the NBConvert conversion utility. Read the |
|
5 | 5 | readme.rst for usage information |
|
6 | 6 | """ |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | #Copyright (c) 2013, the IPython Development Team. |
|
9 | 9 | # |
|
10 | 10 | #Distributed under the terms of the Modified BSD License. |
|
11 | 11 | # |
|
12 | 12 | #The full license is in the file COPYING.txt, distributed with this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | #Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | #Stdlib imports |
|
20 | 20 | from __future__ import print_function |
|
21 | 21 | import sys |
|
22 | 22 | import io |
|
23 | 23 | import os |
|
24 | 24 | |
|
25 | 25 | #From IPython |
|
26 | 26 | #All the stuff needed for the configurable things |
|
27 | 27 | from IPython.config.application import Application |
|
28 | 28 | from IPython.utils.traitlets import (Bool) |
|
29 | 29 | |
|
30 | 30 | #Local imports |
|
31 | 31 | from nbconvert.export import export_by_name |
|
32 | 32 | from nbconvert.exporters.exporter import Exporter |
|
33 | 33 | from nbconvert.transformers import extractfigure |
|
34 | 34 | |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | #Globals and constants |
|
37 | 37 | #----------------------------------------------------------------------------- |
|
38 | 38 | |
|
39 | 39 | #'Keys in resources' user prompt. |
|
40 | 40 | KEYS_PROMPT_HEAD = "====================== Keys in Resources ==================================" |
|
41 | 41 | KEYS_PROMPT_BODY = """ |
|
42 | 42 | =========================================================================== |
|
43 | 43 | You are responsible for writting these files into the appropriate |
|
44 | 44 | directorie(s) if need be. If you do not want to see this message, enable |
|
45 | 45 | the 'write' (boolean) flag of the converter. |
|
46 | 46 | =========================================================================== |
|
47 | 47 | """ |
|
48 | 48 | |
|
49 | 49 | #----------------------------------------------------------------------------- |
|
50 | 50 | #Classes and functions |
|
51 | 51 | #----------------------------------------------------------------------------- |
|
52 | 52 | |
|
53 | 53 | class NbConvertApp(Application): |
|
54 | 54 | """Application used to convert to and from notebook file type (*.ipynb)""" |
|
55 | 55 | |
|
56 | 56 | stdout = Bool( |
|
57 | 57 | True, config=True, |
|
58 | 58 | help="""Whether to print the converted IPYNB file to stdout |
|
59 | 59 | use full do diff files without actually writing a new file""" |
|
60 | 60 | ) |
|
61 | 61 | |
|
62 | 62 | write = Bool( |
|
63 | 63 | False, config=True, |
|
64 | 64 | help="""Should the converted notebook file be written to disk |
|
65 | 65 | along with potential extracted resources.""" |
|
66 | 66 | ) |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def __init__(self, **kwargs): |
|
70 | 70 | """Public constructor""" |
|
71 | 71 | |
|
72 | 72 | #Call base class |
|
73 | 73 | super(NbConvertApp, self).__init__(**kwargs) |
|
74 | 74 | |
|
75 | 75 | #Register class here to have help with help all |
|
76 | 76 | self.classes.insert(0, Exporter) |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def start(self, argv=None): |
|
80 | 80 | """Entrypoint of NbConvert application. |
|
81 | 81 | |
|
82 | 82 | Parameters |
|
83 | 83 | ---------- |
|
84 | 84 | argv : list |
|
85 | 85 | Commandline arguments |
|
86 | 86 | """ |
|
87 | 87 | |
|
88 | 88 | #Parse the commandline options. |
|
89 | 89 | self.parse_command_line(argv) |
|
90 | 90 | |
|
91 | 91 | #Call base |
|
92 | 92 | super(NbConvertApp, self).start() |
|
93 | 93 | |
|
94 | 94 | #The last arguments in list will be used by nbconvert |
|
95 | 95 | export_type = (self.extra_args)[1] |
|
96 | 96 | ipynb_file = (self.extra_args)[2] |
|
97 | 97 | |
|
98 | 98 | #Export |
|
99 |
return_value = export_by_name( |
|
|
99 | return_value = export_by_name(export_type, ipynb_file) | |
|
100 | 100 | if return_value is None: |
|
101 | 101 | print("Error: '%s' template not found." % export_type) |
|
102 | 102 | return |
|
103 | 103 | else: |
|
104 | 104 | (output, resources, exporter) = return_value |
|
105 | 105 | |
|
106 | 106 | #TODO: Allow user to set output directory and file. |
|
107 | 107 | destination_filename = None |
|
108 | 108 | destination_directory = None |
|
109 | 109 | if self.write: |
|
110 | 110 | |
|
111 | 111 | #Get the file name without the '.ipynb' (6 chars) extension and then |
|
112 | 112 | #remove any addition periods and spaces. The resulting name will |
|
113 | 113 | #be used to create the directory that the files will be exported |
|
114 | 114 | #into. |
|
115 | 115 | out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') |
|
116 | 116 | destination_filename = os.path.join(out_root+'.'+exporter.file_extension) |
|
117 | 117 | |
|
118 | 118 | destination_directory = out_root+'_files' |
|
119 | 119 | if not os.path.exists(destination_directory): |
|
120 | 120 | os.mkdir(destination_directory) |
|
121 | 121 | |
|
122 | 122 | #Write the results |
|
123 | 123 | if self.stdout or not (destination_filename is None and destination_directory is None): |
|
124 | 124 | self._write_results(output, resources, self.stdout, destination_filename, destination_directory) |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | def _write_results(self, output, resources, stdout=False, destination_filename=None, destination_directory=None): |
|
128 | 128 | """Output the conversion results to the console and/or filesystem |
|
129 | 129 | |
|
130 | 130 | Parameters |
|
131 | 131 | ---------- |
|
132 | 132 | output : str |
|
133 | 133 | Output of conversion |
|
134 | 134 | resources : dictionary |
|
135 | 135 | Additional input/output used by the transformers. For |
|
136 | 136 | example, the ExtractFigure transformer outputs the |
|
137 | 137 | figures it extracts into this dictionary. This method |
|
138 | 138 | relies on the figures being in this dictionary when |
|
139 | 139 | attempting to write the figures to the file system. |
|
140 | 140 | stdout : bool, Optional |
|
141 | 141 | Whether or not to echo output to console |
|
142 | 142 | destination_filename : str, Optional |
|
143 | 143 | Filename to write output into. If None, output is not |
|
144 | 144 | written to a file. |
|
145 | 145 | destination_directory : str, Optional |
|
146 | 146 | Directory to write notebook data (i.e. figures) to. If |
|
147 | 147 | None, figures are not written to the file system. |
|
148 | 148 | """ |
|
149 | 149 | |
|
150 | 150 | if stdout: |
|
151 | 151 | print(output.encode('utf-8')) |
|
152 | 152 | |
|
153 | 153 | #Write file output from conversion. |
|
154 | 154 | if not destination_filename is None: |
|
155 | 155 | with io.open(destination_filename, 'w') as f: |
|
156 | 156 | f.write(output) |
|
157 | 157 | |
|
158 | 158 | #Get the key names used by the extract figure transformer |
|
159 | 159 | figures_key = extractfigure.FIGURES_KEY |
|
160 | 160 | binary_key = extractfigure.BINARY_KEY |
|
161 | 161 | text_key = extractfigure.TEXT_KEY |
|
162 | 162 | |
|
163 | 163 | #Output any associate figures into the same "root" directory. |
|
164 | 164 | binkeys = resources.get(figures_key, {}).get(binary_key,{}).keys() |
|
165 | 165 | textkeys = resources.get(figures_key, {}).get(text_key,{}).keys() |
|
166 | 166 | if binkeys or textkeys : |
|
167 | 167 | if not destination_directory is None: |
|
168 | 168 | for key in binkeys: |
|
169 | 169 | with io.open(os.path.join(destination_directory, key), 'wb') as f: |
|
170 | 170 | f.write(resources[figures_key][binary_key][key]) |
|
171 | 171 | for key in textkeys: |
|
172 | 172 | with io.open(os.path.join(destination_directory, key), 'w') as f: |
|
173 | 173 | f.write(resources[figures_key][text_key][key]) |
|
174 | 174 | |
|
175 | 175 | #Figures that weren't exported which will need to be created by the |
|
176 | 176 | #user. Tell the user what figures these are. |
|
177 | 177 | if stdout: |
|
178 | 178 | print(KEYS_PROMPT_HEAD) |
|
179 | 179 | print(resources[figures_key].keys()) |
|
180 | 180 | print(KEYS_PROMPT_BODY) |
|
181 | 181 | |
|
182 | 182 | #----------------------------------------------------------------------------- |
|
183 | 183 | #Script main |
|
184 | 184 | #----------------------------------------------------------------------------- |
|
185 | 185 | |
|
186 | 186 | def main(): |
|
187 | 187 | """Application entry point""" |
|
188 | 188 | |
|
189 | 189 | app = NbConvertApp.instance() |
|
190 | 190 | app.description = __doc__ |
|
191 | 191 | app.start(argv=sys.argv) |
|
192 | 192 | |
|
193 | 193 | #Check to see if python is calling this file directly. |
|
194 | 194 | if __name__ == '__main__': |
|
195 | 195 | main() No newline at end of file |
@@ -1,483 +1,212 | |||
|
1 | 1 | """ |
|
2 | 2 | Module containing single call export functions. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | import sys |
|
17 | 17 | import inspect |
|
18 | from functools import wraps | |
|
18 | 19 | |
|
19 | 20 | from IPython.nbformat.v3.nbbase import NotebookNode |
|
20 | 21 | |
|
21 | 22 | from .exporters.exporter import Exporter |
|
22 | 23 | from .exporters.basichtml import BasicHtmlExporter |
|
23 | 24 | from .exporters.fullhtml import FullHtmlExporter |
|
24 | 25 | from .exporters.latex import LatexExporter |
|
25 | 26 | from .exporters.markdown import MarkdownExporter |
|
26 | 27 | from .exporters.python import PythonExporter |
|
27 | 28 | from .exporters.python_armor import PythonArmorExporter |
|
28 | 29 | from .exporters.reveal import RevealExporter |
|
29 | 30 | from .exporters.rst import RstExporter |
|
30 | 31 | from .exporters.sphinx_howto import SphinxHowtoExporter |
|
31 | 32 | from .exporters.sphinx_manual import SphinxManualExporter |
|
32 | 33 | |
|
33 | ||
|
34 | 34 | #----------------------------------------------------------------------------- |
|
35 | # Functions | |
|
35 | # Classes | |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | def export(nb, config=None, transformers=None, filters=None, exporter_type=Exporter): | |
|
39 | """ | |
|
40 | Export a notebook object using specific exporter class. | |
|
38 | def DocDecorator(f): | |
|
41 | 39 | |
|
42 | Parameters | |
|
43 | ---------- | |
|
40 | #Set docstring of function | |
|
41 | f.__doc__ = f.__doc__ + """ | |
|
42 | nb : Notebook node | |
|
44 | 43 | config : config |
|
45 | 44 | User configuration instance. |
|
46 | 45 | transformers : list[of transformer] |
|
47 | 46 | Custom transformers to apply to the notebook prior to engaging |
|
48 | 47 | the Jinja template engine. Any transformers specified here |
|
49 | 48 | will override existing transformers if a naming conflict |
|
50 | 49 | occurs. |
|
51 | 50 | filters : list[of filter] |
|
52 | 51 | Custom filters to make accessible to the Jinja templates. Any |
|
53 | 52 | filters specified here will override existing filters if a |
|
54 | 53 | naming conflict occurs. |
|
55 | exporter_type: | |
|
56 | Class type of the exporter that should be used. This method | |
|
57 | will initialize it's own instance of the class. It is | |
|
58 | ASSUMED that the class type provided exposes a | |
|
59 | constructor (__init__) with the same signature as the | |
|
60 | base Exporter class. | |
|
61 | 54 | |
|
62 | 55 | Returns |
|
63 | 56 | ---------- |
|
64 | 57 | tuple- output, resources, exporter_instance |
|
65 | 58 | output : str |
|
66 | 59 | Jinja 2 output. This is the resulting converted notebook. |
|
67 | 60 | resources : dictionary |
|
68 | 61 | Dictionary of resources used prior to and during the conversion |
|
69 | 62 | process. |
|
70 | 63 | exporter_instance : Exporter |
|
71 | 64 | Instance of the Exporter class used to export the document. Useful |
|
72 | 65 | to caller because it provides a 'file_extension' property which |
|
73 | specifies what extension the output should be saved as. | |
|
66 | specifies what extension the output should be saved as.""" | |
|
67 | ||
|
68 | @wraps(f) | |
|
69 | def decorator(*args, **kwargs): | |
|
70 | return f(*args, **kwargs) | |
|
71 | ||
|
72 | return decorator | |
|
73 | ||
|
74 | ||
|
75 | #----------------------------------------------------------------------------- | |
|
76 | # Functions | |
|
77 | #----------------------------------------------------------------------------- | |
|
78 | ||
|
79 | @DocDecorator | |
|
80 | def export(exporter_type, nb, config=None, transformers=None, filters=None): | |
|
81 | """ | |
|
82 | Export a notebook object using specific exporter class. | |
|
83 | ||
|
84 | exporter_type : Exporter class type | |
|
85 | Class type of the exporter that should be used. This method | |
|
86 | will initialize it's own instance of the class. It is | |
|
87 | ASSUMED that the class type provided exposes a | |
|
88 | constructor (__init__) with the same signature as the | |
|
89 | base Exporter class.} | |
|
74 | 90 | """ |
|
75 | 91 | |
|
76 | 92 | #Check arguments |
|
77 | 93 | if exporter_type is None: |
|
78 | 94 | raise TypeError("Exporter is None") |
|
79 | 95 | elif not issubclass(exporter_type, Exporter): |
|
80 | 96 | raise TypeError("Exporter type does not inherit from Exporter (base)") |
|
81 | 97 | |
|
82 | 98 | if nb is None: |
|
83 | 99 | raise TypeError("nb is None") |
|
84 | 100 | |
|
85 | 101 | #Create the exporter |
|
86 | 102 | exporter_instance = exporter_type(preprocessors=transformers, |
|
87 | 103 | jinja_filters=filters, config=config) |
|
88 | 104 | |
|
89 | 105 | #Try to convert the notebook using the appropriate conversion function. |
|
90 | 106 | if isinstance(nb, NotebookNode): |
|
91 | 107 | output, resources = exporter_instance.from_notebook_node(nb) |
|
92 | 108 | elif isinstance(nb, basestring): |
|
93 | 109 | output, resources = exporter_instance.from_filename(nb) |
|
94 | 110 | else: |
|
95 | 111 | output, resources = exporter_instance.from_file(nb) |
|
96 | 112 | return output, resources, exporter_instance |
|
97 | 113 | |
|
98 | 114 | |
|
115 | @DocDecorator | |
|
99 | 116 | def export_sphinx_manual(nb, config=None, transformers=None, filters=None): |
|
100 | 117 | """ |
|
101 | 118 | Export a notebook object to Sphinx Manual LaTeX |
|
102 | ||
|
103 | Parameters | |
|
104 | ---------- | |
|
105 | config : config | |
|
106 | User configuration instance. | |
|
107 | transformers : list[of transformer] | |
|
108 | Custom transformers to apply to the notebook prior to engaging | |
|
109 | the Jinja template engine. Any transformers specified here | |
|
110 | will override existing transformers if a naming conflict | |
|
111 | occurs. | |
|
112 | filters : list[of filter] | |
|
113 | Custom filters to make accessible to the Jinja templates. Any | |
|
114 | filters specified here will override existing filters if a | |
|
115 | naming conflict occurs. | |
|
116 | ||
|
117 | Returns | |
|
118 | ---------- | |
|
119 | tuple- output, resources, exporter_instance | |
|
120 | output : str | |
|
121 | Jinja 2 output. This is the resulting converted notebook. | |
|
122 | resources : dictionary | |
|
123 | Dictionary of resources used prior to and during the conversion | |
|
124 | process. | |
|
125 | exporter_instance : Exporter | |
|
126 | Instance of the Exporter class used to export the document. Useful | |
|
127 | to caller because it provides a 'file_extension' property which | |
|
128 | specifies what extension the output should be saved as. | |
|
129 | 119 | """ |
|
130 |
return export(nb, config, transformers, filters |
|
|
120 | return export(SphinxManualExporter, nb, config, transformers, filters) | |
|
131 | 121 | |
|
132 | 122 | |
|
123 | @DocDecorator | |
|
133 | 124 | def export_sphinx_howto(nb, config=None, transformers=None, filters=None): |
|
134 | 125 | """ |
|
135 | 126 | Export a notebook object to Sphinx HowTo LaTeX |
|
136 | ||
|
137 | Parameters | |
|
138 | ---------- | |
|
139 | config : config | |
|
140 | User configuration instance. | |
|
141 | transformers : list[of transformer] | |
|
142 | Custom transformers to apply to the notebook prior to engaging | |
|
143 | the Jinja template engine. Any transformers specified here | |
|
144 | will override existing transformers if a naming conflict | |
|
145 | occurs. | |
|
146 | filters : list[of filter] | |
|
147 | Custom filters to make accessible to the Jinja templates. Any | |
|
148 | filters specified here will override existing filters if a | |
|
149 | naming conflict occurs. | |
|
150 | ||
|
151 | Returns | |
|
152 | ---------- | |
|
153 | tuple- output, resources, exporter_instance | |
|
154 | output : str | |
|
155 | Jinja 2 output. This is the resulting converted notebook. | |
|
156 | resources : dictionary | |
|
157 | Dictionary of resources used prior to and during the conversion | |
|
158 | process. | |
|
159 | exporter_instance : Exporter | |
|
160 | Instance of the Exporter class used to export the document. Useful | |
|
161 | to caller because it provides a 'file_extension' property which | |
|
162 | specifies what extension the output should be saved as. | |
|
163 | 127 | """ |
|
164 |
return export(nb, config, transformers, filters |
|
|
128 | return export(SphinxHowtoExporter, nb, config, transformers, filters) | |
|
165 | 129 | |
|
166 | 130 | |
|
131 | @DocDecorator | |
|
167 | 132 | def export_basic_html(nb, config=None, transformers=None, filters=None): |
|
168 | 133 | """ |
|
169 | 134 | Export a notebook object to Basic HTML |
|
170 | ||
|
171 | Parameters | |
|
172 | ---------- | |
|
173 | config : config | |
|
174 | User configuration instance. | |
|
175 | transformers : list[of transformer] | |
|
176 | Custom transformers to apply to the notebook prior to engaging | |
|
177 | the Jinja template engine. Any transformers specified here | |
|
178 | will override existing transformers if a naming conflict | |
|
179 | occurs. | |
|
180 | filters : list[of filter] | |
|
181 | Custom filters to make accessible to the Jinja templates. Any | |
|
182 | filters specified here will override existing filters if a | |
|
183 | naming conflict occurs. | |
|
184 | ||
|
185 | Returns | |
|
186 | ---------- | |
|
187 | tuple- output, resources, exporter_instance | |
|
188 | output : str | |
|
189 | Jinja 2 output. This is the resulting converted notebook. | |
|
190 | resources : dictionary | |
|
191 | Dictionary of resources used prior to and during the conversion | |
|
192 | process. | |
|
193 | exporter_instance : Exporter | |
|
194 | Instance of the Exporter class used to export the document. Useful | |
|
195 | to caller because it provides a 'file_extension' property which | |
|
196 | specifies what extension the output should be saved as. | |
|
197 | 135 | """ |
|
198 |
return export(nb, config, transformers, filters |
|
|
136 | return export(BasicHtmlExporter, nb, config, transformers, filters) | |
|
199 | 137 | |
|
200 | 138 | |
|
139 | @DocDecorator | |
|
201 | 140 | def export_full_html(nb, config=None, transformers=None, filters=None): |
|
202 | 141 | """ |
|
203 | 142 | Export a notebook object to Full HTML |
|
204 | ||
|
205 | Parameters | |
|
206 | ---------- | |
|
207 | config : config | |
|
208 | User configuration instance. | |
|
209 | transformers : list[of transformer] | |
|
210 | Custom transformers to apply to the notebook prior to engaging | |
|
211 | the Jinja template engine. Any transformers specified here | |
|
212 | will override existing transformers if a naming conflict | |
|
213 | occurs. | |
|
214 | filters : list[of filter] | |
|
215 | Custom filters to make accessible to the Jinja templates. Any | |
|
216 | filters specified here will override existing filters if a | |
|
217 | naming conflict occurs. | |
|
218 | ||
|
219 | Returns | |
|
220 | ---------- | |
|
221 | tuple- output, resources, exporter_instance | |
|
222 | output : str | |
|
223 | Jinja 2 output. This is the resulting converted notebook. | |
|
224 | resources : dictionary | |
|
225 | Dictionary of resources used prior to and during the conversion | |
|
226 | process. | |
|
227 | exporter_instance : Exporter | |
|
228 | Instance of the Exporter class used to export the document. Useful | |
|
229 | to caller because it provides a 'file_extension' property which | |
|
230 | specifies what extension the output should be saved as. | |
|
231 | 143 | """ |
|
232 |
return export(nb, config, transformers, filters |
|
|
144 | return export(FullHtmlExporter, nb, config, transformers, filters) | |
|
233 | 145 | |
|
234 | 146 | |
|
147 | @DocDecorator | |
|
235 | 148 | def export_latex(nb, config=None, transformers=None, filters=None): |
|
236 | 149 | """ |
|
237 | 150 | Export a notebook object to LaTeX |
|
238 | ||
|
239 | Parameters | |
|
240 | ---------- | |
|
241 | config : config | |
|
242 | User configuration instance. | |
|
243 | transformers : list[of transformer] | |
|
244 | Custom transformers to apply to the notebook prior to engaging | |
|
245 | the Jinja template engine. Any transformers specified here | |
|
246 | will override existing transformers if a naming conflict | |
|
247 | occurs. | |
|
248 | filters : list[of filter] | |
|
249 | Custom filters to make accessible to the Jinja templates. Any | |
|
250 | filters specified here will override existing filters if a | |
|
251 | naming conflict occurs. | |
|
252 | ||
|
253 | Returns | |
|
254 | ---------- | |
|
255 | tuple- output, resources, exporter_instance | |
|
256 | output : str | |
|
257 | Jinja 2 output. This is the resulting converted notebook. | |
|
258 | resources : dictionary | |
|
259 | Dictionary of resources used prior to and during the conversion | |
|
260 | process. | |
|
261 | exporter_instance : Exporter | |
|
262 | Instance of the Exporter class used to export the document. Useful | |
|
263 | to caller because it provides a 'file_extension' property which | |
|
264 | specifies what extension the output should be saved as. | |
|
265 | 151 | """ |
|
266 |
return export(nb, config, transformers, filters |
|
|
152 | return export(LatexExporter, nb, config, transformers, filters) | |
|
267 | 153 | |
|
268 | 154 | |
|
155 | @DocDecorator | |
|
269 | 156 | def export_markdown(nb, config=None, transformers=None, filters=None): |
|
270 | 157 | """ |
|
271 | 158 | Export a notebook object to Markdown |
|
272 | ||
|
273 | Parameters | |
|
274 | ---------- | |
|
275 | config : config | |
|
276 | User configuration instance. | |
|
277 | transformers : list[of transformer] | |
|
278 | Custom transformers to apply to the notebook prior to engaging | |
|
279 | the Jinja template engine. Any transformers specified here | |
|
280 | will override existing transformers if a naming conflict | |
|
281 | occurs. | |
|
282 | filters : list[of filter] | |
|
283 | Custom filters to make accessible to the Jinja templates. Any | |
|
284 | filters specified here will override existing filters if a | |
|
285 | naming conflict occurs. | |
|
286 | ||
|
287 | Returns | |
|
288 | ---------- | |
|
289 | tuple- output, resources, exporter_instance | |
|
290 | output : str | |
|
291 | Jinja 2 output. This is the resulting converted notebook. | |
|
292 | resources : dictionary | |
|
293 | Dictionary of resources used prior to and during the conversion | |
|
294 | process. | |
|
295 | exporter_instance : Exporter | |
|
296 | Instance of the Exporter class used to export the document. Useful | |
|
297 | to caller because it provides a 'file_extension' property which | |
|
298 | specifies what extension the output should be saved as. | |
|
299 | 159 | """ |
|
300 |
return export(nb, config, transformers, filters |
|
|
160 | return export(MarkdownExporter, nb, config, transformers, filters) | |
|
301 | 161 | |
|
302 | 162 | |
|
163 | @DocDecorator | |
|
303 | 164 | def export_python(nb, config=None, transformers=None, filters=None): |
|
304 | 165 | """ |
|
305 | 166 | Export a notebook object to Python |
|
306 | ||
|
307 | Parameters | |
|
308 | ---------- | |
|
309 | config : config | |
|
310 | User configuration instance. | |
|
311 | transformers : list[of transformer] | |
|
312 | Custom transformers to apply to the notebook prior to engaging | |
|
313 | the Jinja template engine. Any transformers specified here | |
|
314 | will override existing transformers if a naming conflict | |
|
315 | occurs. | |
|
316 | filters : list[of filter] | |
|
317 | Custom filters to make accessible to the Jinja templates. Any | |
|
318 | filters specified here will override existing filters if a | |
|
319 | naming conflict occurs. | |
|
320 | ||
|
321 | Returns | |
|
322 | ---------- | |
|
323 | tuple- output, resources, exporter_instance | |
|
324 | output : str | |
|
325 | Jinja 2 output. This is the resulting converted notebook. | |
|
326 | resources : dictionary | |
|
327 | Dictionary of resources used prior to and during the conversion | |
|
328 | process. | |
|
329 | exporter_instance : Exporter | |
|
330 | Instance of the Exporter class used to export the document. Useful | |
|
331 | to caller because it provides a 'file_extension' property which | |
|
332 | specifies what extension the output should be saved as. | |
|
333 | 167 | """ |
|
334 |
return export(nb, config, transformers, filters |
|
|
168 | return export(PythonExporter, nb, config, transformers, filters) | |
|
335 | 169 | |
|
336 | 170 | |
|
171 | @DocDecorator | |
|
337 | 172 | def export_python_armor(nb, config=None, transformers=None, filters=None): |
|
338 | 173 | """ |
|
339 | 174 | Export a notebook object to Python (Armor) |
|
340 | ||
|
341 | Parameters | |
|
342 | ---------- | |
|
343 | config : config | |
|
344 | User configuration instance. | |
|
345 | transformers : list[of transformer] | |
|
346 | Custom transformers to apply to the notebook prior to engaging | |
|
347 | the Jinja template engine. Any transformers specified here | |
|
348 | will override existing transformers if a naming conflict | |
|
349 | occurs. | |
|
350 | filters : list[of filter] | |
|
351 | Custom filters to make accessible to the Jinja templates. Any | |
|
352 | filters specified here will override existing filters if a | |
|
353 | naming conflict occurs. | |
|
354 | ||
|
355 | Returns | |
|
356 | ---------- | |
|
357 | tuple- output, resources, exporter_instance | |
|
358 | output : str | |
|
359 | Jinja 2 output. This is the resulting converted notebook. | |
|
360 | resources : dictionary | |
|
361 | Dictionary of resources used prior to and during the conversion | |
|
362 | process. | |
|
363 | exporter_instance : Exporter | |
|
364 | Instance of the Exporter class used to export the document. Useful | |
|
365 | to caller because it provides a 'file_extension' property which | |
|
366 | specifies what extension the output should be saved as. | |
|
367 | 175 | """ |
|
368 |
return export(nb, config, transformers, filters |
|
|
176 | return export(PythonArmorExporter, nb, config, transformers, filters) | |
|
369 | 177 | |
|
370 | 178 | |
|
179 | @DocDecorator | |
|
371 | 180 | def export_reveal(nb, config=None, transformers=None, filters=None): |
|
372 | 181 | """ |
|
373 | 182 | Export a notebook object to Reveal |
|
374 | ||
|
375 | Parameters | |
|
376 | ---------- | |
|
377 | config : config | |
|
378 | User configuration instance. | |
|
379 | transformers : list[of transformer] | |
|
380 | Custom transformers to apply to the notebook prior to engaging | |
|
381 | the Jinja template engine. Any transformers specified here | |
|
382 | will override existing transformers if a naming conflict | |
|
383 | occurs. | |
|
384 | filters : list[of filter] | |
|
385 | Custom filters to make accessible to the Jinja templates. Any | |
|
386 | filters specified here will override existing filters if a | |
|
387 | naming conflict occurs. | |
|
388 | ||
|
389 | Returns | |
|
390 | ---------- | |
|
391 | tuple- output, resources, exporter_instance | |
|
392 | output : str | |
|
393 | Jinja 2 output. This is the resulting converted notebook. | |
|
394 | resources : dictionary | |
|
395 | Dictionary of resources used prior to and during the conversion | |
|
396 | process. | |
|
397 | exporter_instance : Exporter | |
|
398 | Instance of the Exporter class used to export the document. Useful | |
|
399 | to caller because it provides a 'file_extension' property which | |
|
400 | specifies what extension the output should be saved as. | |
|
401 | 183 | """ |
|
402 |
return export(nb, config, transformers, filters |
|
|
184 | return export(RevealExporter, nb, config, transformers, filters) | |
|
403 | 185 | |
|
404 | 186 | |
|
187 | @DocDecorator | |
|
405 | 188 | def export_rst(nb, config=None, transformers=None, filters=None): |
|
406 | 189 | """ |
|
407 | 190 | Export a notebook object to RST |
|
408 | ||
|
409 | Parameters | |
|
410 | ---------- | |
|
411 | config : config | |
|
412 | User configuration instance. | |
|
413 | transformers : list[of transformer] | |
|
414 | Custom transformers to apply to the notebook prior to engaging | |
|
415 | the Jinja template engine. Any transformers specified here | |
|
416 | will override existing transformers if a naming conflict | |
|
417 | occurs. | |
|
418 | filters : list[of filter] | |
|
419 | Custom filters to make accessible to the Jinja templates. Any | |
|
420 | filters specified here will override existing filters if a | |
|
421 | naming conflict occurs. | |
|
422 | ||
|
423 | Returns | |
|
424 | ---------- | |
|
425 | tuple- output, resources, exporter_instance | |
|
426 | output : str | |
|
427 | Jinja 2 output. This is the resulting converted notebook. | |
|
428 | resources : dictionary | |
|
429 | Dictionary of resources used prior to and during the conversion | |
|
430 | process. | |
|
431 | exporter_instance : Exporter | |
|
432 | Instance of the Exporter class used to export the document. Useful | |
|
433 | to caller because it provides a 'file_extension' property which | |
|
434 | specifies what extension the output should be saved as. | |
|
435 | 191 | """ |
|
436 |
return export(nb, config, transformers, filters |
|
|
192 | return export(RstExporter, nb, config, transformers, filters) | |
|
437 | 193 | |
|
438 | 194 | |
|
439 | def export_by_name(nb, template_name, config=None, transformers=None, filters=None): | |
|
195 | @DocDecorator | |
|
196 | def export_by_name(template_name, nb, config=None, transformers=None, filters=None): | |
|
440 | 197 | """ |
|
441 | 198 | Export a notebook object to a template type by its name. Reflection |
|
442 | 199 | (Inspect) is used to find the template's corresponding explicit export |
|
443 | 200 | method defined in this module. That method is then called directly. |
|
444 | 201 | |
|
445 | Parameters | |
|
446 | ---------- | |
|
447 | 202 | template_name : str |
|
448 | 203 | Name of the template style to export to. |
|
449 | config : config | |
|
450 | User configuration instance. | |
|
451 | transformers : list[of transformer] | |
|
452 | Custom transformers to apply to the notebook prior to engaging | |
|
453 | the Jinja template engine. Any transformers specified here | |
|
454 | will override existing transformers if a naming conflict | |
|
455 | occurs. | |
|
456 | filters : list[of filter] | |
|
457 | Custom filters to make accessible to the Jinja templates. Any | |
|
458 | filters specified here will override existing filters if a | |
|
459 | naming conflict occurs. | |
|
460 | ||
|
461 | Returns | |
|
462 | ---------- | |
|
463 | tuple- (output, resources, exporter_instance) | |
|
464 | None- if template not found | |
|
465 | ||
|
466 | output : str | |
|
467 | Jinja 2 output. This is the resulting converted notebook. | |
|
468 | resources : dictionary | |
|
469 | Dictionary of resources used prior to and during the conversion | |
|
470 | process. | |
|
471 | exporter_instance : Exporter | |
|
472 | Instance of the Exporter class used to export the document. Useful | |
|
473 | to caller because it provides a 'file_extension' property which | |
|
474 | specifies what extension the output should be saved as. | |
|
475 | 204 | """ |
|
476 | 205 | |
|
477 | 206 | function_name = "export_" + template_name.lower() |
|
478 | 207 | |
|
479 | 208 | if function_name in globals(): |
|
480 | 209 | return globals()[function_name](nb, config, transformers, filters) |
|
481 | 210 | else: |
|
482 | 211 | return None |
|
483 | 212 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now