##// END OF EJS Templates
put figures in subkey
put figures in subkey

File last commit:

r9331:d63467b7
r9331:d63467b7
Show More
runme.py
137 lines | 4.4 KiB | text/x-python | PythonLexer
Matthias BUSSONNIER
basic test for converter template
r9011 #!/usr/bin/env python
Matthias BUSSONNIER
add stdout flag and description in help
r9308 """
================================================================================
|,---. | | , .| |
||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|---
|| | || | || || | | \ || || | || | \ / |---'| |
`` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---'
`---'
================================================================================
Highly experimental for now
"""
Matthias BUSSONNIER
rename runme2 to runme
r9215 #-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
fix utf8
r9014 from __future__ import print_function
Matthias BUSSONNIER
null template
r8997 import sys
Matthias BUSSONNIER
handle unicode output
r9003 import io
Matthias BUSSONNIER
use configuration file to do nice stuff
r9234 import os
Matthias BUSSONNIER
rename runme2 to runme
r9215
Matthias BUSSONNIER
starting templates
r8994 from converters.template import *
Matthias BUSSONNIER
rename runme2 to runme
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
use configuration file to do nice stuff
r9234 from IPython.config.loader import ConfigFileNotFound
Matthias BUSSONNIER
rename runme2 to runme
r9215 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum
Matthias BUSSONNIER
create configurable preprocessors
r9307 from converters.transformers import (ConfigurableTransformers,Foobar,ExtractFigureTransformer)
Matthias BUSSONNIER
rename runme2 to runme
r9215
class NbconvertApp(Application):
Matthias BUSSONNIER
add stdout flag and description in help
r9308 stdout = Bool(True, config=True)
write = Bool(False, config=True)
Matthias BUSSONNIER
fix fileext and html multidisplay
r9328 fileext = Unicode('txt', config=True)
Matthias BUSSONNIER
add stdout flag and description in help
r9308 aliases = {
'stdout':'NbconvertApp.stdout',
'write':'NbconvertApp.write',
}
flags= {}
flags['no-stdout'] = (
{'NbconvertApp' : {'stdout' : False}},
"""the doc for this flag
"""
)
Matthias BUSSONNIER
rename runme2 to runme
r9215
def __init__(self, **kwargs):
super(NbconvertApp, self).__init__(**kwargs)
self.classes.insert(0,ConverterTemplate)
Matthias BUSSONNIER
create configurable preprocessors
r9307 # register class here to have help with help all
self.classes.insert(0,ExtractFigureTransformer)
self.classes.insert(0,Foobar)
Matthias BUSSONNIER
rename runme2 to runme
r9215 # ensure those are registerd
Matthias BUSSONNIER
use configuration file to do nice stuff
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
rename runme2 to runme
r9215
def initialize(self, argv=None):
self.parse_command_line(argv)
cl_config = self.config
Matthias BUSSONNIER
use configuration file to do nice stuff
r9234 profile_file = sys.argv[1]
self.load_config_file(profile_file)
Matthias BUSSONNIER
rename runme2 to runme
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
multiple env
r9212
Matthias BUSSONNIER
fix soem figure extraction logic
r9312 C = ConverterTemplate(config=self.config)
Matthias BUSSONNIER
rename runme2 to runme
r9215 C.read(ipynb_file)
Matthias BUSSONNIER
multiple env
r9212
Matthias BUSSONNIER
flag for extracting figure
r9229 output,resources = C.convert()
Matthias BUSSONNIER
add stdout flag and description in help
r9308 if self.stdout :
print(output.encode('utf-8'))
Matthias BUSSONNIER
multiple env
r9212
Matthias BUSSONNIER
partial tex fixes
r9327 out_root = ipynb_file[:-6].replace('.','_').replace(' ','_')
Matthias BUSSONNIER
null template
r8997
Matthias BUSSONNIER
put figures in subkey
r9331 keys = resources.get('figures',{}).keys()
Matthias BUSSONNIER
partial tex fixes
r9327 if self.write :
Matthias BUSSONNIER
fix fileext and html multidisplay
r9328 with io.open(os.path.join(out_root+'.'+self.fileext),'w') as f:
Matthias BUSSONNIER
partial tex fixes
r9327 f.write(output)
Matthias BUSSONNIER
flag for extracting figure
r9229 if keys :
Matthias BUSSONNIER
partial tex fixes
r9327 if self.write and not os.path.exists(out_root+'_files'):
os.mkdir(out_root+'_files')
Matthias BUSSONNIER
add stdout flag and description in help
r9308 for key in keys:
if self.write:
Matthias BUSSONNIER
unified figure folder
r9314 with io.open(os.path.join(out_root+'_files',key),'wb') as f:
Matthias BUSSONNIER
fix soem figure extraction logic
r9312 print(' writing to ',os.path.join(out_root,key))
Matthias BUSSONNIER
put figures in subkey
r9331 f.write(resources['figures'][key])
Matthias BUSSONNIER
add stdout flag and description in help
r9308 if self.stdout:
print('''
Matthias BUSSONNIER
flag for extracting figure
r9229 ====================== Keys in Resources ==================================
''')
Matthias BUSSONNIER
put figures in subkey
r9331 print(resources['figures'].keys())
Matthias BUSSONNIER
add stdout flag and description in help
r9308 print("""
Matthias BUSSONNIER
flag for extracting figure
r9229 ===========================================================================
you are responsible from writing those data do a file in the right place if
they need to be.
===========================================================================
""")
Matthias BUSSONNIER
rename runme2 to runme
r9215 def main():
"""Convert a notebook to html in one step"""
app = NbconvertApp.instance()
Matthias BUSSONNIER
add stdout flag and description in help
r9308 app.description = __doc__
Matthias BUSSONNIER
rename runme2 to runme
r9215 app.initialize()
app.start()
app.run()
#-----------------------------------------------------------------------------
# Script main
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
Start to think on api...
r9182
Matthias BUSSONNIER
rename runme2 to runme
r9215 if __name__ == '__main__':
main()