prefect_jupyter.notebook
Module containing tasks for interacting with Jupyter.
Classes
OutputFormat
Bases: Enum
Valid output formats of a notebook.
Source code in prefect_jupyter/notebook.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Functions
execute_notebook
Task for running Jupyter Notebooks.
In order to parametrize the notebook, you need to mark the parameters cell as described in the papermill documentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Where to fetch the notebook from; can be a cloud storage path. |
required |
parameters |
Optional[Dict[str, Any]]
|
Parameters to use for the notebook. |
None
|
log_output |
bool
|
Whether or not to log notebook cell output to the papermill logger. |
False
|
kernel_name |
Optional[str]
|
Name of kernel to execute the notebook against. |
None
|
**execute_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
str
|
The NotebookNode object of the executed notebook. |
Examples:
Run a parameterized notebook.
from prefect import flow
from prefect_jupyter import notebook
@flow
def example_execute_notebook():
nb = notebook.execute_notebook(
"test_notebook.ipynb",
parameters={"num": 5}
)
body = notebook.export_notebook(nb)
output_path = "executed_notebook.ipynb"
with open(output_path, "w") as f:
f.write(body)
return output_path
example_execute_notebook()
Source code in prefect_jupyter/notebook.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
export_notebook
Task for exporting a NotebookNode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nb |
NotebookNode
|
The notebook to export. |
required |
output_format |
OutputFormat
|
The notebook output format. |
NOTEBOOK
|
**export_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
str
|
The body of the output. |
Examples:
Export a notebook to HTML
import nbformat
from prefect import flow
from prefect_jupyter import notebook
@flow
def example_export_notebook():
with open("example_notebook.ipynb", "r") as f:
nb = nbformat.read(f, as_version=4)
html_body = notebook.export_notebook(
nb, output_format=notebook.OutputFormat.HTML
)
with open("exported_notebook.html", "w") as f:
f.write(html_body)
example_export_notebook()
Source code in prefect_jupyter/notebook.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|