Skip to content

Conversation

AoifeHughes
Copy link
Contributor

@AoifeHughes AoifeHughes commented Sep 29, 2025

This was to try and fix #642 as I was afraid of a caching issue, please see that PR for previous discussions

Aoife and others added 29 commits September 18, 2025 13:31
Co-authored-by: Penelope Yong <penelopeysm@gmail.com>
@AoifeHughes AoifeHughes self-assigned this Sep 29, 2025
@AoifeHughes AoifeHughes changed the title Add notebooks to docs downloasd Add notebooks to docs download Sep 30, 2025
Copy link
Contributor

Preview the changes: https://turinglang.org/docs/pr-previews/652
Please avoid using the search feature and navigation bar in PR previews!

@AoifeHughes AoifeHughes marked this pull request as ready for review October 1, 2025 08:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds notebook generation and download functionality to the documentation site by creating Jupyter notebooks from Quarto .qmd files and providing download links in the rendered HTML pages.

  • Introduces a Python script to convert .qmd files to .ipynb format with proper cell structure
  • Adds shell scripts to generate notebooks and inject download links into HTML pages
  • Integrates notebook generation into both publish and preview GitHub workflows

Reviewed Changes

Copilot reviewed 5 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
assets/scripts/qmd_to_ipynb.py Python converter that parses .qmd files and creates Jupyter notebooks with appropriate cell types
assets/scripts/generate_notebooks.sh Shell script that finds .qmd files and converts them to notebooks in the _site directory
assets/scripts/add_notebook_links.sh Shell script that adds download links to HTML pages using Perl regex replacement
.github/workflows/publish.yml Adds notebook generation and link injection steps to the publish workflow
.github/workflows/preview.yml Adds notebook generation and link injection steps to the preview workflow

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +136 to +138
"display_name": "Julia 1.11",
"language": "julia",
"name": self.kernel_name
Copy link
Preview

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded display name 'Julia 1.11' doesn't match the dynamic kernel name when using Python. Consider making the display name dynamic based on the kernel.

Copilot uses AI. Check for mistakes.

Comment on lines +140 to +145
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.11.0"
}
Copy link
Preview

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The language_info is hardcoded for Julia even when the kernel is Python. This should be conditional based on the detected engine/kernel type.

Copilot uses AI. Check for mistakes.

# Insert the notebook link AFTER the "Report an issue" link
# This ensures it goes in the right place in the sidebar toc-actions
# The download="index.ipynb" attribute forces browser to download instead of navigate
perl -i -pe 's/(<a href="[^"]*issues\/new"[^>]*><i class="bi[^"]*"><\/i>Report an issue<\/a><\/li>)/$1<li><a href="index.ipynb" class="toc-action" download="index.ipynb"><i class="bi bi-journal-code"><\/i>Download notebook<\/a><\/li>/g' "$html_file"
Copy link
Preview

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This complex regex replacement is brittle and hard to maintain. Consider using a more robust HTML parsing approach or breaking this into multiple steps for better readability.

Suggested change
perl -i -pe 's/(<a href="[^"]*issues\/new"[^>]*><i class="bi[^"]*"><\/i>Report an issue<\/a><\/li>)/$1<li><a href="index.ipynb" class="toc-action" download="index.ipynb"><i class="bi bi-journal-code"><\/i>Download notebook<\/a><\/li>/g' "$html_file"
python3 - <<EOF
import sys
from bs4 import BeautifulSoup
html_file = "$html_file"
with open(html_file, "r", encoding="utf-8") as f:
soup = BeautifulSoup(f, "html.parser")
# Find the "Report an issue" link
report_link = None
for a in soup.find_all("a"):
if a.get_text(strip=True) == "Report an issue":
report_link = a
break
if report_link is not None:
# Insert the new link after the parent <li> of the report_link
parent_li = report_link.find_parent("li")
if parent_li is not None and parent_li.parent is not None:
new_li = soup.new_tag("li")
new_a = soup.new_tag("a", href="index.ipynb", **{"class": "toc-action", "download": "index.ipynb"})
new_i = soup.new_tag("i", **{"class": "bi bi-journal-code"})
new_a.append(new_i)
new_a.append("Download notebook")
new_li.append(new_a)
parent_li.insert_after(new_li)
with open(html_file, "w", encoding="utf-8") as f:
f.write(str(soup))
EOF

Copilot uses AI. Check for mistakes.

echo "Generating Jupyter notebooks from .qmd files..."

# Find all .qmd files in tutorials, usage, and developers directories
find tutorials usage developers -name "index.qmd" | while read qmd_file; do
Copy link
Preview

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 'while read' with 'find' can fail with filenames containing spaces or special characters. Use 'find ... -print0 | while IFS= read -r -d "" qmd_file; do' for safer file handling.

Suggested change
find tutorials usage developers -name "index.qmd" | while read qmd_file; do
find tutorials usage developers -name "index.qmd" -print0 | while IFS= read -r -d '' qmd_file; do

Copilot uses AI. Check for mistakes.

@AoifeHughes
Copy link
Contributor Author

Screenshot 2025-10-01 at 09 26 20

This PR adds a download button to get jupyter notebooks from the docs.

Some notes:

  • quarto doesn't like either using convert or the _quarto.yml config options to make notebooks as well as html
  • To avoid running each file 2x in the deployment I created a few scripts, the main of which is https://github.com/TuringLang/docs/pull/652/files#diff-fa790f7c53fe8322e42e5baf383fa2350e43f4f279c3ee25e4aeac1d4680f3a4 which handles the conversion and builds the notebooks
  • The CI has been updated to perform this
  • I would have much rather used quarto's built in solutions, but they're just not fit for purpose in this instance (please see the dozens of commits in which I tried every solution I could find before raising them as possible solutions @mhauru can attest to the numerous attempts I made in the last week).

@shravanngoswamii if you'd have some time to check these out, I'd appreciate it.

@AoifeHughes
Copy link
Contributor Author

I should also mention, I had a few issues with the CI versions of bash/posix vs deploying on my mac, so some of the scripting is done to appease github actions moreso than how users may run locally.

@AoifeHughes
Copy link
Contributor Author

I think this partially satisfies some of the points from #641 ~ it might be worth adding some note about these being available in the docs / have a quick way to export into online systems, as discussed in that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants