Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sfc-gh-bnisco
Copy link
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco commented Jun 6, 2025

Describe your changes

  • Removes a race condition for scrolling an element into view.
  • Writes a test and adds a new pytest marker for loading a page with a hash

GitHub Issue Link (if applicable)

Fixes #11551

Testing Plan

  • ✅ E2E Tests - Adds an e2e test

Contribution License Agreement

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

@sfc-gh-bnisco sfc-gh-bnisco added security-assessment-completed Security assessment has been completed for PR change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Jun 6, 2025
@snyk-io
Copy link
Contributor

snyk-io bot commented Jun 6, 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)

@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2025

✅ PR preview is ready!

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

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot June 6, 2025 21:17
Copy link
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 pull request fixes a race condition encountered when scrolling target elements into view and introduces a new end-to-end test to verify anchor scrolling behavior.

  • Refactors the StreamlitMarkdown component to remove the delayed scroll logic and instead use an immediate scroll call.
  • Adds a new pytest marker and an end-to-end test in Playwright to verify anchor scrolling functionality.
  • Updates the app fixture in the test configuration to support URL hash fragments.

Reviewed Changes

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

File Description
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx Refactors scroll behavior by removing an intermediate state and useEffect handler for scroll, directly scrolling the element when its ref is set.
e2e_playwright/st_markdown_test.py Introduces a new test case to ensure that anchor scrolling brings the target header into view.
e2e_playwright/conftest.py Modifies the app fixture to append a URL hash based on a pytest marker, enabling testing of hash-based navigation.
Comments suppressed due to low confidence (2)

frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx:223

  • Removing the delayed scroll mechanism may cause the element to be scrolled to before it is fully rendered, especially in edge cases. Please ensure that this immediate scroll behavior is verified across various page load timings.
scrollNodeIntoView(node)

e2e_playwright/conftest.py:308

  • Review the URL construction in the app fixture to ensure that appending the hash fragment does not lead to unintended formatting (such as an extra slash) which could affect routing. Consider clarifying this behavior with inline comments or tests.
response = page.goto(f"http://localhost:{app_port}/{hash_fragment}")

@sfc-gh-bnisco sfc-gh-bnisco marked this pull request as ready for review June 6, 2025 21:37
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

note: This was a bit of a drive-by change to better leverage JSX since I was already working in this file.

@sfc-gh-tteixeira
Copy link
Contributor

Awesome! Can you add a test that this works with MPAv2 as well?

Something like:

import streamlit as st


def page1():
    st.title("Page1")

    st.write("""
      Instructions:

      * Click the button below.
      * A new tab will open (that's OK)
      * The app in that tab should scroll down to 'My title 2' -- but it won't!
    """)
    st.link_button("Open new tab", "/page2#my-title-2")


def page2():
    st.title("Page2")

    st.header("My title 1")
    for _ in range(30):
        st.text("blah " * 100)

    st.header("My title 2")
    for _ in range(30):
        st.text("blah " * 100)


page = st.navigation(
    [
        st.Page(page1, title="Page1"),
        st.Page(page2, title="Page2"),
    ]
)

page.run()

@sfc-gh-bnisco
Copy link
Collaborator Author

@sfc-gh-tteixeira Good idea, just added a test for this!

Copy link
Contributor

@sfc-gh-tteixeira sfc-gh-tteixeira left a comment

Choose a reason for hiding this comment

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

I had no idea you could use ref that way!

@sfc-gh-bnisco sfc-gh-bnisco merged commit 7025fa9 into develop Jun 10, 2025
37 checks passed
@sfc-gh-bnisco sfc-gh-bnisco deleted the bnisco/fix-11551 branch June 10, 2025 15:50
@sfc-gh-bnisco sfc-gh-bnisco self-assigned this Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Page doesn't scroll to anchor link on first load

4 participants