Created
September 27, 2019 10:53
-
-
Save nchaly/e4bc726b07ec1f150c1cec55523a2d7c to your computer and use it in GitHub Desktop.
How to pass arguments to jupyter notebook (via papermill), run it (using nbconvert), and then write output to html file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import nbformat | |
| import os | |
| from nbconvert.preprocessors import ExecutePreprocessor, CellExecutionError | |
| from nbconvert import HTMLExporter | |
| from nbconvert.writers import FilesWriter | |
| import papermill.parameterize | |
| def prepare_metadata(nb): | |
| if not hasattr(nb.metadata, 'papermill'): | |
| nb.metadata['papermill'] = { | |
| 'parameters': dict(), | |
| 'environment_variables': dict(), | |
| # 'version': __version__, | |
| } | |
| for cell in nb.cells: | |
| if not hasattr(cell.metadata, 'tags'): | |
| cell.metadata['tags'] = [] # Create tags attr if one doesn't exist. | |
| if not hasattr(cell.metadata, 'papermill'): | |
| cell.metadata['papermill'] = dict() | |
| return nb | |
| def exec_notebook(notebook_filename, | |
| output_filename, | |
| parameters_dict=None, | |
| output_format='html', | |
| as_version=nbformat.NO_CONVERT, | |
| timeout=None, | |
| kernel_name=None): | |
| """ | |
| Executes jupyter notebook and exports result to file. | |
| :return: | |
| """ | |
| if not notebook_filename: | |
| raise ValueError('Notebook filename should be provided') | |
| if not os.path.isfile(notebook_filename): | |
| raise ValueError('Notebook file not found: {}'.format(notebook_filename)) | |
| if output_format == 'html': | |
| exporter = HTMLExporter() | |
| else: | |
| raise ValueError('Only html format is supported now.') | |
| with open(notebook_filename) as f: | |
| nb = nbformat.read(f, as_version=as_version) | |
| prepare_metadata(nb) | |
| if parameters_dict: | |
| nb = papermill.parameterize.parameterize_notebook(nb, parameters=parameters_dict) | |
| ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel_name) | |
| ep.preprocess(nb) | |
| data, resources = exporter.from_notebook_node(nb) | |
| writer = FilesWriter() | |
| writer.write(data, resources, notebook_name=output_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment