Skip to content

Commit

Permalink
extract notebook ids from deepnote put
Browse files Browse the repository at this point in the history
  • Loading branch information
RussTedrake committed Dec 14, 2023
1 parent 91db019 commit 95a49ae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
27 changes: 18 additions & 9 deletions book.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,35 @@ function system_html(sys, url = null) {
}

function notebook_header(chapter) {
if (chapter in deepnote) {
return `<a href="https://deepnote.com/workspace/${deepnote_workspace_id}/project/${deepnote[chapter]}/notebook/%2F${chapter}" style="float:right; margin-top:20px; margin-bottom:-100px;background:white;border:0;" target="${chapter}">
if (chapter in chapter_project_ids) {
return `<a href="https://deepnote.com/workspace/${deepnote_workspace_id}/project/${chapter_project_ids[chapter]}/" style="float:right; margin-top:20px; margin-bottom:-100px;background:white;border:0;" target="${chapter}">
<img src="https://deepnote.com/buttons/launch-in-deepnote-white.svg"></a>
<div style="clear:right;"></div>`;
}
return "";
}

drake_deepnote = {"tutorials": "2b4fc509-aef2-417d-a40d-6071dfed9199"}
drake_tutorials_id = "2b4fc509-aef2-417d-a40d-6071dfed9199"
drake_workspace_id = "Drake-0b3b2c53-a7ad-441b-80f8-bf8350752305"

function notebook_link(project, d=deepnote, link_text="", notebook="", workspace_id=deepnote_workspace_id) {
if (notebook == "") {
notebook = project;
function drake_tutorials_link(name, link_text="") {
// TODO(russt): Retrieve IDs for sp
if (link_text) {
return `<a href="https://deepnote.com/workspace/${drake_workspace_id}/project/${drake_tutorials_id}" target="drake_tutorials">${link_text}</a>`;
} else {
return `<p><a href="https://deepnote.com/workspace/${drake_workspace_id}/project/${drake_tutorials_id}" style="background:none; border:none;" target="drake_tutorials"> <img src="https://deepnote.com/buttons/launch-in-deepnote-white.svg"></a></p>`;
}
if (project in d) {
}

function notebook_link(chapter, notebook, link_text="") {
if (!notebook) { notebook = chapter; }
chapter_project_id = chapter_project_ids[chapter];
notebook_id = notebook_ids[chapter][notebook];
if (notebook_id) {
if (link_text) {
return `<a href="https://deepnote.com/workspace/${workspace_id}/project/${d[project]}/notebook/%2F${notebook}" target="${project}">${link_text}</a>`;
return `<a href="https://deepnote.com/workspace/${deepnote_workspace_id}/project/${chapter_project_id}/notebook/${notebook}-${notebook_id}" target="${chapter}">${link_text}</a>`;
} else {
return `<p><a href="https://deepnote.com/workspace/${workspace_id}/project/${d[project]}/notebook/%2F${notebook}" style="background:none; border:none;" target="${project}"> <img src="https://deepnote.com/buttons/launch-in-deepnote-white.svg"></a></p>`;
return `<p><a href="https://deepnote.com/workspace/${deepnote_workspace_id}/project/${chapter_project_id}/notebook/${notebook}-${notebook_id}" style="background:none; border:none;" target="${chapter}"> <img src="https://deepnote.com/buttons/launch-in-deepnote-white.svg"></a></p>`;
}
}
return `<p><center>ERROR: <i>Notebook link not found. Please do a "force reload" of this page. If that doesn't fix it, please email russt@mit.edu and let me know.</i></center></p>`;
Expand Down
49 changes: 24 additions & 25 deletions publish_to_deepnote.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@
root = os.path.dirname(htmlbook)
repository = os.path.basename(root)

notebooks = json.load(open("Deepnote.json"))

for path in Path(root).rglob("*/Deepnote.json"):
# Ignore some directories.
if "bazel" in str(path):
continue
d = path.parent.relative_to(Path(root))
notebooks[str(d)] = json.load(open(path))
deepnote = json.load(open("Deepnote.json"))

api_key = os.environ["DEEPNOTE_API_KEY_" + repository.upper()]
headers = {"Authorization": f"Bearer {api_key}"}
Expand All @@ -43,9 +36,10 @@

def update(notebook, project_id, path=""):
expected_files = set(["Dockerfile"])
expected_notebooks = {"Init"}
expected_notebooks = dict()
notebook_path = Path(path) / notebook
notebook = Path(notebook).stem

# If notebook is a directory, publish all notebooks in that directory
if notebook_path.is_dir():
for p in notebook_path.rglob("*.ipynb"):
Expand Down Expand Up @@ -89,9 +83,7 @@ def update(notebook, project_id, path=""):
print("failed to update notebook")
r = requests.put(url, headers=headers, json=payload)
print(r.status_code, r.reason, r.text)
# TODO(russt): r.text contains the magic sha for the link:
# e.g. for intro/intro.ipynb, it contains {"id":"7553c700044345fe8ce505eecfcf087f"}
expected_notebooks.update([f"{notebook}"])
expected_notebooks[notebook] = json.loads(r.text)["id"]
return expected_files, expected_notebooks


Expand All @@ -118,31 +110,38 @@ def check_files(expected_files, expected_notebooks, project_id):

separator = output.index("---")
notebooks = set(output[:separator])
expected_notebooks = set(expected_notebooks)
files = set(output[separator + 1 :])
expected_files = set(expected_files)

if not expected_notebooks == notebooks:
print(f"Expected notebooks: {expected_notebooks}")
print(f"Notebooks on Deepnote: {notebooks}")
if expected_notebooks - notebooks:
print(f"Missing notebooks: {expected_notebooks - notebooks}")
if notebooks - expected_notebooks:
print(f"Extra notebooks: {notebooks - expected_notebooks}")

if not expected_files == files:
print(f"Expected files: {expected_files}")
print(f"Files on Deepnote: {files}")
if expected_files - files:
print(f"Missing files: {expected_files - files}")
if files - expected_files:
print(f"Extra files: {files - expected_files}")


with open(Path(root) / "Deepnote_workspace.txt") as workspace_file:
workspace = workspace_file.read()

for notebook, project_id in notebooks.items():
if isinstance(project_id, dict):
for n, p in project_id.items():
expected_files, expected_notebooks = update(n, p, notebook)
check_files(expected_files, expected_notebooks, p)
else:
expected_files, expected_notebooks = update(notebook, project_id, "")
check_files(expected_files, expected_notebooks, project_id)
notebooks = dict()

for chapter, project_id in deepnote.items():
expected_files, chapter_notebooks = update(chapter, project_id, "")
check_files(expected_files, chapter_notebooks.keys(), project_id)
notebooks[chapter] = chapter_notebooks

with open(Path(root) / "chapters.js", "w") as f:
f.write("deepnote = ")
f.write("chapter_project_ids = ")
json.dump(deepnote, f, sort_keys=True)
f.write("\n")
f.write("notebook_ids = ")
json.dump(notebooks, f, sort_keys=True)
f.write("\n")
f.write(f'deepnote_workspace_id = "{workspace}"')

0 comments on commit 95a49ae

Please sign in to comment.