Show More
@@ -1,117 +1,123 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """Convert IPython notebooks to other formats, such as ReST, and HTML. |
|
2 | """Convert IPython notebooks to other formats, such as ReST, and HTML. | |
3 |
|
3 | |||
4 | Example: |
|
4 | Example: | |
5 | ./nbconvert.py --format rst file.ipynb |
|
5 | ./nbconvert.py --format rst file.ipynb | |
6 |
|
6 | |||
7 | Produces 'file.rst', along with auto-generated figure files |
|
7 | Produces 'file.rst', along with auto-generated figure files | |
8 | called nb_figure_NN.png. |
|
8 | called nb_figure_NN.png. | |
9 | """ |
|
9 | """ | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | from __future__ import print_function |
|
13 | from __future__ import print_function | |
14 |
|
14 | |||
15 | # From IPython |
|
15 | # From IPython | |
16 | from IPython.external import argparse |
|
16 | from IPython.external import argparse | |
17 |
|
17 | |||
18 | # All the stuff needed for the configurable things |
|
18 | # All the stuff needed for the configurable things | |
19 | from IPython.config.application import Application, catch_config_error |
|
19 | from IPython.config.application import Application, catch_config_error | |
20 | from IPython.config.configurable import Configurable, SingletonConfigurable |
|
20 | from IPython.config.configurable import Configurable, SingletonConfigurable | |
21 | from IPython.config.loader import Config, ConfigFileNotFound |
|
21 | from IPython.config.loader import Config, ConfigFileNotFound | |
22 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum |
|
22 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | # local |
|
25 | # local | |
26 | from converters.html import ConverterHTML |
|
26 | from converters.html import ConverterHTML | |
27 | from converters.markdown import ConverterMarkdown |
|
27 | from converters.markdown import ConverterMarkdown | |
28 | from converters.bloggerhtml import ConverterBloggerHTML |
|
28 | from converters.bloggerhtml import ConverterBloggerHTML | |
29 | from converters.rst import ConverterRST |
|
29 | from converters.rst import ConverterRST | |
30 | from converters.latex import ConverterLaTeX |
|
30 | from converters.latex import ConverterLaTeX | |
31 | from converters.python import ConverterPy |
|
31 | from converters.python import ConverterPy | |
32 | from converters.base import Converter |
|
32 | from converters.base import Converter | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | # When adding a new format, make sure to add it to the `converters` |
|
35 | # When adding a new format, make sure to add it to the `converters` | |
36 | # dictionary below. This is used to create the list of known formats, |
|
36 | # dictionary below. This is used to create the list of known formats, | |
37 | # which gets printed in case an unknown format is encounteres, as well |
|
37 | # which gets printed in case an unknown format is encounteres, as well | |
38 | # as in the help |
|
38 | # as in the help | |
39 |
|
39 | |||
40 | converters = { |
|
40 | converters = { | |
41 | 'rst': ConverterRST, |
|
41 | 'rst': ConverterRST, | |
42 | 'markdown': ConverterMarkdown, |
|
42 | 'markdown': ConverterMarkdown, | |
43 | 'html': ConverterHTML, |
|
43 | 'html': ConverterHTML, | |
44 | 'blogger-html': ConverterBloggerHTML, |
|
44 | 'blogger-html': ConverterBloggerHTML, | |
45 | 'latex': ConverterLaTeX, |
|
45 | 'latex': ConverterLaTeX, | |
46 | 'py': ConverterPy, |
|
46 | 'py': ConverterPy, | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | default_format = 'rst' |
|
49 | default_format = 'rst' | |
50 |
|
50 | |||
51 | # Extract the list of known formats and mark the first format as the default. |
|
51 | # Extract the list of known formats and mark the first format as the default. | |
52 | known_formats = ', '.join([key + " (default)" if key == default_format else key |
|
52 | known_formats = ', '.join([key + " (default)" if key == default_format else key | |
53 | for key in converters]) |
|
53 | for key in converters]) | |
54 |
|
54 | |||
55 | class NbconvertApp(Application): |
|
55 | class NbconvertApp(Application): | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | fmt = CaselessStrEnum(converters.keys(), |
|
58 | fmt = CaselessStrEnum(converters.keys(), | |
59 | default_value='rst', |
|
59 | default_value='rst', | |
60 | config=True, |
|
60 | config=True, | |
61 | help="Supported conversion format") |
|
61 | help="Supported conversion format") | |
62 |
|
62 | |||
63 | exclude = List( [], |
|
63 | exclude = List( [], | |
64 | config=True, |
|
64 | config=True, | |
65 | help = 'list of cells to exclude while converting') |
|
65 | help = 'list of cells to exclude while converting') | |
66 |
|
66 | |||
67 | aliases = { |
|
67 | aliases = { | |
68 | 'format':'NbconvertApp.fmt', |
|
68 | 'format':'NbconvertApp.fmt', | |
69 | 'exclude':'NbconvertApp.exclude', |
|
69 | 'exclude':'NbconvertApp.exclude', | |
70 | 'highlight':'Converter.highlight_source', |
|
70 | 'highlight':'Converter.highlight_source', | |
71 | 'preamble':'Converter.preamble', |
|
71 | 'preamble':'Converter.preamble', | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | def __init__(self, **kwargs): |
|
74 | def __init__(self, **kwargs): | |
75 | super(NbconvertApp, self).__init__(**kwargs) |
|
75 | super(NbconvertApp, self).__init__(**kwargs) | |
76 | # ensure those are registerd |
|
76 | # ensure those are registerd | |
77 | self.classes.insert(0,Converter) |
|
77 | self.classes.insert(0,Converter) | |
78 | self.classes.insert(0,ConverterRST) |
|
78 | self.classes.insert(0,ConverterRST) | |
79 | self.classes.insert(0,ConverterMarkdown) |
|
79 | self.classes.insert(0,ConverterMarkdown) | |
80 | self.classes.insert(0,ConverterBloggerHTML) |
|
80 | self.classes.insert(0,ConverterBloggerHTML) | |
81 | self.classes.insert(0,ConverterLaTeX) |
|
81 | self.classes.insert(0,ConverterLaTeX) | |
82 | self.classes.insert(0,ConverterPy) |
|
82 | self.classes.insert(0,ConverterPy) | |
83 |
|
83 | |||
84 | def initialize(self, argv=None): |
|
84 | def initialize(self, argv=None): | |
85 | self.parse_command_line(argv) |
|
85 | self.parse_command_line(argv) | |
86 | cl_config = self.config |
|
86 | cl_config = self.config | |
87 | self.update_config(cl_config) |
|
87 | self.update_config(cl_config) | |
88 |
|
88 | |||
89 | def run(self): |
|
89 | def run(self): | |
90 | """Convert a notebook to html in one step""" |
|
90 | """Convert a notebook to html in one step""" | |
91 | ConverterClass = converters[self.fmt] |
|
91 | ConverterClass = converters[self.fmt] | |
92 | infile = (self.extra_args or [None])[0] |
|
92 | infile = (self.extra_args or [None])[0] | |
93 | converter = ConverterClass(infile=infile, config=self.config) |
|
93 | converter = ConverterClass(infile=infile, config=self.config) | |
94 | converter.render() |
|
94 | converter.render() | |
95 |
|
95 | |||
96 | def main(): |
|
96 | def main(): | |
97 | """Convert a notebook to html in one step""" |
|
97 | """Convert a notebook to html in one step""" | |
98 | app = NbconvertApp.instance() |
|
98 | app = NbconvertApp.instance() | |
99 | app.description = __doc__ |
|
99 | app.description = __doc__ | |
|
100 | print(""" | |||
|
101 | ====================================================== | |||
|
102 | Warning, we are deprecating this version of nbconvert, | |||
|
103 | please consider using the new version. | |||
|
104 | ====================================================== | |||
|
105 | """) | |||
100 | app.initialize() |
|
106 | app.initialize() | |
101 | app.start() |
|
107 | app.start() | |
102 | app.run() |
|
108 | app.run() | |
103 | #----------------------------------------------------------------------------- |
|
109 | #----------------------------------------------------------------------------- | |
104 | # Script main |
|
110 | # Script main | |
105 | #----------------------------------------------------------------------------- |
|
111 | #----------------------------------------------------------------------------- | |
106 |
|
112 | |||
107 | if __name__ == '__main__': |
|
113 | if __name__ == '__main__': | |
108 | # TODO: consider passing file like object around, rather than filenames |
|
114 | # TODO: consider passing file like object around, rather than filenames | |
109 | # would allow us to process stdin, or even http streams |
|
115 | # would allow us to process stdin, or even http streams | |
110 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), |
|
116 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | |
111 | # default=sys.stdin) |
|
117 | # default=sys.stdin) | |
112 |
|
118 | |||
113 | #parser.add_argument('-e', '--exclude', default='', |
|
119 | #parser.add_argument('-e', '--exclude', default='', | |
114 | # help='Comma-separated list of cells to exclude') |
|
120 | # help='Comma-separated list of cells to exclude') | |
115 | #exclude_cells = [s.strip() for s in args.exclude.split(',')] |
|
121 | #exclude_cells = [s.strip() for s in args.exclude.split(',')] | |
116 |
|
122 | |||
117 | main() |
|
123 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now