##// END OF EJS Templates
add stdout flag and description in help
Matthias BUSSONNIER -
Show More
@@ -1,7 +1,8 b''
1 1 c = get_config()
2 2
3 3 c.ConverterTemplate.extract_figures=True
4 4 c.ConverterTemplate.template_file='latex_base'
5 5 c.ConverterTemplate.tex_environement=True
6 6
7 7 c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'}
8 c.ExtractFigureTransformer.enabled=True
@@ -1,94 +1,133 b''
1 1 #!/usr/bin/env python
2 """
3 ================================================================================
4
5 |,---. | | , .| |
6 ||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|---
7 || | || | || || | | \ || || | || | \ / |---'| |
8 `` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---'
9 `---'
10 ================================================================================
11
12 Highly experimental for now
13
14 """
2 15 #-----------------------------------------------------------------------------
3 16 # Imports
4 17 #-----------------------------------------------------------------------------
5 18 from __future__ import print_function
6 19 import sys
7 20 import io
8 21 import os
9 22
10 23 from converters.template import *
11 24 from converters.template import ConverterTemplate
12 25 from converters.html import ConverterHTML
13 26 # From IPython
14 27
15 28 # All the stuff needed for the configurable things
16 29 from IPython.config.application import Application
17 30 from IPython.config.loader import ConfigFileNotFound
18 31 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum
19 32
20 33 from converters.transformers import (ConfigurableTransformers,Foobar,ExtractFigureTransformer)
21 34
22 35
23 36 class NbconvertApp(Application):
24 37
38 stdout = Bool(True, config=True)
39 write = Bool(False, config=True)
40
41 aliases = {
42 'stdout':'NbconvertApp.stdout',
43 'write':'NbconvertApp.write',
44 }
45
46 flags= {}
47 flags['no-stdout'] = (
48 {'NbconvertApp' : {'stdout' : False}},
49 """the doc for this flag
50
51 """
52 )
25 53
26 54 def __init__(self, **kwargs):
27 55 super(NbconvertApp, self).__init__(**kwargs)
28 56 self.classes.insert(0,ConverterTemplate)
29 57 # register class here to have help with help all
30 58 self.classes.insert(0,ExtractFigureTransformer)
31 59 self.classes.insert(0,Foobar)
32 60 # ensure those are registerd
33 61
34 62 def load_config_file(self, profile_name):
35 63 try:
36 64 Application.load_config_file(
37 65 self,
38 66 profile_name+'.nbcv',
39 67 path=[os.path.join(os.getcwdu(),'profile')]
40 68 )
41 69 except ConfigFileNotFound:
42 70 self.log.warn("Config file for profile '%s' not found, giving up ",profile_name)
43 71 exit(1)
44 72
45 73
46 74 def initialize(self, argv=None):
47 75 self.parse_command_line(argv)
48 76 cl_config = self.config
49 77 profile_file = sys.argv[1]
50 78 self.load_config_file(profile_file)
51 79 self.update_config(cl_config)
52 80
53 81
54 82
55 83 def run(self):
56 84 """Convert a notebook to html in one step"""
57 85 template_file = (self.extra_args or [None])[0]
58 86 ipynb_file = (self.extra_args or [None])[1]
59 87
60 88 template_file = sys.argv[1]
61 89
62 90 C = ConverterTemplate(tplfile=sys.argv[1],
63 91 config=self.config)
64 92 C.read(ipynb_file)
65 93
66 94 output,resources = C.convert()
67
95 if self.stdout :
68 96 print(output.encode('utf-8'))
69 97
98 out_root = ipynb_file[:-6].replace('.','_')
99
70 100 keys = resources.keys()
71 101 if keys :
102 if self.write :
103 os.mkdir(out_root)
104 for key in keys:
105 if self.write:
106 with io.open(os.path.join(out_root+key),'w') as f:
107 print(' writing to ',os.path.join(out_root+key))
108 f.write(resources[key])
109 if self.stdout:
72 110 print('''
73 111 ====================== Keys in Resources ==================================
74 112 ''')
75 113 print(resources.keys())
76 114 print("""
77 115 ===========================================================================
78 116 you are responsible from writing those data do a file in the right place if
79 117 they need to be.
80 118 ===========================================================================
81 119 """)
82 120
83 121 def main():
84 122 """Convert a notebook to html in one step"""
85 123 app = NbconvertApp.instance()
124 app.description = __doc__
86 125 app.initialize()
87 126 app.start()
88 127 app.run()
89 128 #-----------------------------------------------------------------------------
90 129 # Script main
91 130 #-----------------------------------------------------------------------------
92 131
93 132 if __name__ == '__main__':
94 133 main()
General Comments 0
You need to be logged in to leave comments. Login now