Show More
@@ -1,198 +1,197 | |||
|
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 os |
|
23 | 23 | import glob |
|
24 | 24 | |
|
25 | 25 | #From IPython |
|
26 | 26 | from IPython.core.application import BaseIPythonApplication |
|
27 | 27 | from IPython.config.application import catch_config_error |
|
28 | 28 | from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type |
|
29 | 29 | from IPython.utils.importstring import import_item |
|
30 | 30 | |
|
31 | 31 | from .exporters.export import export_by_name, get_export_names, ExporterNameError |
|
32 | 32 | from .exporters.exporter import Exporter |
|
33 | 33 | from .writers.base import WriterBase |
|
34 | 34 | from .utils.config import GlobalConfigurable |
|
35 | 35 | |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | #Classes and functions |
|
38 | 38 | #----------------------------------------------------------------------------- |
|
39 | 39 | |
|
40 | 40 | class NbConvertApp(BaseIPythonApplication): |
|
41 | 41 | """Application used to convert to and from notebook file type (*.ipynb)""" |
|
42 | 42 | |
|
43 | name = 'ipython-nbconvert' | |
|
43 | 44 | |
|
44 | 45 | description = Unicode( |
|
45 | 46 | u"""This application is used to convert notebook files (*.ipynb). |
|
46 | 47 | An ipython config file can be used to batch convert notebooks in the |
|
47 | 48 | current directory.""") |
|
48 | 49 | |
|
49 | 50 | examples = Unicode(u""" |
|
50 | 51 | Running `ipython nbconvert` will read the directory config file and then |
|
51 | 52 | apply it to one or more notebooks. |
|
52 | 53 | |
|
53 | 54 | Multiple notebooks can be given at the command line in a couple of |
|
54 | 55 | different ways: |
|
55 | 56 | |
|
56 | 57 | > ipython nbconvert notebook*.ipynb |
|
57 | 58 | > ipython nbconvert notebook1.ipynb notebook2.ipynb |
|
58 | 59 | > ipython nbconvert # this will use the config file to fill in the notebooks |
|
59 | 60 | """) |
|
60 | 61 | |
|
61 | config_file_name = Unicode(u'ipython_nbconvert_config.py') | |
|
62 | ||
|
63 | 62 | #Writer specific variables |
|
64 | 63 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', |
|
65 | 64 | help="""Instance of the writer class used to write the |
|
66 | 65 | results of the conversion.""") |
|
67 | 66 | writer_class = DottedObjectName('FilesWriter', config=True, |
|
68 | 67 | help="""Writer class used to write the |
|
69 | 68 | results of the conversion""") |
|
70 | 69 | writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter', |
|
71 | 70 | 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter', |
|
72 | 71 | 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'} |
|
73 | 72 | writer_factory = Type() |
|
74 | 73 | |
|
75 | 74 | def _writer_class_changed(self, name, old, new): |
|
76 | 75 | if new in self.writer_aliases: |
|
77 | 76 | new = self.writer_aliases[new] |
|
78 | 77 | self.writer_factory = import_item(new) |
|
79 | 78 | |
|
80 | 79 | |
|
81 | 80 | #Other configurable variables |
|
82 | 81 | export_format = Unicode( |
|
83 | 82 | "", config=True, |
|
84 | 83 | help="""If specified, nbconvert will convert the document(s) specified |
|
85 | 84 | using this format.""") |
|
86 | 85 | |
|
87 | 86 | notebooks = List([], config=True, help="""List of notebooks to convert. |
|
88 | 87 | Search patterns are supported.""") |
|
89 | 88 | |
|
90 | 89 | nbconvert_aliases = {'format':'NbConvertApp.export_format', |
|
91 | 90 | 'notebooks':'NbConvertApp.notebooks', |
|
92 | 91 | 'writer':'NbConvertApp.writer_class'} |
|
93 | 92 | |
|
94 | 93 | |
|
95 | 94 | @catch_config_error |
|
96 | 95 | def initialize(self, argv=None): |
|
97 | 96 | self.aliases.update(self.nbconvert_aliases) |
|
98 | 97 | |
|
99 | 98 | super(NbConvertApp, self).initialize(argv) |
|
100 | 99 | |
|
101 | 100 | #Register class here to have help with help all |
|
102 | 101 | self.classes.insert(0, Exporter) |
|
103 | 102 | self.classes.insert(0, WriterBase) |
|
104 | 103 | self.classes.insert(0, GlobalConfigurable) |
|
105 | 104 | |
|
106 | 105 | #Init |
|
107 | 106 | self.init_config(self.extra_args) |
|
108 | 107 | self.init_writer() |
|
109 | 108 | |
|
110 | 109 | |
|
111 | 110 | def init_config(self, extra_args): |
|
112 | 111 | """ |
|
113 | 112 | Add notebooks to the config if needed. Glob each notebook to replace |
|
114 | 113 | notebook patterns with filenames. |
|
115 | 114 | """ |
|
116 | 115 | |
|
117 | 116 | #Get any additional notebook patterns from the commandline |
|
118 | 117 | if len(extra_args) > 0: |
|
119 | 118 | for pattern in extra_args: |
|
120 | 119 | self.notebooks.append(pattern) |
|
121 | 120 | |
|
122 | 121 | #Use glob to replace all the notebook patterns with filenames. |
|
123 | 122 | filenames = [] |
|
124 | 123 | for pattern in self.notebooks: |
|
125 | 124 | for filename in glob.glob(pattern): |
|
126 | 125 | if not filename in filenames: |
|
127 | 126 | filenames.append(filename) |
|
128 | 127 | self.notebooks = filenames |
|
129 | 128 | |
|
130 | 129 | |
|
131 | 130 | def init_writer(self): |
|
132 | 131 | """ |
|
133 | 132 | Initialize the writer (which is stateless) |
|
134 | 133 | """ |
|
135 | 134 | self._writer_class_changed(None, self.writer_class, self.writer_class) |
|
136 | 135 | self.writer = self.writer_factory(parent=self) |
|
137 | 136 | |
|
138 | 137 | |
|
139 | 138 | def start(self, argv=None): |
|
140 | 139 | """ |
|
141 | 140 | Ran after initiialization completed |
|
142 | 141 | """ |
|
143 | 142 | super(NbConvertApp, self).start() |
|
144 | 143 | self.convert_notebooks() |
|
145 | 144 | |
|
146 | 145 | |
|
147 | 146 | def convert_notebooks(self): |
|
148 | 147 | """ |
|
149 | 148 | Convert the notebooks in the self.notebook traitlet |
|
150 | 149 | """ |
|
151 | 150 | #Export each notebook |
|
152 | 151 | conversion_success = 0 |
|
153 | 152 | for notebook_filename in self.notebooks: |
|
154 | 153 | |
|
155 | 154 | #Get a unique key for the notebook and set it in the resources object. |
|
156 | 155 | basename = os.path.basename(notebook_filename) |
|
157 | 156 | notebook_name = basename[:basename.rfind('.')] |
|
158 | 157 | resources = {} |
|
159 | 158 | resources['unique_key'] = notebook_name |
|
160 | 159 | |
|
161 | 160 | #Try to export |
|
162 | 161 | try: |
|
163 | 162 | output, resources = export_by_name(self.export_format, |
|
164 | 163 | notebook_filename, |
|
165 | 164 | resources=resources, |
|
166 | 165 | config=self.config) |
|
167 | 166 | except ExporterNameError as e: |
|
168 | 167 | print("Error: '%s' exporter not found." % self.export_format, |
|
169 | 168 | file=sys.stderr) |
|
170 | 169 | print("Known exporters are:", |
|
171 | 170 | "\n\t" + "\n\t".join(get_export_names()), |
|
172 | 171 | file=sys.stderr) |
|
173 | 172 | sys.exit(-1) |
|
174 | 173 | #except Exception as e: |
|
175 | 174 | #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr) |
|
176 | 175 | #print(e, file=sys.stderr) |
|
177 | 176 | else: |
|
178 | 177 | self.writer.write(output, resources, notebook_name=notebook_name) |
|
179 | 178 | conversion_success += 1 |
|
180 | 179 | |
|
181 | 180 | #If nothing was converted successfully, help the user. |
|
182 | 181 | if conversion_success == 0: |
|
183 | 182 | |
|
184 | 183 | #No notebooks were specified, show help. |
|
185 | 184 | if len(self.notebooks) == 0: |
|
186 | 185 | self.print_help() |
|
187 | 186 | |
|
188 | 187 | #Notebooks were specified, but not converted successfully. Show how |
|
189 | 188 | #to access help. |
|
190 | 189 | else: |
|
191 | 190 | print('For help, use "ipython nbconvert --help"') |
|
192 | 191 | |
|
193 | 192 | |
|
194 | 193 | #----------------------------------------------------------------------------- |
|
195 | 194 | # Main entry point |
|
196 | 195 | #----------------------------------------------------------------------------- |
|
197 | 196 | |
|
198 | 197 | launch_new_instance = NbConvertApp.launch_instance |
General Comments 0
You need to be logged in to leave comments.
Login now