Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ When the draft PR is created:
The workflow uses:
- **Python**: 3.11
- **Permissions**: `contents: write`, `pull-requests: write`
- **Action**: `peter-evans/create-pull-request@v6`
- **Action**: `peter-evans/create-pull-request@v8`

**Authentication and `docs-code-eval`**

The automatic `GITHUB_TOKEN` is scoped to this repository (`wandb/docs`) only. It does not grant read access to other private repositories in the org, so it cannot replace a PAT for cloning `wandb/docs-code-eval` when that repo is private.

To sync from a **private** `docs-code-eval`, add a repository secret named `DOCS_CODE_EVAL_READ_PAT`: a fine-grained personal access token (or classic PAT) with **Contents** read access to `wandb/docs-code-eval`. The sync script uses it only for `git clone`. If the secret is not set and `docs-code-eval` is public, the workflow clones without a token.

**Clone fails with `could not read Username for 'https://github.com'`**

That usually means Git tried to prompt for credentials (no TTY in Actions) or a credential helper failed. The sync script clears `credential.helper` for the clone and uses HTTPS with `x-access-token` when `DOCS_CODE_EVAL_READ_PAT` is set. If the log shows **anonymous** clone but the repo is private, the secret is missing, misnamed, or not available to this workflow (for example some fork contexts). Re-check the secret name `DOCS_CODE_EVAL_READ_PAT` and that the job log line above the clone says **PAT over HTTPS**.

### Troubleshooting

Expand Down
16 changes: 14 additions & 2 deletions .github/workflows/sync-code-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@ jobs:
- name: Checkout docs repository
uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}

# Do not persist GITHUB_TOKEN as http.https://github.com/.extraheader:
# a later `git clone` of docs-code-eval would reuse it and often exit 128.
persist-credentials: false

# Do not use actions/checkout for wandb/docs-code-eval. GITHUB_TOKEN is scoped
# to this repository only, so the REST get-repository call for another repo returns
# Not Found for a private docs-code-eval. Clone in the script instead.
#
# For a private docs-code-eval, add repository secret DOCS_CODE_EVAL_READ_PAT
# (PAT with contents:read on wandb/docs-code-eval). Optional if the eval repo is public.

- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'

- name: Run sync script
env:
DOCS_CODE_EVAL_READ_TOKEN: ${{ secrets.DOCS_CODE_EVAL_READ_PAT }}
GIT_TERMINAL_PROMPT: "0"
run: |
chmod +x scripts/sync_code_examples.sh
./scripts/sync_code_examples.sh
Expand Down
30 changes: 30 additions & 0 deletions models/ref/sdk-coding-cheat-sheet/logging.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,60 @@ title: Logging
description: Log metrics, hyperparameters, tables, and custom data to W&B.
---

import LogCsvFiles from '/snippets/en/_includes/code-examples/log_csv_files.mdx';
import LogCustomSummaryMetric from '/snippets/en/_includes/code-examples/log_custom_summary_metric.mdx';
import LogCustomSummatyMetric from '/snippets/en/_includes/code-examples/log_custom_summaty_metric.mdx';
import LogExistingArtifactLinkCollection from '/snippets/en/_includes/code-examples/log_existing_artifact_link_collection.mdx';
import LogHistogramPlot from '/snippets/en/_includes/code-examples/log_histogram_plot.mdx';
import LogHyperparameter from '/snippets/en/_includes/code-examples/log_hyperparameter.mdx';
import LogLinePlot from '/snippets/en/_includes/code-examples/log_line_plot.mdx';
import LogMetric from '/snippets/en/_includes/code-examples/log_metric.mdx';
import LogScatterPlot from '/snippets/en/_includes/code-examples/log_scatter_plot.mdx';
import LogSummaryMetric from '/snippets/en/_includes/code-examples/log_summary_metric.mdx';
import LogTable from '/snippets/en/_includes/code-examples/log_table.mdx';

Log metrics, hyperparameters, tables, and custom data to W&B.

## Log a CSV file to as a table

<LogCsvFiles />

## Log a custom summary metric

<LogCustomSummaryMetric />

## Log a custom summary metric

<LogCustomSummatyMetric />

## Download and log an existing artifact from a registry collection

<LogExistingArtifactLinkCollection />

## Log a histogram plot

<LogHistogramPlot />

## Initialize a run and log hyperparameters

<LogHyperparameter />

## Log a line plot

<LogLinePlot />

## Initialize a run and log a metric

<LogMetric />

## Log a scatter plot

<LogScatterPlot />

## Explicitly log a summary metric

<LogSummaryMetric />

## Log a table

<LogTable />
Expand Down
25 changes: 25 additions & 0 deletions models/ref/sdk-coding-cheat-sheet/runs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ description: Initialize and manage W&B runs to organize your experiments and tra
import ExperimentsCreate from '/snippets/en/_includes/code-examples/experiments_create.mdx';
import RunFork from '/snippets/en/_includes/code-examples/run_fork.mdx';
import RunInit from '/snippets/en/_includes/code-examples/run_init.mdx';
import RunsAddTags from '/snippets/en/_includes/code-examples/runs_add_tags.mdx';
import RunsGroup from '/snippets/en/_includes/code-examples/runs_group.mdx';
import RunsOrganizeByType from '/snippets/en/_includes/code-examples/runs_organize_by_type.mdx';
import RunsUpdateTag from '/snippets/en/_includes/code-examples/runs_update_tag.mdx';
import RunsUpdateTagPublicApi from '/snippets/en/_includes/code-examples/runs_update_tag_public_api.mdx';

Initialize and manage W&B runs to organize your experiments and track your work.

Expand All @@ -21,3 +26,23 @@ Initialize and manage W&B runs to organize your experiments and track your work.

<RunInit />

## Add one or more tags to a run

<RunsAddTags />

## Add one or more runs to a group

<RunsGroup />

## Organize runs by their job type

<RunsOrganizeByType />

## Add one or more tags to an active run

<RunsUpdateTag />

## Add one or more tags to previously saved runs

<RunsUpdateTagPublicApi />

38 changes: 30 additions & 8 deletions scripts/sync_code_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,41 @@ set -e # Exit on error
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DOCS_ROOT="$(dirname "$SCRIPT_DIR")"
SUBMODULE_PATH="$DOCS_ROOT/.temp_code_eval"
EVAL_REPO_URL="https://github.com/wandb/docs-code-eval.git"

echo "🔄 Syncing code examples from docs-code-eval..."

# Clean up any existing temp submodule
if [ -d "$SUBMODULE_PATH" ]; then
echo " Removing existing temporary directory..."
rm -rf "$SUBMODULE_PATH"
# Optional: fine-grained or classic PAT with contents:read on wandb/docs-code-eval.
# Set by CI via secrets (see sync-code-examples workflow). Unset = anonymous clone (public repo only).
if [ -n "${DOCS_CODE_EVAL_READ_TOKEN:-}" ]; then
EVAL_CLONE_URL="https://x-access-token:${DOCS_CODE_EVAL_READ_TOKEN}@github.com/wandb/docs-code-eval.git"
else
EVAL_CLONE_URL="https://github.com/wandb/docs-code-eval.git"
fi

# Clone the repo (not as submodule, just a temp clone)
echo " Cloning docs-code-eval repository..."
git clone --depth 1 "$EVAL_REPO_URL" "$SUBMODULE_PATH" --quiet
# Prefer an existing .temp_code_eval (e.g. local prep). In CI we clone after docs
# checkout with persist-credentials false; see sync-code-examples workflow comments.
if [ -d "$SUBMODULE_PATH/ground_truth" ] && compgen -G "$SUBMODULE_PATH/ground_truth/"*.py > /dev/null; then
echo " Using existing docs-code-eval checkout at $SUBMODULE_PATH"
else
if [ -d "$SUBMODULE_PATH" ]; then
echo " Removing existing temporary directory..."
rm -rf "$SUBMODULE_PATH"
fi
echo " Cloning docs-code-eval repository..."
if [ -n "${DOCS_CODE_EVAL_READ_TOKEN:-}" ]; then
echo " (HTTPS with PAT from DOCS_CODE_EVAL_READ_PAT)"
else
echo " (anonymous HTTPS; set secret DOCS_CODE_EVAL_READ_PAT if the eval repo is private)"
fi
# - Clear extraheader so a stale Actions token does not override URL credentials.
# - credential.helper= stops the runner's helper from prompting (CI has no TTY; you
# may otherwise see "could not read Username" / "No such device or address").
export GIT_TERMINAL_PROMPT=0
git \
-c http.https://github.com/.extraheader= \
-c credential.helper= \
clone --depth 1 "$EVAL_CLONE_URL" "$SUBMODULE_PATH" --quiet
fi

# Copy Python files and create MDX wrappers
echo " Copying Python code examples and creating MDX wrappers..."
Expand Down
40 changes: 38 additions & 2 deletions snippets/CodeSnippet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* AUTO-GENERATED: Do not edit manually. Run sync_code_examples.sh to regenerate.
*/

import React from 'react';

// Import all MDX-wrapped code examples
import ArtifactAddAlias from '/snippets/en/_includes/code-examples/artifact_add_alias.mdx';
import ArtifactAddAliasExisting from '/snippets/en/_includes/code-examples/artifact_add_alias_existing.mdx';
Expand All @@ -28,10 +30,16 @@ import ArtifactUpdate from '/snippets/en/_includes/code-examples/artifact_update
import ArtifactUpdateExisting from '/snippets/en/_includes/code-examples/artifact_update_existing.mdx';
import ArtifactsDownload from '/snippets/en/_includes/code-examples/artifacts_download.mdx';
import ExperimentsCreate from '/snippets/en/_includes/code-examples/experiments_create.mdx';
import LogCsvFiles from '/snippets/en/_includes/code-examples/log_csv_files.mdx';
import LogCustomSummaryMetric from '/snippets/en/_includes/code-examples/log_custom_summary_metric.mdx';
import LogCustomSummatyMetric from '/snippets/en/_includes/code-examples/log_custom_summaty_metric.mdx';
import LogExistingArtifactLinkCollection from '/snippets/en/_includes/code-examples/log_existing_artifact_link_collection.mdx';
import LogHistogramPlot from '/snippets/en/_includes/code-examples/log_histogram_plot.mdx';
import LogHyperparameter from '/snippets/en/_includes/code-examples/log_hyperparameter.mdx';
import LogLinePlot from '/snippets/en/_includes/code-examples/log_line_plot.mdx';
import LogMetric from '/snippets/en/_includes/code-examples/log_metric.mdx';
import LogScatterPlot from '/snippets/en/_includes/code-examples/log_scatter_plot.mdx';
import LogSummaryMetric from '/snippets/en/_includes/code-examples/log_summary_metric.mdx';
import LogTable from '/snippets/en/_includes/code-examples/log_table.mdx';
import RegistryAddAnnotation from '/snippets/en/_includes/code-examples/registry_add_annotation.mdx';
import RegistryCollectionCreate from '/snippets/en/_includes/code-examples/registry_collection_create.mdx';
Expand All @@ -41,8 +49,19 @@ import RegistryCreate from '/snippets/en/_includes/code-examples/registry_create
import RegistryDelete from '/snippets/en/_includes/code-examples/registry_delete.mdx';
import RegistryLinkArtifactExisting from '/snippets/en/_includes/code-examples/registry_link_artifact_existing.mdx';
import RegistryUseLinkedArtifact from '/snippets/en/_includes/code-examples/registry_use_linked_artifact.mdx';
import ResumeRun from '/snippets/en/_includes/code-examples/resume_run.mdx';
import RewindRun from '/snippets/en/_includes/code-examples/rewind_run.mdx';
import RunFork from '/snippets/en/_includes/code-examples/run_fork.mdx';
import RunInit from '/snippets/en/_includes/code-examples/run_init.mdx';
import RunsAddTags from '/snippets/en/_includes/code-examples/runs_add_tags.mdx';
import RunsGroup from '/snippets/en/_includes/code-examples/runs_group.mdx';
import RunsOrganizeByType from '/snippets/en/_includes/code-examples/runs_organize_by_type.mdx';
import RunsUpdateTag from '/snippets/en/_includes/code-examples/runs_update_tag.mdx';
import RunsUpdateTagPublicApi from '/snippets/en/_includes/code-examples/runs_update_tag_public_api.mdx';
import SweepConfig from '/snippets/en/_includes/code-examples/sweep_config.mdx';
import SweepCreate from '/snippets/en/_includes/code-examples/sweep_create.mdx';
import SweepInitialize from '/snippets/en/_includes/code-examples/sweep_initialize.mdx';
import SweepStart from '/snippets/en/_includes/code-examples/sweep_start.mdx';

// Map filenames to imported content
const snippets = {
Expand All @@ -61,10 +80,16 @@ const snippets = {
'artifact_update_existing.py': ArtifactUpdateExisting,
'artifacts_download.py': ArtifactsDownload,
'experiments_create.py': ExperimentsCreate,
'log_csv_files.py': LogCsvFiles,
'log_custom_summary_metric.py': LogCustomSummaryMetric,
'log_custom_summaty_metric.py': LogCustomSummatyMetric,
'log_existing_artifact_link_collection.py': LogExistingArtifactLinkCollection,
'log_histogram_plot.py': LogHistogramPlot,
'log_hyperparameter.py': LogHyperparameter,
'log_line_plot.py': LogLinePlot,
'log_metric.py': LogMetric,
'log_scatter_plot.py': LogScatterPlot,
'log_summary_metric.py': LogSummaryMetric,
'log_table.py': LogTable,
'registry_add_annotation.py': RegistryAddAnnotation,
'registry_collection_create.py': RegistryCollectionCreate,
Expand All @@ -74,21 +99,32 @@ const snippets = {
'registry_delete.py': RegistryDelete,
'registry_link_artifact_existing.py': RegistryLinkArtifactExisting,
'registry_use_linked_artifact.py': RegistryUseLinkedArtifact,
'resume_run.py': ResumeRun,
'rewind_run.py': RewindRun,
'run_fork.py': RunFork,
'run_init.py': RunInit,
'runs_add_tags.py': RunsAddTags,
'runs_group.py': RunsGroup,
'runs_organize_by_type.py': RunsOrganizeByType,
'runs_update_tag.py': RunsUpdateTag,
'runs_update_tag_public_api.py': RunsUpdateTagPublicApi,
'sweep_config.py': SweepConfig,
'sweep_create.py': SweepCreate,
'sweep_initialize.py': SweepInitialize,
'sweep_start.py': SweepStart,
};

export const CodeSnippet = ({ file }) => {
const Component = snippets[file];

if (!Component) {
return (
<div style={{ padding: '1rem', background: '#fee', border: '1px solid #fcc', borderRadius: '4px' }}>
<p style={{ margin: 0, color: '#c00' }}>Code snippet not found: {file}</p>
</div>
);
}

return <Component />;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

with wandb.init(project="<project>") as run:
# Log the artifact with aliases
run.log_artifact(artifact, aliases=["<alias1>", "<alias2>"])
run.log_artifact(artifact, aliases=["<alias1>", "<alias2>"])
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# Retrieve an existing artifact and add an alias to it
artifact = wandb.Api().artifact("entity/project/artifact:version")
artifact.aliases = ["<new-alias>"]
artifact.save()
artifact.save()
2 changes: 1 addition & 1 deletion snippets/en/_includes/code-examples/artifact_add_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

# Log the artifact with tags
with wandb.init(project="<project>") as run:
run.log_artifact(artifact, tags=["<tag1>", "<tag2>"])
run.log_artifact(artifact, tags=["<tag1>", "<tag2>"])
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# Retrieve an existing artifact and add a tag to it
artifact = wandb.Api().artifact("entity/project/artifact:version")
artifact.tags = ["new-tag"]
artifact.save()
artifact.save()
2 changes: 1 addition & 1 deletion snippets/en/_includes/code-examples/artifact_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
# Training and logging code goes here

# Example of logging an artifact
run.log_artifact(artifact)
run.log_artifact(artifact)
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
with wandb.init(project="<project>") as run:

# Log the artifact's metadata
run.log_artifact(artifact)
run.log_artifact(artifact)
2 changes: 1 addition & 1 deletion snippets/en/_includes/code-examples/artifact_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
# Index the last two characters of the artifact version name (str) that
# consists of the version number
if artifact_version.name[-2:] == "v"+ "<version_number>":
artifact_version.delete(delete_aliases=True)
artifact_version.delete(delete_aliases=True)
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
type_name="<artifact_type>"
)

collection.delete()
collection.delete()
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
artifact = run.use_artifact("<artifact_name>:<alias>")

# Download a specific file or sub-folder
artifact.download(path_prefix="<file_name>") # downloads only the specified file or folder
artifact.download(path_prefix="<file_name>") # downloads only the specified file or folder
2 changes: 1 addition & 1 deletion snippets/en/_includes/code-examples/artifact_ttl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

with wandb.init(project="<project>", entity="<entity>") as run:
# Log the artifact with TTL
run.log_artifact(artifact)
run.log_artifact(artifact)
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
artifact.ttl = timedelta(days=365) # Delete in one year

# Save the updated artifact
artifact.save()
artifact.save()
2 changes: 1 addition & 1 deletion snippets/en/_includes/code-examples/artifact_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# Update the artifact's description
artifact.description = "<description>"
# Save the updated artifact
artifact.save()
artifact.save()
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
artifact.aliases = ["replaced"]

# Persist all artifact modifications
artifact.save()
artifact.save()
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
entry = artifact.get_entry("<file_name>")

# Download the entire artifact
datadir = artifact.download()
datadir = artifact.download()
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# Initialize a W&B run
with wandb.init(project="<project>") as run:
# Experiment code goes here
pass
pass
Loading