Skip to content

docs: add Open in Colab badges to tutorial notebooks#391

Merged
johnnygreco merged 4 commits intomainfrom
feat/colab-notebook-badges
Mar 13, 2026
Merged

docs: add Open in Colab badges to tutorial notebooks#391
johnnygreco merged 4 commits intomainfrom
feat/colab-notebook-badges

Conversation

@mvansegbroeck
Copy link
Copy Markdown
Contributor

@mvansegbroeck mvansegbroeck commented Mar 10, 2026

Summary

Adds an "Open in Colab" badge as the first cell in all 6 tutorial notebooks under docs/colab_notebooks/.

Instead of editing the generated .ipynb files directly (which would be overwritten on the next regeneration), the badge injection is done in docs/scripts/generate_colab_notebooks.py - the script that generates these notebooks from the source files in docs/notebook_source/. This ensures the badges persist across regenerations.

Changes

  • Add COLAB_BADGE_TEMPLATE constant to generate_colab_notebooks.py
  • Prepend an "Open in Colab" markdown cell in process_notebook()
  • Regenerate all 6 Colab notebooks with the badge

Notes

  • The docs site already has Colab badges via docs/overrides/main.html (injected on pages with page.nb_url). This PR adds badges inside the notebooks themselves, which is useful for people browsing them directly on GitHub.

@mvansegbroeck mvansegbroeck requested a review from a team as a code owner March 10, 2026 21:24
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 10, 2026

Greptile Summary

This PR adds "Open in Colab" badges to all 6 tutorial notebooks under docs/colab_notebooks/ by modifying the generation script rather than the .ipynb files directly, ensuring the badges persist across future regenerations.

  • COLAB_BADGE_TEMPLATE is introduced as a module-level constant and uses Python implicit string concatenation; source_path.stem correctly derives the output notebook filename (e.g., 1-the-basics from 1-the-basics.py), which matches the output_path construction on line 126 of the same script — the URL mapping is accurate for all 6 notebooks.
  • The badge cell is inserted at index 0 via new_markdown_cell, before the existing title and Colab setup cells; this mirrors the standard convention used by many open-source ML notebooks.
  • All six regenerated .ipynb files have had their cell IDs regenerated — this is expected behavior from the jupytext.write call with cell_metadata_filter: -id and is not a concern.
  • The blob/main/ branch reference hardcoded in the template is intentional and standard for Colab badges, pointing users to the stable published version of the notebooks.

Confidence Score: 5/5

  • This PR is safe to merge — it is a documentation enhancement with no logic changes to library code.
  • Changes are purely additive: a new constant and a single-line cell prepend in the generation script, plus the 6 regenerated notebooks. The badge URLs are correctly constructed using source_path.stem, the template string is well-formed HTML, and no existing functionality is altered.
  • No files require special attention.

Important Files Changed

Filename Overview
docs/scripts/generate_colab_notebooks.py Added COLAB_BADGE_TEMPLATE constant and prepends a badge markdown cell as the first cell in process_notebook(). Logic is correct: source_path.stem correctly derives the output .ipynb filename, matching the output_path construction on line 126.
docs/colab_notebooks/1-the-basics.ipynb Badge cell prepended as first cell with a correct Colab URL. All existing cell IDs were regenerated — expected behavior from the nbformat cell creation path in the script.
docs/colab_notebooks/2-structured-outputs-and-jinja-expressions.ipynb Badge cell correctly added with matching filename in the Colab URL. Cell IDs regenerated as expected.
docs/colab_notebooks/3-seeding-with-a-dataset.ipynb Badge cell correctly added with matching filename in the Colab URL. Cell IDs regenerated as expected.
docs/colab_notebooks/4-providing-images-as-context.ipynb Badge cell correctly added with matching filename in the Colab URL. Cell IDs regenerated as expected.
docs/colab_notebooks/5-generating-images.ipynb Badge cell correctly added with matching filename in the Colab URL. Cell IDs regenerated as expected.
docs/colab_notebooks/6-editing-images-with-image-context.ipynb Badge cell correctly added with matching filename in the Colab URL. Cell IDs regenerated as expected.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[docs/notebook_source/*.py] --> B[jupytext.read]
    B --> C[process_notebook]
    C --> D{build badge cell}
    D -->|COLAB_BADGE_TEMPLATE.format\nsource_path.stem + .ipynb| E[new_markdown_cell badge]
    C --> F[find_import_section_index]
    F --> G[create_colab_setup_cells]
    G --> H[inject setup cells before imports]
    E --> I[prepend badge as cell 0]
    H --> I
    I --> J[jupytext.write]
    J --> K[docs/colab_notebooks/*.ipynb]
Loading

Last reviewed commit: 56d3c44

@andreatgretel
Copy link
Copy Markdown
Contributor

Hey @mvansegbroeck, cool idea with the Colab badges!

One thing though - the notebooks in docs/colab_notebooks/ are auto-generated from the source files in docs/notebook_source/ by docs/scripts/generate_colab_notebooks.py (make generate-colab-notebooks). There's a CI check that verifies they stay in sync, so direct edits to the .ipynb files get overwritten on the next regeneration.

Also, we actually already have Colab badges on the docs site - see docs/overrides/main.html. It injects the badge on any tutorial page that has page.nb_url set. So this should already be covered when viewing the notebooks through the docs.

If you still want the badge inside the notebooks themselves (e.g. for people browsing them directly on GitHub), the right place to add it would be in generate_colab_notebooks.py, injecting it as the first cell in process_notebook. Then just make generate-colab-notebooks to regenerate.

@andreatgretel andreatgretel changed the title Add Open in Colab badges to tutorial notebooks docs: add Open in Colab badges to tutorial notebooks Mar 11, 2026
@andreatgretel
Copy link
Copy Markdown
Contributor

Hey @mvansegbroeck, I went ahead and pushed a commit that moves the badge injection into generate_colab_notebooks.py (as suggested in my earlier comment). Here's what changed:

  • Added a COLAB_BADGE_TEMPLATE constant and prepend it as the first cell in process_notebook()
  • Regenerated all 6 notebooks with make generate-colab-notebooks, so they're back in sync with the source files and now include the badge

I also updated the PR title to docs: ... so the semantic-pull-request check passes, and updated the description.

The badge URLs and markup are identical to what you had - just generated automatically now so they survive future regenerations.

@mvansegbroeck
Copy link
Copy Markdown
Contributor Author

Thanks @andreatgretel

@johnnygreco any concerns here? Having the Colab banners on the notebooks would be nice for users who find them through GitHub.

@johnnygreco
Copy link
Copy Markdown
Contributor

No concerns here!

mvansegbroeck and others added 3 commits March 13, 2026 17:30
Move the "Open in Colab" badge injection into the notebook generation
script so badges survive regeneration. The badge is prepended as the
first markdown cell in process_notebook.
nabinchha
nabinchha previously approved these changes Mar 13, 2026
@johnnygreco johnnygreco merged commit 02744d1 into main Mar 13, 2026
47 checks passed
@andreatgretel andreatgretel deleted the feat/colab-notebook-badges branch April 14, 2026 11:56
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.

4 participants