mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-02-22 12:58:36 +00:00
Merge pull request #39 from JaviMerino/ipynb_convert_to_html
ipynb_exporter convert to html
This commit is contained in:
commit
2e4bda71a8
@ -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
|
||||||
|
@ -46,7 +46,8 @@ if IPython:
|
|||||||
|
|
||||||
NotebookNode = IPython.nbformat.NotebookNode # pylint: disable=E1101
|
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'):
|
elif StrictVersion(IPython.__version__) >= StrictVersion('2.0.0'):
|
||||||
import IPython.kernel
|
import IPython.kernel
|
||||||
import IPython.nbformat.v3
|
import IPython.nbformat.v3
|
||||||
@ -59,7 +60,9 @@ if IPython:
|
|||||||
|
|
||||||
NotebookNode = IPython.nbformat.v3.NotebookNode # pylint: disable=E1101
|
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:
|
else:
|
||||||
# Unsupported IPython version
|
# Unsupported IPython version
|
||||||
import_error_str = 'Unsupported IPython version {}'.format(IPython.__version__)
|
import_error_str = 'Unsupported IPython version {}'.format(IPython.__version__)
|
||||||
@ -142,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 + [nbbasename], stderr=devnull)
|
subprocess.check_call(ipython_command + [nbbasename], stderr=devnull)
|
||||||
|
|
||||||
os.chdir(prev_dir)
|
os.chdir(prev_dir)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user