Skip to content

Conversation

Copy link

Copilot AI commented Nov 4, 2025

After portfolio analysis completes, the page scrolls to top due to Streamlit reruns, forcing users to manually scroll down to view results.

Changes

Added scroll-to-element behavior:

  • HTML anchor (<div id="portfolio-section"></div>) at portfolio section
  • Session state flag scroll_to_portfolio tracks when portfolio is first generated
  • JavaScript injection scrolls smoothly to anchor on completion, then resets flag

Implementation:

# Set flag when portfolio generates
st.session_state.generated_portfolio = portfolio
st.session_state.scroll_to_portfolio = True

# On render, inject scroll behavior if flag is set
if st.session_state.scroll_to_portfolio:
    st.markdown(f"""
        <script>
        setTimeout(function() {{
            document.getElementById('portfolio-section')
                .scrollIntoView({{ behavior: 'smooth', block: 'start' }});
        }}, {SCROLL_TO_PORTFOLIO_DELAY_MS});
        </script>
    """, unsafe_allow_html=True)
    st.session_state.scroll_to_portfolio = False

Configuration:

  • SCROLL_TO_PORTFOLIO_DELAY_MS = 100 - delay ensures Streamlit content renders before scroll
  • Flag resets on conversation clear and provider change

Testing

Added tests/unit/test_scroll_to_portfolio.py verifying constant definition, session state initialization, and JavaScript implementation.

Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] Page Scrolls to Top After Portfolio and PAC Analysis Completion</issue_title>
<issue_description>## Description
When the portfolio analysis and PAC metrics extraction are completed, the page automatically scrolls to the top, forcing users to manually scroll down to view the newly generated portfolio recommendations and analysis results. This creates a poor user experience as users lose their position on the page.

Reproducibility information

Branch: develop
This issue occurs consistently at the end of the financial assessment flow, after all questions have been answered and the portfolio analysis has been generated.

Expected behavior

After the portfolio and PAC metrics analysis completes, the page should:

  1. Either maintain the user's current scroll position
  2. Or smoothly scroll to the relevant section showing the newly generated portfolio recommendations
  3. Keep the user in context so they can immediately see the generated analysis without manual scrolling

Expected behavior: Minimal scroll movement or a smooth scroll to the analysis section, allowing users to immediately view their results.

Actual behavior

After portfolio analysis completion, the page scrolls all the way to the top (likely due to a st.rerun() call), and users must manually scroll down to see the generated portfolio, PAC metrics, and other analysis results. This interrupts the user flow and reduces usability.

Current behavior: Full page scroll to top, requiring significant manual scrolling to return to the analysis section.

Attachments

Steps to reproduce:

  1. Start the financial assessment
  2. Answer all 12 questions completely
  3. Wait for the portfolio generation to complete
  4. Observe the page jumping to the top of the page
  5. Note that you must scroll down to see the generated portfolio results

Browser/Environment:

  • Streamlit application
  • Tested on: macOS, Chrome browser

Further content

  • This issue likely occurs due to st.rerun() calls in app.py (lines around portfolio generation)
  • Consider using Streamlit's scroll behavior management or avoiding st.rerun() if possible
  • Alternative solutions:
    • Use st.session_state to preserve scroll position
    • Implement smooth scrolling to the analysis section using JavaScript injection
    • Use Streamlit's st.anchor() or similar anchor functionality to jump directly to results
    • Consider restructuring the layout to keep portfolio results in a more accessible location
  • The issue is particularly problematic for users on mobile devices where scrolling is more cumbersome
  • Test the fix across different screen sizes and devices
    </issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 4, 2025
Copilot AI and others added 4 commits November 4, 2025 10:03
- Add HTML anchor at portfolio section
- Track when portfolio is first generated with scroll_to_portfolio flag
- Inject JavaScript to smoothly scroll to portfolio section after generation
- Reset flag in clear conversation and change provider actions
- Apply black and isort formatting

Co-authored-by: merendamattia <[email protected]>
- Add SCROLL_TO_PORTFOLIO_DELAY_MS constant for configurable delay
- Add detailed comments explaining JavaScript injection necessity
- Document why delay is needed for Streamlit content rendering
- Address code review feedback on magic numbers and security concerns

Co-authored-by: merendamattia <[email protected]>
- Add tests to verify SCROLL_TO_PORTFOLIO_DELAY_MS constant
- Test session state initialization includes scroll_to_portfolio flag
- Verify JavaScript scroll implementation in code
- All tests pass successfully

Co-authored-by: merendamattia <[email protected]>
- Move imports to top of test file
- Use pathlib for robust path resolution
- Make regex patterns whitespace-insensitive for formatting flexibility
- Apply black formatting to test file
- All tests still pass successfully

Co-authored-by: merendamattia <[email protected]>
Copilot AI changed the title [WIP] Fix page scroll behavior after analysis completion Fix page scroll to top after portfolio generation completes Nov 4, 2025
Copilot AI requested a review from merendamattia November 4, 2025 10:14
@merendamattia merendamattia changed the title Fix page scroll to top after portfolio generation completes [WIP] Fix page scroll to top after portfolio generation completes Nov 5, 2025
@merendamattia merendamattia deleted the copilot/fix-scroll-top-issue branch December 22, 2025 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Page Scrolls to Top After Portfolio and PAC Analysis Completion

2 participants