diff --git a/wlauto/result_processors/ipynb_exporter/__init__.py b/wlauto/result_processors/ipynb_exporter/__init__.py index 82a3c892..83f56735 100644 --- a/wlauto/result_processors/ipynb_exporter/__init__.py +++ b/wlauto/result_processors/ipynb_exporter/__init__.py @@ -84,6 +84,12 @@ class IPythonNotebookExporter(ResultProcessor): .. note:: the URL should not contain the final part (the notebook name) which will be populated automatically. '''), + Parameter('convert_to_html', kind=bool, + description='Convert the resulting notebook to HTML.'), + Parameter('show_html', kind=bool, + description='''Open the exported html notebook at the end of + the run. This can only be selected if convert_to_html has + also been selected.'''), Parameter('convert_to_pdf', kind=bool, description='Convert the resulting notebook to PDF.'), Parameter('show_pdf', kind=bool, @@ -111,6 +117,10 @@ class IPythonNotebookExporter(ResultProcessor): if self.notebook_directory and not os.path.isdir(self.notebook_directory): raise ConfigError('notebook_directory {} does not exist'.format(self.notebook_directory)) + if self.show_html and not self.convert_to_html: # pylint: disable=E0203 + self.convert_to_html = True + self.logger.debug('Assuming "convert_to_html" as "show_html" is set') + if self.show_pdf and not self.convert_to_pdf: # pylint: disable=E0203 self.convert_to_pdf = True self.logger.debug('Assuming "convert_to_pdf" as "show_pdf" is set') @@ -121,10 +131,16 @@ class IPythonNotebookExporter(ResultProcessor): self.open_notebook() if self.convert_to_pdf: - ipython.generate_pdf(self.nbbasename, - context.run_output_directory) + ipython.export_notebook(self.nbbasename, + context.run_output_directory, 'pdf') if self.show_pdf: - self.open_pdf() + self.open_file('pdf') + + if self.convert_to_html: + ipython.export_notebook(self.nbbasename, + context.run_output_directory, 'html') + if self.show_html: + self.open_file('html') def generate_notebook(self, result, context): """Generate a notebook from the template and run it""" @@ -149,10 +165,10 @@ class IPythonNotebookExporter(ResultProcessor): """Open the notebook in a browser""" webbrowser.open(self.notebook_url.rstrip('/') + '/' + self.nbbasename) - def open_pdf(self): - """Open the PDF""" - pdf_file = os.path.splitext(self.notebook_file)[0] + ".pdf" - open_file(pdf_file) + def open_file(self, output_format): + """Open the exported notebook""" + fname = os.path.splitext(self.notebook_file)[0] + "." + output_format + open_file(fname) # Add the default template to the documentation diff --git a/wlauto/utils/ipython.py b/wlauto/utils/ipython.py index 3ec3f443..74802c71 100644 --- a/wlauto/utils/ipython.py +++ b/wlauto/utils/ipython.py @@ -46,7 +46,8 @@ if IPython: NotebookNode = IPython.nbformat.NotebookNode # pylint: disable=E1101 - IPYTHON_NBCONVERT = ['ipython', 'nbconvert', '--to=pdf'] + IPYTHON_NBCONVERT_HTML = ['ipython', 'nbconvert', '--to=html'] + IPYTHON_NBCONVERT_PDF = ['ipython', 'nbconvert', '--to=pdf'] elif StrictVersion(IPython.__version__) >= StrictVersion('2.0.0'): import IPython.kernel import IPython.nbformat.v3 @@ -59,7 +60,9 @@ if IPython: NotebookNode = IPython.nbformat.v3.NotebookNode # pylint: disable=E1101 - IPYTHON_NBCONVERT = ['ipython', 'nbconvert', '--to=latex', '--post=PDF'] + IPYTHON_NBCONVERT_HTML = ['ipython', 'nbconvert', '--to=html'] + IPYTHON_NBCONVERT_PDF = ['ipython', 'nbconvert', '--to=latex', + '--post=PDF'] else: # Unsupported IPython version import_error_str = 'Unsupported IPython version {}'.format(IPython.__version__) @@ -142,19 +145,28 @@ def run_notebook(notebook): kernel_manager.shutdown_kernel() -def generate_pdf(nbbasename, output_directory): - """Generate a PDF from the ipython notebook +def export_notebook(nbbasename, output_directory, output_format): + """Generate a PDF or HTML from the ipython notebook + + output_format has to be either 'pdf' or 'html'. These are the + only formats currently supported. ipython nbconvert claims that the CLI is not stable, so keep this function here to be able to cope with inconsistencies """ + if output_format == "html": + ipython_command = IPYTHON_NBCONVERT_HTML + elif output_format == "pdf": + ipython_command = IPYTHON_NBCONVERT_PDF + else: + raise ValueError("Unknown output format: {}".format(output_format)) + prev_dir = os.getcwd() os.chdir(output_directory) with open(os.devnull, 'w') as devnull: - subprocess.check_call(IPYTHON_NBCONVERT + [nbbasename], stderr=devnull) + subprocess.check_call(ipython_command + [nbbasename], stderr=devnull) os.chdir(prev_dir) -