Update code-saving notebook to use run.log_code#636
Open
johndmulhausen wants to merge 1 commit into
Open
Conversation
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>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Contributor
There was a problem hiding this comment.
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 keepingsave_code=True) and explicitlyrun.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.configconsistently and fixed a “notebbok” typo.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
johndmulhausen
marked this pull request as ready for review
July 21, 2026 21:34
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The
Saving_Code_with_W&B.ipynbcolab demonstrated code saving solely throughwandb.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=Truealone 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 versionedcode-type artifact that populates the code viewer:wandb.Run.log_code()" for fine-grained capture;wandb.Settings(code_dir=".")for automatic captureRun.log_codereferenceWhat changed
run.log_code(include_fn=lambda path: path.endswith(".py") or path.endswith(".ipynb"))beforerun.finish(), keepingsave_code=Trueinwandb.init.run.log_codeto upload (this is what makes capture work on Colab, where the notebook file is not on disk).save_code=True... That's it!" markdown to describe the two-step approach, and noted that scripts can alternatively usesave_code=Truewithsettings=wandb.Settings(code_dir=".")(per the SDK,code_dironly takes effect whensave_codeis enabled).run.log/run.configAPI 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.wandbtransaction logs:save_code=Trueonly): run files contain a copy of the main script underfiles/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.code-type artifact (source-code_save-...) whose manifest contains both the.pyfile and the.ipynbfile picked up by theinclude_fn.save_code=Falserun logs no code in either form, matching the notebook's "without code" section.All 17 cells validate with
nbformat, and every code cell parses withast.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