1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-02-21 20:38:57 +00:00

ipynb_exporter: learn to convert the notebook to HTML

This commit is contained in:
Javi Merino 2015-07-09 11:44:40 +01:00
parent 539c3de7b8
commit 30d7ee52f4
2 changed files with 38 additions and 11 deletions

View File

@ -84,6 +84,12 @@ class IPythonNotebookExporter(ResultProcessor):
.. note:: the URL should not contain the final part (the notebook name) which will be populated automatically. .. 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, Parameter('convert_to_pdf', kind=bool,
description='Convert the resulting notebook to PDF.'), description='Convert the resulting notebook to PDF.'),
Parameter('show_pdf', kind=bool, Parameter('show_pdf', kind=bool,
@ -111,6 +117,10 @@ class IPythonNotebookExporter(ResultProcessor):
if self.notebook_directory and not os.path.isdir(self.notebook_directory): if self.notebook_directory and not os.path.isdir(self.notebook_directory):
raise ConfigError('notebook_directory {} does not exist'.format(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 if self.show_pdf and not self.convert_to_pdf: # pylint: disable=E0203
self.convert_to_pdf = True self.convert_to_pdf = True
self.logger.debug('Assuming "convert_to_pdf" as "show_pdf" is set') self.logger.debug('Assuming "convert_to_pdf" as "show_pdf" is set')
@ -121,10 +131,16 @@ class IPythonNotebookExporter(ResultProcessor):
self.open_notebook() self.open_notebook()
if self.convert_to_pdf: if self.convert_to_pdf:
ipython.generate_pdf(self.nbbasename, ipython.export_notebook(self.nbbasename,
context.run_output_directory) context.run_output_directory, 'pdf')
if self.show_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): def generate_notebook(self, result, context):
"""Generate a notebook from the template and run it""" """Generate a notebook from the template and run it"""
@ -149,10 +165,10 @@ class IPythonNotebookExporter(ResultProcessor):
"""Open the notebook in a browser""" """Open the notebook in a browser"""
webbrowser.open(self.notebook_url.rstrip('/') + '/' + self.nbbasename) webbrowser.open(self.notebook_url.rstrip('/') + '/' + self.nbbasename)
def open_pdf(self): def open_file(self, output_format):
"""Open the PDF""" """Open the exported notebook"""
pdf_file = os.path.splitext(self.notebook_file)[0] + ".pdf" fname = os.path.splitext(self.notebook_file)[0] + "." + output_format
open_file(pdf_file) open_file(fname)
# Add the default template to the documentation # Add the default template to the documentation

View File

@ -46,6 +46,7 @@ if IPython:
NotebookNode = IPython.nbformat.NotebookNode # pylint: disable=E1101 NotebookNode = IPython.nbformat.NotebookNode # pylint: disable=E1101
IPYTHON_NBCONVERT_HTML = ['ipython', 'nbconvert', '--to=html']
IPYTHON_NBCONVERT_PDF = ['ipython', 'nbconvert', '--to=pdf'] IPYTHON_NBCONVERT_PDF = ['ipython', 'nbconvert', '--to=pdf']
elif StrictVersion(IPython.__version__) >= StrictVersion('2.0.0'): elif StrictVersion(IPython.__version__) >= StrictVersion('2.0.0'):
import IPython.kernel import IPython.kernel
@ -59,6 +60,7 @@ if IPython:
NotebookNode = IPython.nbformat.v3.NotebookNode # pylint: disable=E1101 NotebookNode = IPython.nbformat.v3.NotebookNode # pylint: disable=E1101
IPYTHON_NBCONVERT_HTML = ['ipython', 'nbconvert', '--to=html']
IPYTHON_NBCONVERT_PDF = ['ipython', 'nbconvert', '--to=latex', IPYTHON_NBCONVERT_PDF = ['ipython', 'nbconvert', '--to=latex',
'--post=PDF'] '--post=PDF']
else: else:
@ -143,19 +145,28 @@ def run_notebook(notebook):
kernel_manager.shutdown_kernel() kernel_manager.shutdown_kernel()
def generate_pdf(nbbasename, output_directory): def export_notebook(nbbasename, output_directory, output_format):
"""Generate a PDF from the ipython notebook """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 ipython nbconvert claims that the CLI is not stable, so keep this
function here to be able to cope with inconsistencies 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() prev_dir = os.getcwd()
os.chdir(output_directory) os.chdir(output_directory)
with open(os.devnull, 'w') as devnull: with open(os.devnull, 'w') as devnull:
subprocess.check_call(IPYTHON_NBCONVERT_PDF + [nbbasename], stderr=devnull) subprocess.check_call(ipython_command + [nbbasename], stderr=devnull)
os.chdir(prev_dir) os.chdir(prev_dir)