Show More
@@ -15,13 +15,13 b' Command-line interface for the NbConvert conversion utility.' | |||
|
15 | 15 | #Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | #Stdlib imports | |
|
18 | # Stdlib imports | |
|
19 | 19 | from __future__ import print_function |
|
20 | 20 | import sys |
|
21 | 21 | import os |
|
22 | 22 | import glob |
|
23 | 23 | |
|
24 | #From IPython | |
|
24 | # From IPython | |
|
25 | 25 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags |
|
26 | 26 | from IPython.config import catch_config_error, Configurable |
|
27 | 27 | from IPython.utils.traitlets import ( |
@@ -103,7 +103,7 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
103 | 103 | |
|
104 | 104 | > ipython nbconvert --config mycfg.py |
|
105 | 105 | """.format(get_export_names())) |
|
106 | #Writer specific variables | |
|
106 | # Writer specific variables | |
|
107 | 107 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', |
|
108 | 108 | help="""Instance of the writer class used to write the |
|
109 | 109 | results of the conversion.""") |
@@ -121,7 +121,7 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
121 | 121 | self.writer_factory = import_item(new) |
|
122 | 122 | |
|
123 | 123 | |
|
124 | #Other configurable variables | |
|
124 | # Other configurable variables | |
|
125 | 125 | export_format = CaselessStrEnum(get_export_names(), |
|
126 | 126 | default_value="full_html", |
|
127 | 127 | config=True, |
@@ -153,7 +153,7 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
153 | 153 | else: |
|
154 | 154 | patterns = self.notebooks |
|
155 | 155 | |
|
156 | #Use glob to replace all the notebook patterns with filenames. | |
|
156 | # Use glob to replace all the notebook patterns with filenames. | |
|
157 | 157 | filenames = [] |
|
158 | 158 | for pattern in patterns: |
|
159 | 159 | for filename in glob.glob(pattern): |
@@ -179,18 +179,18 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
179 | 179 | """ |
|
180 | 180 | Convert the notebooks in the self.notebook traitlet |
|
181 | 181 | """ |
|
182 | #Export each notebook | |
|
182 | # Export each notebook | |
|
183 | 183 | conversion_success = 0 |
|
184 | 184 | for notebook_filename in self.notebooks: |
|
185 | 185 | |
|
186 | #Get a unique key for the notebook and set it in the resources object. | |
|
186 | # Get a unique key for the notebook and set it in the resources object. | |
|
187 | 187 | basename = os.path.basename(notebook_filename) |
|
188 | 188 | notebook_name = basename[:basename.rfind('.')] |
|
189 | 189 | resources = {} |
|
190 | 190 | resources['unique_key'] = notebook_name |
|
191 | 191 | resources['output_files_dir'] = '%s_files' % notebook_name |
|
192 | 192 | |
|
193 | #Try to export | |
|
193 | # Try to export | |
|
194 | 194 | try: |
|
195 | 195 | output, resources = export_by_name(self.export_format, |
|
196 | 196 | notebook_filename, |
@@ -203,22 +203,22 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
203 | 203 | "\n\t" + "\n\t".join(get_export_names()), |
|
204 | 204 | file=sys.stderr) |
|
205 | 205 | sys.exit(-1) |
|
206 | #except Exception as e: | |
|
207 | #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr) | |
|
208 | #print(e, file=sys.stderr) | |
|
206 | # except Exception as e: | |
|
207 | # print("Error: could not export '%s'" % notebook_filename, file=sys.stderr) | |
|
208 | # print(e, file=sys.stderr) | |
|
209 | 209 | else: |
|
210 | 210 | self.writer.write(output, resources, notebook_name=notebook_name) |
|
211 | 211 | conversion_success += 1 |
|
212 | 212 | |
|
213 | #If nothing was converted successfully, help the user. | |
|
213 | # If nothing was converted successfully, help the user. | |
|
214 | 214 | if conversion_success == 0: |
|
215 | 215 | |
|
216 | #No notebooks were specified, show help. | |
|
216 | # No notebooks were specified, show help. | |
|
217 | 217 | if len(self.notebooks) == 0: |
|
218 | 218 | self.print_help() |
|
219 | 219 | |
|
220 | #Notebooks were specified, but not converted successfully. Show how | |
|
221 | #to access help. | |
|
220 | # Notebooks were specified, but not converted successfully. Show how | |
|
221 | # to access help. | |
|
222 | 222 | else: |
|
223 | 223 | print('For help, use "ipython nbconvert --help"') |
|
224 | 224 |
@@ -36,7 +36,7 b' class FilesWriter(WriterBase):' | |||
|
36 | 36 | to output to the current directory""") |
|
37 | 37 | |
|
38 | 38 | |
|
39 | #Make sure that the output directory exists. | |
|
39 | # Make sure that the output directory exists. | |
|
40 | 40 | def _build_directory_changed(self, name, old, new): |
|
41 | 41 | if new and not os.path.isdir(new): |
|
42 | 42 | os.makedirs(new) |
@@ -57,46 +57,46 b' class FilesWriter(WriterBase):' | |||
|
57 | 57 | See base for more... |
|
58 | 58 | """ |
|
59 | 59 | |
|
60 | #Pull the extension from the resources dict. | |
|
60 | # Pull the extension and subdir from the resources dict. | |
|
61 | 61 | output_extension = resources['output_extension'] |
|
62 | 62 | |
|
63 | #Write all of the extracted resources to the destination directory. | |
|
64 | #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG | |
|
65 | #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | |
|
63 | # Write all of the extracted resources to the destination directory. | |
|
64 | # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG | |
|
65 | # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | |
|
66 | 66 | for filename, data in resources.get('figures', {}).items(): |
|
67 | 67 | |
|
68 | #Determine where to write the file to | |
|
68 | # Determine where to write the file to | |
|
69 | 69 | dest = os.path.join(self.build_directory, filename) |
|
70 | 70 | path = os.path.dirname(dest) |
|
71 | 71 | if not os.path.isdir(path): |
|
72 | 72 | os.makedirs(path) |
|
73 | 73 | |
|
74 | #Write file | |
|
74 | # Write file | |
|
75 | 75 | with io.open(dest, 'wb') as f: |
|
76 | 76 | f.write(data) |
|
77 | 77 | |
|
78 | #Copy referenced files to output directory | |
|
78 | # Copy referenced files to output directory | |
|
79 | 79 | if self.build_directory: |
|
80 | 80 | for filename in self.files: |
|
81 | 81 | |
|
82 | #Copy files that match search pattern | |
|
82 | # Copy files that match search pattern | |
|
83 | 83 | for matching_filename in glob.glob(filename): |
|
84 | 84 | |
|
85 | #Make sure folder exists. | |
|
85 | # Make sure folder exists. | |
|
86 | 86 | dest = os.path.join(self.build_directory, filename) |
|
87 | 87 | path = os.path.dirname(dest) |
|
88 | 88 | if not os.path.isdir(path): |
|
89 | 89 | os.makedirs(path) |
|
90 | 90 | |
|
91 | #Copy if destination is different. | |
|
91 | # Copy if destination is different. | |
|
92 | 92 | if not os.path.normpath(dest) == os.path.normpath(matching_filename): |
|
93 | 93 | shutil.copyfile(matching_filename, dest) |
|
94 | 94 | |
|
95 | #Determine where to write conversion results. | |
|
95 | # Determine where to write conversion results. | |
|
96 | 96 | dest = notebook_name + '.' + output_extension |
|
97 | 97 | if self.build_directory: |
|
98 | 98 | dest = os.path.join(self.build_directory, dest) |
|
99 | 99 | |
|
100 | #Write conversion results. | |
|
100 | # Write conversion results. | |
|
101 | 101 | with io.open(dest, 'w') as f: |
|
102 | 102 | f.write(output) |
General Comments 0
You need to be logged in to leave comments.
Login now