-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π MERGE: Improve Notebook Execution (#236)
1. Standardise auto/cache execution Both now call the same underlying function (from jupyter-cache) and act the same. This improves auto, by making it output error reports and not raising an exception on an error. Additional config has also been added: `execution_allow_errors` and `execution_in_temp`. Like for timeout, `allow_errors` can also be set in the notebook `metadata.execution.allow_errors` This presents one breaking change, in that `auto` will now by default execute in a temporary folder as the cwd. (we could set temp to False by default, but I think this is safer?) 2. For both methods, executions data is captured into: ```python env.nb_execution_data[env.docname] = { "mtime": datetime.datetime.utcnow().isoformat(), "runtime": runtime, "method": execution_method, "succeeded": succeeded, } ``` and a directive `nb-exec-table` has been added, to create a table of these results.
- Loading branch information
Showing
27 changed files
with
634 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"""A directive to create a table of executed notebooks, and related statistics.""" | ||
from datetime import datetime | ||
|
||
from docutils import nodes | ||
from sphinx.transforms.post_transforms import SphinxPostTransform | ||
from sphinx.util.docutils import SphinxDirective | ||
|
||
|
||
def setup_exec_table(app): | ||
"""execution statistics table.""" | ||
app.add_node(ExecutionStatsNode) | ||
app.add_directive("nb-exec-table", ExecutionStatsTable) | ||
app.add_post_transform(ExecutionStatsPostTransform) | ||
|
||
|
||
class ExecutionStatsNode(nodes.General, nodes.Element): | ||
"""A placeholder node, for adding a notebook execution statistics table.""" | ||
|
||
|
||
class ExecutionStatsTable(SphinxDirective): | ||
"""Add a notebook execution statistics table.""" | ||
|
||
has_content = True | ||
final_argument_whitespace = True | ||
|
||
def run(self): | ||
|
||
return [ExecutionStatsNode()] | ||
|
||
|
||
class ExecutionStatsPostTransform(SphinxPostTransform): | ||
"""Replace the placeholder node with the final table.""" | ||
|
||
default_priority = 400 | ||
|
||
def run(self, **kwargs) -> None: | ||
for node in self.document.traverse(ExecutionStatsNode): | ||
node.replace_self(make_stat_table(self.env.nb_execution_data)) | ||
|
||
|
||
def make_stat_table(nb_execution_data): | ||
|
||
key2header = { | ||
"mtime": "Modified", | ||
"method": "Method", | ||
"runtime": "Run Time (s)", | ||
"succeeded": "Status", | ||
} | ||
|
||
key2transform = { | ||
"mtime": lambda x: datetime.fromtimestamp(x).strftime("%Y-%m-%d %H:%M") | ||
if x | ||
else "", | ||
"method": str, | ||
"runtime": lambda x: "-" if x is None else str(round(x, 2)), | ||
"succeeded": lambda x: "β " if x is True else "β", | ||
} | ||
|
||
# top-level element | ||
table = nodes.table() | ||
table["classes"] += ["colwidths-auto"] | ||
# self.set_source_info(table) | ||
|
||
# column settings element | ||
ncols = len(key2header) + 1 | ||
tgroup = nodes.tgroup(cols=ncols) | ||
table += tgroup | ||
colwidths = [round(100 / ncols, 2)] * ncols | ||
for colwidth in colwidths: | ||
colspec = nodes.colspec(colwidth=colwidth) | ||
tgroup += colspec | ||
|
||
# header | ||
thead = nodes.thead() | ||
tgroup += thead | ||
row = nodes.row() | ||
thead += row | ||
|
||
for name in ["Document"] + list(key2header.values()): | ||
row.append(nodes.entry("", nodes.paragraph(text=name))) | ||
|
||
# body | ||
tbody = nodes.tbody() | ||
tgroup += tbody | ||
|
||
for doc, data in nb_execution_data.items(): | ||
row = nodes.row() | ||
tbody += row | ||
row.append(nodes.entry("", nodes.paragraph(text=doc))) | ||
for name in key2header.keys(): | ||
text = key2transform[name](data[name]) | ||
row.append(nodes.entry("", nodes.paragraph(text=text))) | ||
|
||
return table |
Oops, something went wrong.