Show More
@@ -1,8 +1,7 b'' | |||||
1 | #!/usr/bin/env python |
|
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 |
|
4 | Command-line interface for the NbConvert conversion utility. | |
5 | readme.rst for usage information |
|
|||
6 | """ |
|
5 | """ | |
7 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
8 | #Copyright (c) 2013, the IPython Development Team. |
|
7 | #Copyright (c) 2013, the IPython Development Team. | |
@@ -23,7 +22,7 b' import os' | |||||
23 | import glob |
|
22 | import glob | |
24 |
|
23 | |||
25 | #From IPython |
|
24 | #From IPython | |
26 | from IPython.core.application import BaseIPythonApplication |
|
25 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags | |
27 | from IPython.config.application import catch_config_error |
|
26 | from IPython.config.application import catch_config_error | |
28 | from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type |
|
27 | from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type | |
29 | from IPython.utils.importstring import import_item |
|
28 | from IPython.utils.importstring import import_item | |
@@ -37,10 +36,37 b' from .utils.base import NbConvertBase' | |||||
37 | #Classes and functions |
|
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 | class NbConvertApp(BaseIPythonApplication): |
|
57 | class NbConvertApp(BaseIPythonApplication): | |
41 | """Application used to convert to and from notebook file type (*.ipynb)""" |
|
58 | """Application used to convert to and from notebook file type (*.ipynb)""" | |
42 |
|
59 | |||
43 | name = 'ipython-nbconvert' |
|
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 | description = Unicode( |
|
71 | description = Unicode( | |
46 | u"""This application is used to convert notebook files (*.ipynb). |
|
72 | u"""This application is used to convert notebook files (*.ipynb). | |
@@ -48,17 +74,13 b' class NbConvertApp(BaseIPythonApplication):' | |||||
48 | current directory.""") |
|
74 | current directory.""") | |
49 |
|
75 | |||
50 | examples = Unicode(u""" |
|
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 | Multiple notebooks can be given at the command line in a couple of |
|
77 | Multiple notebooks can be given at the command line in a couple of | |
55 | different ways: |
|
78 | different ways: | |
56 |
|
79 | |||
57 | > ipython nbconvert notebook*.ipynb |
|
80 | > ipython nbconvert notebook*.ipynb | |
58 | > ipython nbconvert notebook1.ipynb notebook2.ipynb |
|
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 | #Writer specific variables |
|
84 | #Writer specific variables | |
63 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', |
|
85 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', | |
64 | help="""Instance of the writer class used to write the |
|
86 | help="""Instance of the writer class used to write the | |
@@ -86,47 +108,29 b' class NbConvertApp(BaseIPythonApplication):' | |||||
86 | notebooks = List([], config=True, help="""List of notebooks to convert. |
|
108 | notebooks = List([], config=True, help="""List of notebooks to convert. | |
87 | Search patterns are supported.""") |
|
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 | @catch_config_error |
|
111 | @catch_config_error | |
95 | def initialize(self, argv=None): |
|
112 | def initialize(self, argv=None): | |
96 | self.aliases.update(self.nbconvert_aliases) |
|
|||
97 |
|
||||
98 | super(NbConvertApp, self).initialize(argv) |
|
113 | super(NbConvertApp, self).initialize(argv) | |
99 |
|
114 | self.init_notebooks() | ||
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) |
|
|||
107 | self.init_writer() |
|
115 | self.init_writer() | |
108 |
|
116 | |||
109 |
|
117 | def init_notebooks(self): | ||
110 | def init_config(self, extra_args): |
|
|||
111 | """ |
|
118 | """ | |
112 | Add notebooks to the config if needed. Glob each notebook to replace |
|
119 | Add notebooks to the config if needed. Glob each notebook to replace | |
113 | notebook patterns with filenames. |
|
120 | notebook patterns with filenames. | |
114 | """ |
|
121 | """ | |
115 |
|
122 | |||
116 | #Get any additional notebook patterns from the commandline |
|
123 | #Get any additional notebook patterns from the commandline | |
117 | if len(extra_args) > 0: |
|
124 | patterns = self.notebooks + self.extra_args | |
118 | for pattern in extra_args: |
|
|||
119 | self.notebooks.append(pattern) |
|
|||
120 |
|
125 | |||
121 | #Use glob to replace all the notebook patterns with filenames. |
|
126 | #Use glob to replace all the notebook patterns with filenames. | |
122 | filenames = [] |
|
127 | filenames = [] | |
123 |
for pattern in |
|
128 | for pattern in patterns: | |
124 | for filename in glob.glob(pattern): |
|
129 | for filename in glob.glob(pattern): | |
125 | if not filename in filenames: |
|
130 | if not filename in filenames: | |
126 | filenames.append(filename) |
|
131 | filenames.append(filename) | |
127 | self.notebooks = filenames |
|
132 | self.notebooks = filenames | |
128 |
|
133 | |||
129 |
|
||||
130 | def init_writer(self): |
|
134 | def init_writer(self): | |
131 | """ |
|
135 | """ | |
132 | Initialize the writer (which is stateless) |
|
136 | Initialize the writer (which is stateless) | |
@@ -134,15 +138,13 b' class NbConvertApp(BaseIPythonApplication):' | |||||
134 | self._writer_class_changed(None, self.writer_class, self.writer_class) |
|
138 | self._writer_class_changed(None, self.writer_class, self.writer_class) | |
135 | self.writer = self.writer_factory(parent=self) |
|
139 | self.writer = self.writer_factory(parent=self) | |
136 |
|
140 | |||
137 |
|
141 | def start(self): | ||
138 | def start(self, argv=None): |
|
|||
139 | """ |
|
142 | """ | |
140 |
Ran after initi |
|
143 | Ran after initialization completed | |
141 | """ |
|
144 | """ | |
142 | super(NbConvertApp, self).start() |
|
145 | super(NbConvertApp, self).start() | |
143 | self.convert_notebooks() |
|
146 | self.convert_notebooks() | |
144 |
|
147 | |||
145 |
|
||||
146 | def convert_notebooks(self): |
|
148 | def convert_notebooks(self): | |
147 | """ |
|
149 | """ | |
148 | Convert the notebooks in the self.notebook traitlet |
|
150 | Convert the notebooks in the self.notebook traitlet |
General Comments 0
You need to be logged in to leave comments.
Login now