forked from slojo404/nbconvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbconvert2.py
executable file
·161 lines (129 loc) · 5.43 KB
/
nbconvert2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
"""
================================================================================
|,---. | | , .| |
||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|---
|| | || | || || | | \ || || | || | \ / |---'| |
`` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---'
`---'
================================================================================
Highly experimental for now
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
import sys
import io
import os
from converters.template import ConverterTemplate
# From IPython
# All the stuff needed for the configurable things
from IPython.config.application import Application
from IPython.config.loader import ConfigFileNotFound
from IPython.utils.traitlets import Unicode, Bool
from converters.transformers import (ExtractFigureTransformer)
from converters.config import GlobalConfigurable
class NbconvertApp(Application):
"""A basic application to convert ipynb files
"""
stdout = Bool(True, config=True,
help="""
Wether to print the converted ipynb file to stdout
use full do diff files without actually writing a new file
""")
write = Bool(False, config=True,
help="""Shoudl the converted notebook file be written to disk
along with potential extracted resources.
"""
)
fileext = Unicode('txt', config=True,
help="""Extension of the file that should be written to disk"""
)
aliases = {
'stdout':'NbconvertApp.stdout',
'write':'NbconvertApp.write',
}
flags = {}
flags['no-stdout'] = (
{'NbconvertApp' : {'stdout' : False}},
"""Do not print converted file to stdout, equivalent to --stdout=False
"""
)
def __init__(self, **kwargs):
super(NbconvertApp, self).__init__(**kwargs)
self.classes.insert(0, ConverterTemplate)
# register class here to have help with help all
self.classes.insert(0, ExtractFigureTransformer)
self.classes.insert(0, GlobalConfigurable)
def load_config_file(self, profile_name):
"""load a config file from the config file dir
profile_name : {string} name of the profile file to load without file extension.
"""
try:
Application.load_config_file(
self,
profile_name+'.nbcv',
path=[os.path.join(os.getcwdu(), 'profile')]
)
return True
except ConfigFileNotFound:
self.log.warn("Config file for profile '%s' not found, giving up ", profile_name)
return False
def initialize(self, argv=None):
"""parse command line and load config"""
self.parse_command_line(argv)
cl_config = self.config
profile_file = argv[1]
if not self.load_config_file(profile_file):
exit(1)
self.update_config(cl_config)
def run(self):
"""Convert a notebook in one step"""
ipynb_file = (self.extra_args or [None])[2]
# If you are writting a custom transformer, append it to the dictionary
# below.
userpreprocessors = {}
# Create the converter
C = ConverterTemplate(config=self.config, preprocessors=userpreprocessors)
output, resources = C.from_filename(ipynb_file)
if self.stdout :
print(output.encode('utf-8'))
out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_')
if self.write :
with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f:
f.write(output)
binkeys = resources.get('figures', {}).get('binary',{}).keys()
textkeys = resources.get('figures', {}).get('text',{}).keys()
if binkeys or textkeys :
if self.write:
files_dir = out_root+'_files'
if not os.path.exists(out_root+'_files'):
os.mkdir(files_dir)
for key in binkeys:
with io.open(os.path.join(files_dir, key), 'wb') as f:
f.write(resources['figures']['binary'][key])
for key in textkeys:
with io.open(os.path.join(files_dir, key), 'w') as f:
f.write(resources['figures']['text'][key])
elif self.stdout:
print('''====================== Keys in Resources ==================================''')
print(resources['figures'].keys())
print("""
===========================================================================
you are responsible from writing those data do a file in the right place if
they need to be.
===========================================================================
""")
def main():
"""Convert a notebook to html in one step"""
app = NbconvertApp.instance()
app.description = __doc__
app.initialize(argv=sys.argv)
app.start()
app.run()
#-----------------------------------------------------------------------------
# Script main
#-----------------------------------------------------------------------------
if __name__ == '__main__':
main()