Skip to content

[feat] Add default param to st.tabs#12313

Merged
sfc-gh-bnisco merged 8 commits intodevelopfrom
bnisco/st-tabs-default-tab
Aug 26, 2025
Merged

[feat] Add default param to st.tabs#12313
sfc-gh-bnisco merged 8 commits intodevelopfrom
bnisco/st-tabs-default-tab

Conversation

@sfc-gh-bnisco
Copy link
Copy Markdown
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco commented Aug 25, 2025

Describe your changes

GitHub Issue Link (if applicable)

Fixes #10910

Testing Plan

  • ✅ Adds Unit Tests - JS and Python

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 Aug 25, 2025

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

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@sfc-gh-bnisco sfc-gh-bnisco added security-assessment-completed change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Aug 25, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 25, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-12313/streamlit-1.48.1-py3-none-any.whl
🕹️ Preview app pr-12313.streamlit.app (☁️ Deploy here if not accessible)

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot August 25, 2025 19:24
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

This PR adds a default parameter to the st.tabs function, allowing users to specify which tab should be initially selected. The implementation includes validation for duplicate tab names and ensures the correct tab index is used when duplicate labels exist.

Key Changes

  • Added default parameter to st.tabs API with validation logic
  • Updated frontend components to use the default tab index from the backend
  • Refactored layout handling to properly extract layout-relevant fields from block submessages

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
proto/streamlit/proto/Block.proto Added default_tab_index field to TabContainer message
lib/streamlit/elements/layouts.py Implemented default parameter validation and index calculation
lib/tests/streamlit/elements/layouts_test.py Added comprehensive test coverage for default tab functionality
frontend/lib/src/components/elements/Tabs/Tabs.tsx Updated React component to use default tab index from backend
frontend/lib/src/components/elements/Tabs/Tabs.test.tsx Added frontend tests for default tab behavior
frontend/lib/src/components/core/Block/Block.tsx Refactored layout field extraction to handle TabContainer blocks properly

@sfc-gh-bnisco sfc-gh-bnisco marked this pull request as ready for review August 25, 2025 19:27
@sfc-gh-bnisco sfc-gh-bnisco requested a review from a team as a code owner August 25, 2025 19:27
)
const [activeTabKey, setActiveTabKey] = useState<React.Key>(defaultTabIndex)
const [activeTabName, setActiveTabName] = useState<string>(() => {
const tab = node.children[defaultTabIndex] as BlockNode
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.

Array bounds error: node.children[defaultTabIndex] can cause runtime crash if defaultTabIndex is >= node.children.length. The defaultTabIndex comes from the backend and could be out of bounds if tabs are modified between renders or if there's a mismatch between frontend and backend state. Add bounds checking: const tab = node.children[defaultTabIndex] ?? node.children[0]

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

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.

Is it guaranteed that node.children is always fully loaded with the entire list of tabs here or do we need a fallback here?

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.

I don't know if it's guaranteed that node.children will always be fully loaded. With that said, we have a fallback in the following line, so this should be safe.

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.

But if the defaultTabIndex is higher than the number of children (e.g. on initial load), wouldn't this cause an array bounds error here?

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.

Good question, but it will return undefined, not throw an error.

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 👍

)
const [activeTabKey, setActiveTabKey] = useState<React.Key>(defaultTabIndex)
const [activeTabName, setActiveTabName] = useState<string>(() => {
const tab = node.children[defaultTabIndex] as BlockNode
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.

Is it guaranteed that node.children is always fully loaded with the entire list of tabs here or do we need a fallback here?

@sfc-gh-bnisco sfc-gh-bnisco merged commit 4c8f18a into develop Aug 26, 2025
36 checks passed
@sfc-gh-bnisco sfc-gh-bnisco deleted the bnisco/st-tabs-default-tab branch August 26, 2025 15:54
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.

Set a default tab in st.tabs

5 participants