Show More
@@ -1,8 +1,7 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 |
"""NBConvert is a utility for conversion of |
|
|
2 | """NBConvert is a utility for conversion of .ipynb files. | |
|
3 | 3 | |
|
4 |
Commandline interface for the N |
|
|
5 | readme.rst for usage information | |
|
4 | Command-line interface for the NbConvert conversion utility. | |
|
6 | 5 | """ |
|
7 | 6 | #----------------------------------------------------------------------------- |
|
8 | 7 | #Copyright (c) 2013, the IPython Development Team. |
@@ -23,7 +22,7 b' import os' | |||
|
23 | 22 | import glob |
|
24 | 23 | |
|
25 | 24 | #From IPython |
|
26 | from IPython.core.application import BaseIPythonApplication | |
|
25 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags | |
|
27 | 26 | from IPython.config.application import catch_config_error |
|
28 | 27 | from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type |
|
29 | 28 | from IPython.utils.importstring import import_item |
@@ -37,10 +36,37 b' from .utils.base import NbConvertBase' | |||
|
37 | 36 | #Classes and functions |
|
38 | 37 | #----------------------------------------------------------------------------- |
|
39 | 38 | |
|
39 | nbconvert_aliases = {} | |
|
40 | nbconvert_aliases.update(base_aliases) | |
|
41 | nbconvert_aliases.update({ | |
|
42 | 'format' : 'NbConvertApp.export_format', | |
|
43 | 'notebooks' : 'NbConvertApp.notebooks', | |
|
44 | 'writer' : 'NbConvertApp.writer_class', | |
|
45 | }) | |
|
46 | ||
|
47 | nbconvert_flags = {} | |
|
48 | nbconvert_flags.update(base_flags) | |
|
49 | nbconvert_flags.update({ | |
|
50 | 'stdout' : ( | |
|
51 | {'NbConvertApp' : {'writer_class' : "StdoutWriter"}}, | |
|
52 | "Write notebook output to stdout instead of files." | |
|
53 | ) | |
|
54 | }) | |
|
55 | ||
|
56 | ||
|
40 | 57 | class NbConvertApp(BaseIPythonApplication): |
|
41 | 58 | """Application used to convert to and from notebook file type (*.ipynb)""" |
|
42 | 59 | |
|
43 | 60 | name = 'ipython-nbconvert' |
|
61 | aliases = nbconvert_aliases | |
|
62 | flags = nbconvert_flags | |
|
63 | ||
|
64 | def _classes_default(self): | |
|
65 | return [ | |
|
66 | Exporter, | |
|
67 | WriterBase, | |
|
68 | NbConvertBase, | |
|
69 | ] | |
|
44 | 70 | |
|
45 | 71 | description = Unicode( |
|
46 | 72 | u"""This application is used to convert notebook files (*.ipynb). |
@@ -48,17 +74,13 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
48 | 74 | current directory.""") |
|
49 | 75 | |
|
50 | 76 | examples = Unicode(u""" |
|
51 | Running `ipython nbconvert` will read the directory config file and then | |
|
52 | apply it to one or more notebooks. | |
|
53 | ||
|
54 | 77 | Multiple notebooks can be given at the command line in a couple of |
|
55 | 78 | different ways: |
|
56 | 79 | |
|
57 | 80 | > ipython nbconvert notebook*.ipynb |
|
58 | 81 | > ipython nbconvert notebook1.ipynb notebook2.ipynb |
|
59 | > ipython nbconvert # this will use the config file to fill in the notebooks | |
|
82 | > ipython nbconvert --format sphinx_howto notebook.ipynb | |
|
60 | 83 | """) |
|
61 | ||
|
62 | 84 | #Writer specific variables |
|
63 | 85 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', |
|
64 | 86 | help="""Instance of the writer class used to write the |
@@ -86,47 +108,29 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
86 | 108 | notebooks = List([], config=True, help="""List of notebooks to convert. |
|
87 | 109 | Search patterns are supported.""") |
|
88 | 110 | |
|
89 | nbconvert_aliases = {'format':'NbConvertApp.export_format', | |
|
90 | 'notebooks':'NbConvertApp.notebooks', | |
|
91 | 'writer':'NbConvertApp.writer_class'} | |
|
92 | ||
|
93 | ||
|
94 | 111 | @catch_config_error |
|
95 | 112 | def initialize(self, argv=None): |
|
96 | self.aliases.update(self.nbconvert_aliases) | |
|
97 | ||
|
98 | 113 | super(NbConvertApp, self).initialize(argv) |
|
99 | ||
|
100 | #Register class here to have help with help all | |
|
101 | self.classes.insert(0, Exporter) | |
|
102 | self.classes.insert(0, WriterBase) | |
|
103 | self.classes.insert(0, NbConvertBase) | |
|
104 | ||
|
105 | #Init | |
|
106 | self.init_config(self.extra_args) | |
|
114 | self.init_notebooks() | |
|
107 | 115 | self.init_writer() |
|
108 | 116 | |
|
109 | ||
|
110 | def init_config(self, extra_args): | |
|
117 | def init_notebooks(self): | |
|
111 | 118 | """ |
|
112 | 119 | Add notebooks to the config if needed. Glob each notebook to replace |
|
113 | 120 | notebook patterns with filenames. |
|
114 | 121 | """ |
|
115 | 122 | |
|
116 | 123 | #Get any additional notebook patterns from the commandline |
|
117 | if len(extra_args) > 0: | |
|
118 | for pattern in extra_args: | |
|
119 | self.notebooks.append(pattern) | |
|
124 | patterns = self.notebooks + self.extra_args | |
|
120 | 125 | |
|
121 | 126 | #Use glob to replace all the notebook patterns with filenames. |
|
122 | 127 | filenames = [] |
|
123 |
for pattern in |
|
|
128 | for pattern in patterns: | |
|
124 | 129 | for filename in glob.glob(pattern): |
|
125 | 130 | if not filename in filenames: |
|
126 | 131 | filenames.append(filename) |
|
127 | 132 | self.notebooks = filenames |
|
128 | 133 | |
|
129 | ||
|
130 | 134 | def init_writer(self): |
|
131 | 135 | """ |
|
132 | 136 | Initialize the writer (which is stateless) |
@@ -134,15 +138,13 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
134 | 138 | self._writer_class_changed(None, self.writer_class, self.writer_class) |
|
135 | 139 | self.writer = self.writer_factory(parent=self) |
|
136 | 140 | |
|
137 | ||
|
138 | def start(self, argv=None): | |
|
141 | def start(self): | |
|
139 | 142 | """ |
|
140 |
Ran after initi |
|
|
143 | Ran after initialization completed | |
|
141 | 144 | """ |
|
142 | 145 | super(NbConvertApp, self).start() |
|
143 | 146 | self.convert_notebooks() |
|
144 | 147 | |
|
145 | ||
|
146 | 148 | def convert_notebooks(self): |
|
147 | 149 | """ |
|
148 | 150 | Convert the notebooks in the self.notebook traitlet |
General Comments 0
You need to be logged in to leave comments.
Login now