Skip to content

[DynamicContainers] Callback support for expanders#14008

Merged
sfc-gh-lwilby merged 1 commit intodevelopfrom
02-18-_dynamiccontainers_callback_support_for_expanders
Feb 24, 2026
Merged

[DynamicContainers] Callback support for expanders#14008
sfc-gh-lwilby merged 1 commit intodevelopfrom
02-18-_dynamiccontainers_callback_support_for_expanders

Conversation

@sfc-gh-lwilby
Copy link
Copy Markdown
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby commented Feb 18, 2026

Describe your changes

Adds callable on_change callback support to st.expander, extending the existing "ignore" / "rerun" string literals.

  • on_change now accepts a callable in addition to the existing string options
  • Optional args and kwargs parameters pass through to the callback
  • Requires a key when using a callable (consistent with existing expander behavior where key is required for "rerun")
  • The callback fires on any state change (both expand and collapse). Users can check st.session_state[key] inside the callback to distinguish between expand (True) and collapse (False) events.

GitHub Issue Link (if applicable)

Testing Plan

  • Unit Tests (Python) — lib/tests/streamlit/elements/layouts_test.py
  • E2E Tests — e2e_playwright/st_expander_test.py
  • Any manual testing needed?
    • Manually verified as well.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Feb 18, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 18, 2026

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-14008/streamlit-1.54.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-14008.streamlit.app (☁️ Deploy here if not accessible)

Copy link
Copy Markdown
Collaborator Author

sfc-gh-lwilby commented Feb 18, 2026

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 18, 2026

📉 Frontend coverage change detected

The frontend unit test (vitest) coverage has decreased by 0.0000%

  • Current PR: 87.4100% (14197 lines, 1787 missed)
  • Latest develop: 87.4100% (14197 lines, 1787 missed)

✅ Coverage change is within normal range.

📊 View detailed coverage comparison

@sfc-gh-lwilby sfc-gh-lwilby changed the base branch from 02-10-_dynamiccontainers_dynamic_expanders to graphite-base/14008 February 18, 2026 20:48
@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_expanders branch from b3c1e40 to 94a0eba Compare February 23, 2026 15:55
@sfc-gh-lwilby sfc-gh-lwilby changed the base branch from graphite-base/14008 to develop February 23, 2026 15:55
@sfc-gh-lwilby sfc-gh-lwilby added security-assessment-completed change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Feb 23, 2026
@sfc-gh-lwilby sfc-gh-lwilby marked this pull request as ready for review February 23, 2026 16:01
Copilot AI review requested due to automatic review settings February 23, 2026 16:01
Copy link
Copy Markdown
Contributor

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

Adds callable on_change callback support to st.expander, including passing args/kwargs and enforcing key when a callback is used.

Changes:

  • Extend st.expander(on_change=...) to accept a callable plus optional args/kwargs.
  • Add/expand unit, typing, and Playwright E2E coverage for callback behavior.
  • Update st.expander docstring to describe the new callback mode.

Reviewed changes

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

Show a summary per file
File Description
lib/streamlit/elements/layouts.py Adds callback + args/kwargs support to st.expander and updates validation/docs.
lib/tests/streamlit/elements/layouts_test.py Adds unit tests covering callback requirements, firing behavior, and args/kwargs passthrough.
lib/tests/streamlit/typing/expander_container_types.py Adds typing assertions for the new on_change callable and args/kwargs.
e2e_playwright/st_expander.py Adds an E2E demo app section exercising callback expanders.
e2e_playwright/st_expander_test.py Adds E2E tests verifying callback fires on toggle and receives args/kwargs.

Comment on lines +1082 to +1087
serializer=serde.serialize,
ctx=ctx,
value_type="bool_value",
on_change_handler=on_change if callable(on_change) else None,
args=args,
kwargs=kwargs,
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

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

args/kwargs are forwarded to register_widget even when on_change_handler is None (e.g., on_change="rerun"). This makes it look like args/kwargs are supported for non-callback modes and can create inconsistent state/config. Consider only passing args/kwargs when on_change is callable (otherwise pass None) and/or raising a StreamlitAPIException if args/kwargs are provided without a callable.

Copilot uses AI. Check for mistakes.
@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_expanders branch from 94a0eba to aaa79d8 Compare February 23, 2026 18:28
@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_expanders branch 2 times, most recently from ce0cc59 to adfae87 Compare February 23, 2026 20:30
Comment on lines +359 to +363
def test_expander_callback_with_args_kwargs(app: Page):
"""Test that a callback with args and kwargs receives them correctly."""
# Initially no result
expect(app.get_by_text("Callback args result:")).to_be_visible()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion: Consider doing a direct string match rather than a substring match here. I believe as written this will pass for both Callback args result: and Callback args result: hello-toggled-world.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Made this update to AGENTS.md about this as well #14102


if on_change not in {"ignore", "rerun"}:
if not callable(on_change) and on_change not in {"ignore", "rerun"}:
raise StreamlitValueError("on_change", ["'rerun'", "'ignore'"])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion: Consider adding "a callable" to the StreamlitValueError.

@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_expanders branch from adfae87 to 62f405e Compare February 24, 2026 07:49
@sfc-gh-lwilby sfc-gh-lwilby merged commit 5deeeff into develop Feb 24, 2026
44 of 46 checks passed
@sfc-gh-lwilby sfc-gh-lwilby deleted the 02-18-_dynamiccontainers_callback_support_for_expanders branch February 24, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants