Last active
March 24, 2022 00:12
-
-
Save zonca/2f2eba4f0288ca4079f7f83efa6b9048 to your computer and use it in GitHub Desktop.
Snapshot a Jupyter Notebook to a gist and add link to git
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
#!/bin/env python | |
import nbformat as nbf | |
import sys | |
from datetime import datetime | |
input_notebook = sys.argv[1] | |
gist_link = sys.argv[2] | |
nb = nbf.read(input_notebook, 4) | |
found_existing_cell = False | |
for cell in nb["cells"]: | |
if "tags" in cell["metadata"] and "snapshotlog" in cell["metadata"]["tags"]: | |
found_existing_cell = True | |
break | |
now = datetime.now() | |
dt_string = now.strftime("%d/%m/%Y %H:%M") | |
log_line = f"\n* [{dt_string}]({gist_link})" | |
if found_existing_cell: | |
cell["source"] += log_line | |
else: | |
nb["cells"].append( | |
nbf.v4.new_markdown_cell( | |
"## Execution log\n" + log_line, metadata={"tags": ["snapshotlog"]} | |
) | |
) | |
nbf.write(nb, input_notebook) |
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
if [ $# -lt 1 ] | |
then | |
echo "USAGE: $0 filename1 filename2" | |
echo | |
echo "Uploads one or more files to a gist" | |
echo "and appends the link to it to the latest git commit message" | |
echo "and creates a cell at the end of the notebook with date time and link to the gist" | |
echo "Binary files are not supported" | |
exit 1 | |
fi | |
NB=$@ | |
OLD_MSG=$(git log --format=%B -n1) | |
GIST=$(gh gist create $NB) | |
echo $GIST | |
git commit --amend -m"$OLD_MSG" -m"Executed artifact(s): $GIST" | |
append_nb.py $NB $GIST |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment