runme.py
136 lines
| 4.4 KiB
| text/x-python
|
PythonLexer
Matthias BUSSONNIER
|
r9011 | #!/usr/bin/env python | ||
Matthias BUSSONNIER
|
r9308 | """ | ||
================================================================================ | ||||
|,---. | | , .| | | ||||
||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|--- | ||||
|| | || | || || | | \ || || | || | \ / |---'| | | ||||
`` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---' | ||||
`---' | ||||
================================================================================ | ||||
Highly experimental for now | ||||
""" | ||||
Matthias BUSSONNIER
|
r9215 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Matthias BUSSONNIER
|
r9014 | from __future__ import print_function | ||
Matthias BUSSONNIER
|
r8997 | import sys | ||
Matthias BUSSONNIER
|
r9003 | import io | ||
Matthias BUSSONNIER
|
r9234 | import os | ||
Matthias BUSSONNIER
|
r9215 | |||
Matthias BUSSONNIER
|
r8994 | from converters.template import * | ||
Matthias BUSSONNIER
|
r9215 | from converters.template import ConverterTemplate | ||
from converters.html import ConverterHTML | ||||
# From IPython | ||||
# All the stuff needed for the configurable things | ||||
from IPython.config.application import Application | ||||
Matthias BUSSONNIER
|
r9234 | from IPython.config.loader import ConfigFileNotFound | ||
Matthias BUSSONNIER
|
r9215 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | ||
Matthias BUSSONNIER
|
r9307 | from converters.transformers import (ConfigurableTransformers,Foobar,ExtractFigureTransformer) | ||
Matthias BUSSONNIER
|
r9215 | |||
class NbconvertApp(Application): | ||||
Matthias BUSSONNIER
|
r9308 | stdout = Bool(True, config=True) | ||
write = Bool(False, config=True) | ||||
Matthias BUSSONNIER
|
r9328 | fileext = Unicode('txt', config=True) | ||
Matthias BUSSONNIER
|
r9308 | aliases = { | ||
'stdout':'NbconvertApp.stdout', | ||||
'write':'NbconvertApp.write', | ||||
} | ||||
flags= {} | ||||
flags['no-stdout'] = ( | ||||
{'NbconvertApp' : {'stdout' : False}}, | ||||
"""the doc for this flag | ||||
""" | ||||
) | ||||
Matthias BUSSONNIER
|
r9215 | |||
def __init__(self, **kwargs): | ||||
super(NbconvertApp, self).__init__(**kwargs) | ||||
self.classes.insert(0,ConverterTemplate) | ||||
Matthias BUSSONNIER
|
r9307 | # register class here to have help with help all | ||
self.classes.insert(0,ExtractFigureTransformer) | ||||
self.classes.insert(0,Foobar) | ||||
Matthias BUSSONNIER
|
r9215 | # ensure those are registerd | ||
Matthias BUSSONNIER
|
r9234 | def load_config_file(self, profile_name): | ||
try: | ||||
Application.load_config_file( | ||||
self, | ||||
profile_name+'.nbcv', | ||||
path=[os.path.join(os.getcwdu(),'profile')] | ||||
) | ||||
except ConfigFileNotFound: | ||||
self.log.warn("Config file for profile '%s' not found, giving up ",profile_name) | ||||
exit(1) | ||||
Matthias BUSSONNIER
|
r9215 | |||
def initialize(self, argv=None): | ||||
self.parse_command_line(argv) | ||||
cl_config = self.config | ||||
Matthias BUSSONNIER
|
r9234 | profile_file = sys.argv[1] | ||
self.load_config_file(profile_file) | ||||
Matthias BUSSONNIER
|
r9215 | self.update_config(cl_config) | ||
def run(self): | ||||
"""Convert a notebook to html in one step""" | ||||
template_file = (self.extra_args or [None])[0] | ||||
ipynb_file = (self.extra_args or [None])[1] | ||||
template_file = sys.argv[1] | ||||
Matthias BUSSONNIER
|
r9212 | |||
Matthias BUSSONNIER
|
r9312 | C = ConverterTemplate(config=self.config) | ||
Matthias BUSSONNIER
|
r9212 | |||
Matthias BUSSONNIER
|
r9332 | output,resources = C.from_filename(ipynb_file) | ||
Matthias BUSSONNIER
|
r9308 | if self.stdout : | ||
print(output.encode('utf-8')) | ||||
Matthias BUSSONNIER
|
r9212 | |||
Matthias BUSSONNIER
|
r9327 | out_root = ipynb_file[:-6].replace('.','_').replace(' ','_') | ||
Matthias BUSSONNIER
|
r8997 | |||
Matthias BUSSONNIER
|
r9331 | keys = resources.get('figures',{}).keys() | ||
Matthias BUSSONNIER
|
r9327 | if self.write : | ||
Matthias BUSSONNIER
|
r9328 | with io.open(os.path.join(out_root+'.'+self.fileext),'w') as f: | ||
Matthias BUSSONNIER
|
r9327 | f.write(output) | ||
Matthias BUSSONNIER
|
r9229 | if keys : | ||
Matthias BUSSONNIER
|
r9327 | if self.write and not os.path.exists(out_root+'_files'): | ||
os.mkdir(out_root+'_files') | ||||
Matthias BUSSONNIER
|
r9308 | for key in keys: | ||
if self.write: | ||||
Matthias BUSSONNIER
|
r9314 | with io.open(os.path.join(out_root+'_files',key),'wb') as f: | ||
Matthias BUSSONNIER
|
r9312 | print(' writing to ',os.path.join(out_root,key)) | ||
Matthias BUSSONNIER
|
r9331 | f.write(resources['figures'][key]) | ||
Matthias BUSSONNIER
|
r9308 | if self.stdout: | ||
print(''' | ||||
Matthias BUSSONNIER
|
r9229 | ====================== Keys in Resources ================================== | ||
''') | ||||
Matthias BUSSONNIER
|
r9331 | print(resources['figures'].keys()) | ||
Matthias BUSSONNIER
|
r9308 | print(""" | ||
Matthias BUSSONNIER
|
r9229 | =========================================================================== | ||
you are responsible from writing those data do a file in the right place if | ||||
they need to be. | ||||
=========================================================================== | ||||
""") | ||||
Matthias BUSSONNIER
|
r9215 | def main(): | ||
"""Convert a notebook to html in one step""" | ||||
app = NbconvertApp.instance() | ||||
Matthias BUSSONNIER
|
r9308 | app.description = __doc__ | ||
Matthias BUSSONNIER
|
r9215 | app.initialize() | ||
app.start() | ||||
app.run() | ||||
#----------------------------------------------------------------------------- | ||||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
Matthias BUSSONNIER
|
r9182 | |||
Matthias BUSSONNIER
|
r9215 | if __name__ == '__main__': | ||
main() | ||||