Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

st.tabs & st.expander - Improve handling of frontend state/mount #8239

Open
mayagbarnes opened this issue Mar 5, 2024 · 6 comments · May be fixed by #8683
Open

st.tabs & st.expander - Improve handling of frontend state/mount #8239

mayagbarnes opened this issue Mar 5, 2024 · 6 comments · May be fixed by #8683
Labels
feature:st.expander feature:st.tabs type:enhancement Requests for feature enhancements or new features

Comments

@mayagbarnes
Copy link
Collaborator

mayagbarnes commented Mar 5, 2024

Summary

Improve the management/heuristics used to handle rendering the frontend st.tabs & st.expander React components.

Scenarios such as conditionally rendering elements before st.tabs or st.expander causes the unmount/remount of the component, therefore losing its FE state/selected tab. This is expected behavior based on the components in their current form, but it can be improved.

When we refactor st.tabs, we should also examine the heuristic we used to assess the selected tab:

When the number or names of the tabs change, we're now looking for the name of the active tab in the list of new tabs, and activating that one. This has the advantage that you can insert tabs and still keep the current tab active. But it comes at the cost of tabs switching when you simply rename them

Additional Context

See related issues:

More expansive feature request for FE/BE communication of tab state:

@mayagbarnes mayagbarnes added type:enhancement Requests for feature enhancements or new features feature:st.tabs labels Mar 5, 2024
Copy link

github-actions bot commented Mar 5, 2024

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@mayagbarnes mayagbarnes changed the title st.tabs - Improve handling of frontend state/mount st.tabs & st.expander - Improve handling of frontend state/mount Mar 5, 2024
@sfc-gh-pkommini
Copy link

sfc-gh-pkommini commented Mar 22, 2024

Hi Team,

I seem to have this same issue but it happens when I have this sort of a code pattern:

# This doesn't work and tabs get reset on button click
(
  tab1,
  tab2,
) = st.tabs([
  "Tab 1",
  "Tab 2",
])

def handle_on_click() -> None:
  if st.session_state.some_state:
    _ = update_records_in_db()

with tab1:
  clicked = st.button("Update Records", on_click=handle_on_click)

  if clicked:
    st.success("Records Updated!", icon="👍🏻")

with tab2:
  # some other code
  pass

The workaround I found for this particular issue is to modify the code like so:

# This works as expected
(
  tab1,
  tab2,
) = st.tabs([
  "Tab 1",
  "Tab 2",
])

with tab1:
  st.button("Update Records", key="my_button_key")

  if st.session_state.my_button_key:
    updated = update_records_in_db()
    st.success(f"`{updated}` records updated!", icon="👍🏻")

with tab2:
  # some other code
  pass

@abhisheksharma26jan
Copy link

+1 Having same issues

@solarleb
Copy link

Please fix this

@sfc-gh-pkommini
Copy link

Hi @mayagbarnes Is this going to be fixed in 1.34.0?

@sfc-gh-pkommini
Copy link

I still have drop downs in my tab 2 that when clicked cause the tabs to reset back to tab 1. Is this in the priority list?

@vdonato vdonato linked a pull request May 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:st.expander feature:st.tabs type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants