forked from datadesk/california-coronavirus-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
87 lines (67 loc) · 2.45 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
85
86
87
# -*- coding: utf-8 -*-
"""
A command-line interface for running Jupyter Notebooks.
Usage: run.py [OPTIONS] [NOTEBOOK_PATHS]...
Executes Jupyter Notebooks from the command line.
Expects one or more file paths input as arguments.
Errors are raised and printed to the console.
Example:
$ python run.py ./src/notebooks.ipynb
Options:
--help Show this message and exit.
"""
import os
import click
import pathlib
import nbformat
from nbclient import NotebookClient
from nbconvert.preprocessors import CellExecutionError
@click.command()
@click.argument('notebook_paths', nargs=-1, type=click.Path(exists=True))
def main(notebook_paths):
"""
Executes Jupyter Notebooks from the command line.
Expects one or more file paths input as arguments.
Errors are raised and printed to the console.
Example:
$ python run.py ./src/notebooks.ipynb
"""
for path_string in notebook_paths:
click.echo(f"🐍🗒️ {path_string}")
# Get the file name
name = path_string.replace(".ipynb", "")
# Get its parent directory so we can add it to the $PATH
path = pathlib.Path(path_string).parent.absolute()
# Set the intput and output file paths
input_path = f"{name}.ipynb"
output_path = f"{name}-output.ipynb"
# Open up the notebook we're going to run
with open(input_path) as f:
nb = nbformat.read(f, as_version=4)
# Configure nbclient to run the notebook
client = NotebookClient(
# What we want it to run
nb,
timeout=600,
kernel_name='python3',
# We want to allow errors so it doesn't fail silently
allow_errors=False,
force_raise_errors=True,
# Here's where the path gets set
resources={'metadata': {'path': path}}
)
try:
# Run it
client.execute()
except CellExecutionError:
# If there's an error, print it to the terminal.
msg = f"Error executing {input_path}.\n See {output_path} for the traceback."
click.echo(msg)
# And then raise it too
raise
finally:
# Once all that's done, write out the output notebook to the filesystem
with open(output_path, mode='w', encoding='utf-8') as f:
nbformat.write(nb, f)
if __name__ == '__main__':
main()