runme.py
94 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
|
r9011 | #!/usr/bin/env python | ||
|
r9215 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r9014 | from __future__ import print_function | ||
|
r8997 | import sys | ||
|
r9003 | import io | ||
|
r9234 | import os | ||
|
r9215 | |||
|
r8994 | from converters.template import * | ||
|
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 | ||||
|
r9234 | from IPython.config.loader import ConfigFileNotFound | ||
|
r9215 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | ||
class NbconvertApp(Application): | ||||
def __init__(self, **kwargs): | ||||
super(NbconvertApp, self).__init__(**kwargs) | ||||
self.classes.insert(0,ConverterTemplate) | ||||
# ensure those are registerd | ||||
|
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) | ||||
|
r9215 | |||
def initialize(self, argv=None): | ||||
self.parse_command_line(argv) | ||||
cl_config = self.config | ||||
|
r9234 | profile_file = sys.argv[1] | ||
self.load_config_file(profile_file) | ||||
|
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] | ||||
|
r9212 | |||
|
r9215 | if template_file.startswith('latex'): | ||
tex_environement=True | ||||
else: | ||||
tex_environement=False | ||||
|
r9214 | |||
|
r9229 | C = ConverterTemplate(tplfile=sys.argv[1], | ||
config=self.config) | ||||
|
r9215 | C.read(ipynb_file) | ||
|
r9212 | |||
|
r9229 | output,resources = C.convert() | ||
|
r9212 | |||
|
r9215 | print(output.encode('utf-8')) | ||
|
r8997 | |||
|
r9229 | keys = resources.keys() | ||
if keys : | ||||
print(''' | ||||
====================== Keys in Resources ================================== | ||||
''') | ||||
print(resources.keys()) | ||||
print(""" | ||||
=========================================================================== | ||||
you are responsible from writing those data do a file in the right place if | ||||
they need to be. | ||||
=========================================================================== | ||||
""") | ||||
|
r9215 | def main(): | ||
"""Convert a notebook to html in one step""" | ||||
app = NbconvertApp.instance() | ||||
app.initialize() | ||||
app.start() | ||||
app.run() | ||||
#----------------------------------------------------------------------------- | ||||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
|
r9182 | |||
|
r9215 | if __name__ == '__main__': | ||
main() | ||||