From 896aaed37015731866e6dbbfd999c81c7d9aa97f 2013-04-28 21:15:36 From: Jonathan Frederic Date: 2013-04-28 21:15:36 Subject: [PATCH] Removed "profiles"... Templates that are shipped with nbconvert by default should have settings built into exporter.py class. If the user wants to add a new template and use profile setting with it, the "profile" (config file) should be specified via the commandline when calling the exporter. --- diff --git a/api/exporter.py b/api/exporter.py index 1c9f8be..bfdd495 100755 --- a/api/exporter.py +++ b/api/exporter.py @@ -122,11 +122,28 @@ class Exporter(Configurable): '', config=True, help="Name of the template file to use") + fileext = Unicode( + 'txt', config=True, + help="Extension of the file that should be written to disk" + ) + + stdout = Bool( + True, config=True, + help="""Whether 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="""Should the converted notebook file be written to disk + along with potential extracted resources.""" + ) + #Processors that process the input data prior to the export, set in the #constructor for this class. preprocessors = [] - def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw): + def __init__(self, preprocessors={}, jinja_filters={}, config=None, export_format, **kw): """ Init a new converter. config: the Configurable config object to pass around. @@ -147,7 +164,15 @@ class Exporter(Configurable): user defined filter will overwrite the one availlable by default. """ - super(ConverterTemplate, self).__init__(config=config, **kw) + + #Merge default config options with user specific override options. + default_config = self._get_default_options() + if not config == None: + default_config._merge(config) + config = default_config + + #Call the base class constructor + super(Exporter, self).__init__(config=config, **kw) #Create a Latex environment if the user is exporting latex. if self.tex_environement: @@ -289,10 +314,66 @@ class Exporter(Configurable): return nb, resources + def _get_default_options(self, export_format): + """ Load the default options for built in formats. + + export_format: Format being exported to. + """ + + c = get_config() + + #Set default data extraction priorities. + c.GlobalConfigurable.display_data_priority =['svg', 'png', 'latex', 'jpg', 'jpeg','text'] + c.ExtractFigureTransformer.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text'] + c.ConverterTemplate.display_data_priority= ['svg', 'png', 'latex', 'jpg', 'jpeg','text'] + + #For most (or all cases), the template file name matches the format name. + c.ConverterTemplate.template_file = export_format + + if export_format == "basichtml" or "fullhtml" or "reveal": + c.CSSHtmlHeaderTransformer.enabled=True + if export_format == 'reveal' + c.NbconvertApp.fileext='reveal.html' + else: + c.NbconvertApp.fileext='html' + + elif export_format == "latex_sphinx_howto" or export_format == "latex_sphinx_manual": + + #Turn on latex environment + c.ConverterTemplate.tex_environement=True + + #Standard latex extension + c.NbconvertApp.fileext='tex' + + #Prioritize latex extraction for latex exports. + c.GlobalConfigurable.display_data_priority =['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] + c.ExtractFigureTransformer.display_data_priority=['latex', 'svg', 'png', 'jpg', 'jpeg'] + c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'} + c.ExtractFigureTransformer.enabled=True + + # Enable latex transformers (make markdown2latex work with math $.) + c.LatexTransformer.enabled=True + c.SphinxTransformer.enabled = True + + elif export_format == 'markdown': + c.NbconvertApp.fileext='md' + c.ExtractFigureTransformer.enabled=True + + elif export_format == 'python': + c.NbconvertApp.fileext='py' + + + elif export_format == 'rst': + c.NbconvertApp.fileext='rst' + c.ExtractFigureTransformer.enabled=True + return c + + #TODO: Comment me. def _rm_fake(strng): return strng.replace('/files/', '') + #TODO: Comment me. def _python_comment(string): return '# '+'\n# '.join(string.split('\n')) diff --git a/nbconvert.py b/nbconvert.py index fc8959c..87e71eb 100755 --- a/nbconvert.py +++ b/nbconvert.py @@ -58,23 +58,6 @@ ERROR_CONFIG_NOT_FOUND = "Config file for profile '%s' not found, giving up." class NbconvertApp(Application): """A basic application to convert ipynb files""" - stdout = Bool( - True, config=True, - help="""Whether 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="""Should 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' @@ -99,42 +82,12 @@ class NbconvertApp(Application): 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 to load the config file. If the file isn't found, catch the - #exception. - try: - Application.load_config_file( - self, - profile_name + '.py', - path=[os.path.join(NBCONVERT_DIR, 'profile')] - ) - return True - - except ConfigFileNotFound: - self.log.warn(ERROR_CONFIG_NOT_FOUND, profile_name) - return False - - def start(self, argv=None): """Convert a notebook in one step""" #Parse the commandline options. self.parse_command_line(argv) - #Load an addition config file if specified by the user via the - #commandline. - cl_config = self.config - profile_file = argv[1] - if not self.load_config_file(profile_file): - exit(1) - self.update_config(cl_config) - #Call base super(NbconvertApp, self).start() @@ -147,11 +100,11 @@ class NbconvertApp(Application): #Create the Jinja template exporter. TODO: Add ability to #import in IPYNB aswell - exporter = Exporter(config=self.config, preprocessors=userpreprocessors) + exporter = Exporter(config=self.config, preprocessors=userpreprocessors,export_format=export_format) #Export output, resources = exporter.from_filename(ipynb_file) - if self.stdout : + if exporter.stdout : print(output.encode('utf-8')) #Get the file name without the '.ipynb' (6 chars) extension and then @@ -161,15 +114,15 @@ class NbconvertApp(Application): out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') #Write file output from conversion. - if self.write : - with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f: + if exporter.write : + with io.open(os.path.join(out_root+'.'+exporter.fileext), 'w') as f: f.write(output) #Output any associate figures into the same "root" directory. binkeys = resources.get('figures', {}).get('binary',{}).keys() textkeys = resources.get('figures', {}).get('text',{}).keys() if binkeys or textkeys : - if self.write: + if exporter.write: files_dir = out_root+'_files' if not os.path.exists(out_root+'_files'): os.mkdir(files_dir) @@ -182,7 +135,7 @@ class NbconvertApp(Application): #Figures that weren't exported which will need to be created by the #user. Tell the user what figures these are. - elif self.stdout: + elif exporter.stdout: print(KEYS_PROMPT_HEAD) print(resources['figures'].keys()) print(KEYS_PROMPT_BODY) diff --git a/nbconvert1/profile/base_html.py b/nbconvert1/profile/base_html.py deleted file mode 100644 index 5338045..0000000 --- a/nbconvert1/profile/base_html.py +++ /dev/null @@ -1,8 +0,0 @@ -c = get_config() - - -c.ConverterTemplate.template_file='basichtml' - -c.NbconvertApp.fileext='html' - -c.CSSHtmlHeaderTransformer.enabled=True diff --git a/nbconvert1/profile/blogger_html.py b/nbconvert1/profile/blogger_html.py deleted file mode 100644 index 0cc34a4..0000000 --- a/nbconvert1/profile/blogger_html.py +++ /dev/null @@ -1,3 +0,0 @@ -c = get_config() - -load_subconfig('base_html.py') diff --git a/nbconvert1/profile/full_html.py b/nbconvert1/profile/full_html.py deleted file mode 100644 index 3049550..0000000 --- a/nbconvert1/profile/full_html.py +++ /dev/null @@ -1,6 +0,0 @@ -c = get_config() - -load_subconfig('base_html.py') - -c.ConverterTemplate.template_file='fullhtml' - diff --git a/nbconvert1/profile/latex_base.py b/nbconvert1/profile/latex_base.py deleted file mode 100644 index 450ac7a..0000000 --- a/nbconvert1/profile/latex_base.py +++ /dev/null @@ -1,18 +0,0 @@ -c = get_config() - -c.ConverterTemplate.template_file='latex_base' -c.ConverterTemplate.tex_environement=True - - -c.NbconvertApp.fileext='tex' - -c.GlobalConfigurable.display_data_priority =['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] -# do not extract text -c.ExtractFigureTransformer.display_data_priority=['latex', 'svg', 'png', 'jpg', 'jpeg'] - - -c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'} -c.ExtractFigureTransformer.enabled=True - -# Enable latex transformer (make markdown2latex work with math $.) -c.LatexTransformer.enabled=True diff --git a/nbconvert1/profile/latex_sphinx_base.py b/nbconvert1/profile/latex_sphinx_base.py deleted file mode 100644 index 3adc4f6..0000000 --- a/nbconvert1/profile/latex_sphinx_base.py +++ /dev/null @@ -1,12 +0,0 @@ -c = get_config() - -# Inherit -load_subconfig('latex_base.py') - -# Overrides -c.ConverterTemplate.template_file='latex_sphinx_base' -c.NbconvertApp.write = True - -# Set sphinx transformer options. -c.SphinxTransformer.enabled = True - diff --git a/nbconvert1/profile/latex_sphinx_howto.py b/nbconvert1/profile/latex_sphinx_howto.py deleted file mode 100644 index 7e50856..0000000 --- a/nbconvert1/profile/latex_sphinx_howto.py +++ /dev/null @@ -1,7 +0,0 @@ -c = get_config() - -#Inherit -load_subconfig('latex_sphinx_base.py') - -#Overrides -c.ConverterTemplate.template_file='latex_sphinx_howto' diff --git a/nbconvert1/profile/latex_sphinx_manual.py b/nbconvert1/profile/latex_sphinx_manual.py deleted file mode 100644 index 922acb0..0000000 --- a/nbconvert1/profile/latex_sphinx_manual.py +++ /dev/null @@ -1,7 +0,0 @@ -c = get_config() - -#Inherit -load_subconfig('latex_sphinx_base.py') - -#Overrides -c.ConverterTemplate.template_file='latex_sphinx_manual' diff --git a/nbconvert1/profile/markdown.py b/nbconvert1/profile/markdown.py deleted file mode 100644 index 4214158..0000000 --- a/nbconvert1/profile/markdown.py +++ /dev/null @@ -1,11 +0,0 @@ -c = get_config() - -c.ConverterTemplate.template_file='markdown' - -c.NbconvertApp.fileext='md' - -c.GlobalConfigurable.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text'] - -c.ExtractFigureTransformer.enabled=True - - diff --git a/nbconvert1/profile/python.py b/nbconvert1/profile/python.py deleted file mode 100644 index 3d55c64..0000000 --- a/nbconvert1/profile/python.py +++ /dev/null @@ -1,5 +0,0 @@ -c = get_config() - -c.ConverterTemplate.template_file='python' - -c.NbconvertApp.fileext='py' diff --git a/nbconvert1/profile/reveal.py b/nbconvert1/profile/reveal.py deleted file mode 100644 index 8b21df1..0000000 --- a/nbconvert1/profile/reveal.py +++ /dev/null @@ -1,7 +0,0 @@ -c = get_config() - -load_subconfig('base_html.py') - -c.ConverterTemplate.template_file='reveal' -c.NbconvertApp.fileext='reveal.html' - diff --git a/nbconvert1/profile/rst.py b/nbconvert1/profile/rst.py deleted file mode 100644 index 361a567..0000000 --- a/nbconvert1/profile/rst.py +++ /dev/null @@ -1,12 +0,0 @@ -c = get_config() - -c.ConverterTemplate.template_file='rst' - -c.NbconvertApp.fileext='rst' -c.ExtractFigureTransformer.enabled=True - -c.GlobalConfigurable.display_data_priority =['svg', 'png', 'latex', 'jpg', 'jpeg','text'] -c.ExtractFigureTransformer.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text'] -c.ConverterTemplate.display_data_priority= ['svg', 'png', 'latex', 'jpg', 'jpeg','text'] - - diff --git a/nbconvert1/profile/test/display_priority.py b/nbconvert1/profile/test/display_priority.py deleted file mode 100644 index a50c60a..0000000 --- a/nbconvert1/profile/test/display_priority.py +++ /dev/null @@ -1,5 +0,0 @@ -c = get_config() - -load_subconfig('null.py') - -c.ConverterTemplate.template_file='display_priority' diff --git a/nbconvert1/profile/test/display_priority_tex.py b/nbconvert1/profile/test/display_priority_tex.py deleted file mode 100644 index d77cf91..0000000 --- a/nbconvert1/profile/test/display_priority_tex.py +++ /dev/null @@ -1,5 +0,0 @@ -c = get_config() - -load_subconfig('null_tex.py') - -c.ConverterTemplate.template_file='display_priority' diff --git a/nbconvert1/profile/test/null.py b/nbconvert1/profile/test/null.py deleted file mode 100644 index 02d32fe..0000000 --- a/nbconvert1/profile/test/null.py +++ /dev/null @@ -1,8 +0,0 @@ -c = get_config() - - -c.ConverterTemplate.extract_figures=False -c.ConverterTemplate.template_file='null' -c.ConverterTemplate.tex_environement=False - -c.ExtractFigureTransformer.enabled = False diff --git a/nbconvert1/profile/test/null_tex.py b/nbconvert1/profile/test/null_tex.py deleted file mode 100644 index 3ee4483..0000000 --- a/nbconvert1/profile/test/null_tex.py +++ /dev/null @@ -1,8 +0,0 @@ -c = get_config() - - -c.ConverterTemplate.extract_figures=False -c.ConverterTemplate.template_file='null' -c.ConverterTemplate.tex_environement=True - -c.ExtractFigureTransformer.enabled = False diff --git a/reveal b/reveal deleted file mode 160000 index 75ffa60..0000000 --- a/reveal +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 75ffa604d8a6ac561c7f574e9d7f7c434f017642