Skip to content

[DynamicContainers] Callback support for tabs#14007

Merged
sfc-gh-lwilby merged 1 commit intodevelopfrom
02-18-_dynamiccontainers_callback_support_for_tabs
Feb 25, 2026
Merged

[DynamicContainers] Callback support for tabs#14007
sfc-gh-lwilby merged 1 commit intodevelopfrom
02-18-_dynamiccontainers_callback_support_for_tabs

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.tabs, extending the existing "ignore" / "rerun" / None 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
  • The callback fires when the user switches to a different tab. st.session_state[key] contains the label of the newly active tab, so the callback can branch on which tab was selected.

GitHub Issue Link (if applicable)

Testing Plan

  • Unit Tests (Python) — lib/tests/streamlit/elements/layouts_test.py
  • E2E Tests — e2e_playwright/st_tabs_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.

@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-14007/streamlit-1.54.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-14007.streamlit.app (☁️ Deploy here if not accessible)

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@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.

@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_tabs branch from 7c5b44b to 9aa118f Compare February 20, 2026 15:54
@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 force-pushed the 02-18-_dynamiccontainers_callback_support_for_tabs branch from 9aa118f to 9fa2210 Compare February 23, 2026 15:11
@sfc-gh-lwilby sfc-gh-lwilby marked this pull request as ready for review February 23, 2026 16:46
Copilot AI review requested due to automatic review settings February 23, 2026 16:46
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 callback support to st.tabs so apps can run user code on tab switches (with optional args/kwargs), while keeping the existing "ignore" / "rerun" / None behaviors intact.

Changes:

  • Extend st.tabs(..., on_change=...) to accept a callable, plus new args/kwargs parameters that forward to the callback.
  • Add Python unit tests covering proto/id wiring, .open behavior, callback invocation semantics, and args/kwargs forwarding.
  • Add typing tests and Playwright E2E coverage for callback behavior.

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 Extends st.tabs API + widget registration to support callable on_change with args/kwargs and updates docstring + validation.
lib/tests/streamlit/elements/layouts_test.py Adds unit tests for callback wiring, state transitions, and args/kwargs forwarding.
lib/tests/streamlit/typing/tab_container_types.py Updates typing tests to ensure callable on_change and args/kwargs type-check.
e2e_playwright/st_tabs.py Adds app scenarios exercising st.tabs callbacks (including args/kwargs).
e2e_playwright/st_tabs_test.py Adds E2E tests asserting callback execution on tab switches and updates tab component count.

Comment on lines +698 to +702
- A callable: A callback function to execute before rerunning the
app when tabs are switched. Enables state tracking (equivalent to
``"rerun"`` plus the callback). The callback receives no arguments
by default, but you can pass arguments using ``args`` and
``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.

The docstring note near the top of st.tabs still says to use on_change="rerun" to enable lazy execution, but this PR adds callable on_change with the same stateful/lazy behavior. Please update that note to also mention the callable option so users don't miss the new capability.

Copilot uses AI. Check for mistakes.
Comment on lines +704 to +705
args : tuple or None
An optional tuple of args to pass to the ``on_change`` callback.
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 is typed as WidgetArgs (tuple or list) across Streamlit widgets, and other widget docstrings document this as “list or tuple”. Here it’s documented as “tuple or None”, which is inconsistent with the actual accepted types. Either update the docstring to “list or tuple” (and keep the WidgetArgs type), or add runtime/type-level validation to only accept tuples.

Suggested change
args : tuple or None
An optional tuple of args to pass to the ``on_change`` callback.
args : list or tuple or None
An optional list or tuple of args to pass to the ``on_change`` callback.

Copilot uses AI. Check for mistakes.
@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_tabs branch 2 times, most recently from 5d5fe20 to b119d39 Compare February 23, 2026 20:17
Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Comment on lines +814 to +817
if not callable(on_change) and (args is not None or kwargs is not None):
raise StreamlitAPIException(
"`args` and `kwargs` can only be used when `on_change` is a callable."
)
Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch Feb 24, 2026

Choose a reason for hiding this comment

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

suggestion: I think it's slightly better here to just not throw the error because 1) for consistency, since I believe we are not doing it anywhere else, 2) with agents, it might be better to be a bit more forgiving.

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.

OK, that sounds good to me. This was a suggestion from one of the AI reviews but it also seemed a bit heavy to me.

@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_tabs branch 2 times, most recently from 8bae35c to 13f8f8f Compare February 25, 2026 07:11
@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 02-18-_dynamiccontainers_callback_support_for_tabs branch from 13f8f8f to d379ac7 Compare February 25, 2026 07:47
@sfc-gh-lwilby sfc-gh-lwilby merged commit c1ad756 into develop Feb 25, 2026
44 checks passed
@sfc-gh-lwilby sfc-gh-lwilby deleted the 02-18-_dynamiccontainers_callback_support_for_tabs branch February 25, 2026 09:06
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