Skip to content

Update code-saving notebook to use run.log_code#636

Open
johndmulhausen wants to merge 1 commit into
mainfrom
fix/issue-591-save-code
Open

Update code-saving notebook to use run.log_code#636
johndmulhausen wants to merge 1 commit into
mainfrom
fix/issue-591-save-code

Conversation

@johndmulhausen

Copy link
Copy Markdown
Contributor

Why

The Saving_Code_with_W&B.ipynb colab demonstrated code saving solely through wandb.init(..., save_code=True), telling readers "That's it!". As reported in #591, running the notebook today does not produce the {} code panel on the run page: save_code=True alone only enables W&B's automatic capture paths, and in notebook environments (Colab, Jupyter, VS Code) those paths depend on W&B being able to locate the notebook file, which is not reliable across platforms (see the same snippet failing on some platforms in this W&B community thread).

Current W&B docs recommend explicitly calling run.log_code(), which uploads a versioned code-type artifact that populates the code viewer:

What changed

  • The "with code" example now calls run.log_code(include_fn=lambda path: path.endswith(".py") or path.endswith(".ipynb")) before run.finish(), keeping save_code=True in wandb.init.
  • The example is split into an init cell and a train/log_code/finish cell, so cells execute while the run is active and W&B's notebook hook can stage a copy of the notebook for run.log_code to upload (this is what makes capture work on Colab, where the notebook file is not on disk).
  • Rewrote the "just pass save_code=True... That's it!" markdown to describe the two-step approach, and noted that scripts can alternatively use save_code=True with settings=wandb.Settings(code_dir=".") (per the SDK, code_dir only takes effect when save_code is enabled).
  • Updated both training loops to the current run.log/run.config API for consistency; fixed a "notebbok" typo. Screenshots are unchanged and still accurate: the second run still produces the {} panel, the first still does not.

Verification (offline, wandb 0.26.1)

Extracted the notebook's code cells into a script and ran the full flow with WANDB_MODE=offline, then inspected the offline run directories and .wandb transaction logs:

  • Old approach (save_code=True only): run files contain a copy of the main script under files/code/, but zero artifact records - nothing to populate the current {} code viewer, and in notebook environments even the main-file copy depends on the flaky notebook-discovery path.
  • Updated approach: same run files, plus a code-type artifact (source-code_save-...) whose manifest contains both the .py file and the .ipynb file picked up by the include_fn.
  • The save_code=False run logs no code in either form, matching the notebook's "without code" section.

All 17 cells validate with nbformat, and every code cell parses with ast.parse. Edits were made as targeted string edits on the raw JSON, so the notebook formatting is untouched for the post-approval auto-clean workflow.

Fixes #591

🤖 Generated with Claude Code

The notebook relied solely on wandb.init(save_code=True), which no
longer reliably populates the {} code panel on the run page,
especially from Colab and Jupyter (#591). Add an explicit
run.log_code() call, restructure the with-code example so the run
spans cells (letting the notebook hook stage a copy of the notebook
for upload), and update the surrounding prose to match current
documented behavior, including the wandb.Settings(code_dir=".")
option for scripts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 17, 2026 17:40
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the Saving_Code_with_W&B.ipynb Colab to reflect current W&B best practices for reliably populating the {} code viewer by explicitly uploading code via run.log_code(), addressing the reported notebook-platform inconsistency in #591.

Changes:

  • Reworked the “save code” example to use run.log_code(include_fn=...) (while keeping save_code=True) and explicitly run.finish().
  • Split the “with code” example into separate cells so the run remains active across cell execution in notebook environments.
  • Updated metric logging to use run.log / run.config consistently and fixed a “notebbok” typo.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@johndmulhausen
johndmulhausen marked this pull request as ready for review July 21, 2026 21:34
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.

Notebook appears outdated — {} code icon and saved code not visible

2 participants