Skip to content

Conversation

snehankekre
Copy link
Contributor

@snehankekre snehankekre commented Jun 13, 2023

Describe your changes

#5913 fixed sliders to adjust the label if it appears outside of the screen, and for ranges where there are two thumb values. That solution is 99% of the way there. But as #6297 demonstrates, it doesn't consider how the slider and select_slider widgets behave when the expander is expanded and collapsed multiple times.

My best guess of the observed behavior: When the expander is collapsed, the sliders are hidden and their layout may be invalidated. Upon re-expansion, the thumb values might be painted before the container sets its final width.

This PR fixes this by using setTimeout(() => { ... }, 0), which delays the execution of the thumbValueAlignment method until the sliders are fully rendered with their final widths. This ensures correct alignment and prevents the thumb values from overflowing in subsequent expander expansions.

GitHub Issue Link (if applicable)

Closes #6297.

Testing Plan

- TODO: e2e tests need to be updated. I need to figure out how to get cypress to expand -> collapse -> re-expand an st.expander with nested sliders before taking a snapshot.

  • Updated e2e tests for st_slider.spec.js, st_select_slider.spec.js, and st_select_slider.py.

Contribution License Agreement

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

@mayagbarnes
Copy link
Collaborator

Re: E2E Testing this fix:
You may have already seen that e2e/scripts/st_slider.py has an expander that starts in the open state so can just add a test like the below to the st_slider.spec.js file. For the select slider, you will need to edit the st_select_slider.py file to add an expander with nested select sliders and follow a similar pattern.

  it("realigns label values when expander re-opened", () => {
    // Closes the expander
    cy.get(".streamlit-expanderHeader").click();

    // Reopens the expander
    cy.get(".streamlit-expanderHeader").click();

    // matchImageSnapshot used to test initial thumb value positioning
    // theme change interactions allow for fix to error in initial position
    cy.getIndexed(".streamlit-expander", 0).matchImageSnapshot();
  });

Note: A snapshot test could prove flaky due to the timing inconsistency of the thumb realignment and how long it takes to capture the snapshot - if thats the case an alternative option is to test the CSS resulting from realignment - this seemed to work (fail without fix, pass with fix):

  it("realigns label values when expander re-opened", () => {
    // Closes the expander
    cy.get(".streamlit-expanderHeader").click();

    // Reopens the expander
    cy.get(".streamlit-expanderHeader").click();

    // Positioning error occurs on overflow of expander container   
    // which occurs when position left set to 0px
    cy.getIndexed(".StyledThumbValue", 5).should("not.have.css", "left", "0px")
  });

Let me know if you run into any issues, happy to pair! 😃

@snehankekre
Copy link
Contributor Author

Thanks, Maya! I was out sick later last week until today, so I didn't make any updates in the interim.

On 38c5608, I copied your option to test the CSS resulting from the realignment for both st.slider and st.select_slider. Thank you for that! It's probably better to go down the CSS change route than potentially introduce flakiness due to the timing of screenshots 👍

Please lmk if there's any other changes required for this PR, including more test coverage.

@@ -88,3 +88,7 @@ def on_change():
)
st.write("Value 8:", st.session_state.select_slider8)
st.write("Select slider changed:", "select_slider_changed" in st.session_state)

with st.expander("Expander", expanded=True):
r2 = st.slider("Label 9", 10000, 25000, [10000, 25000])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit:st.select_slider with relevant params vs. st.slider

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, good catch! Fixed on bffef9a

Copy link
Collaborator

@mayagbarnes mayagbarnes left a comment

Choose a reason for hiding this comment

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

@snehankekre just the one comment on e2e test fix, but otherwise LGTM 👍🏼

Also my fault for interpreting the TODO as a question vs your notes 😅

@snehankekre
Copy link
Contributor Author

All good! You've saved me a lot of time writing the tests 😸

@vdonato vdonato merged commit 8345e43 into develop Jun 26, 2023
@vdonato vdonato deleted the slider-right-thumb branch June 26, 2023 23:13
tconkling added a commit to tconkling/streamlit that referenced this pull request Jun 27, 2023
* develop:
  Minimum version dependency testing (streamlit#6802)
  Delay slider `thumbValueAlignment` to allow page layout to complete (streamlit#6828)
eric-skydio pushed a commit to eric-skydio/streamlit that referenced this pull request Dec 20, 2023
…treamlit#6828)

* Delay slider thumbValueAlignment to allow page layout to complete

* Update st.slider and st.select_slider e2e tests

* Update num of slider elements in select slider e2e

* use st.select_slider in select slider e2e

* Fix session state init error

* Update index on StyledThumbValue snapshot to 11
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Mar 22, 2024
…treamlit#6828)

* Delay slider thumbValueAlignment to allow page layout to complete

* Update st.slider and st.select_slider e2e tests

* Update num of slider elements in select slider e2e

* use st.select_slider in select slider e2e

* Fix session state init error

* Update index on StyledThumbValue snapshot to 11
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
…treamlit#6828)

* Delay slider thumbValueAlignment to allow page layout to complete

* Update st.slider and st.select_slider e2e tests

* Update num of slider elements in select slider e2e

* use st.select_slider in select slider e2e

* Fix session state init error

* Update index on StyledThumbValue snapshot to 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Right-side label of st.slider and st.select_slider overflows when inside st.expander
3 participants