Skip to main content

streamlit-scroll-to-top

Streamlit component which provides a hacky workaround to utilise streamlit buttons, iframes and state management to redirect users to different points on a page.

Streamlit App

Buy Me A Coffee

How does it work?

The component works by creaing an invisible iframe in the location you want to scroll to and then tells the parent window to move the focus to that iframe. See the Example usage further below to get a further idea of how it works/how to implement it in your code. There are other ways to do this however I wanted a way I could easily use streamlit buttons & the session state to move the user to different locations.

This may stop working at any moment or indeed may not work on some browsers. I've tested on Edge, Chrome, Firefox & Opera and all appears to be OK

Features

  • Scroll to the top of a page (or any point) easily using existing Streamlit buttons.

Installation

pip install streamlit-scroll-to-top

Example Usage

Copy this code snippet:

import streamlit as st
from streamlit_scroll_to_top import scroll_to_here

# Step 1: Initialize scroll state in session_state
if 'scroll_to_top' not in st.session_state:
    st.session_state.scroll_to_top = False
    
if 'scroll_to_header' not in st.session_state:
    st.session_state.scroll_to_header = False

# Step 2: Handle the scroll-to-top action
if st.session_state.scroll_to_top:
    scroll_to_here(0, key='top')  # Scroll to the top of the page
    st.session_state.scroll_to_top = False  # Reset the state after scrolling

# Step 3: Define a scroll function to trigger the state change
def scroll():
    st.session_state.scroll_to_top = True
    
def scrollheader():
    st.session_state.scroll_to_header = True

# Step 4: Add some dummy content to simulate a long page
st.title("Dummy Content")
st.write("Scroll down to see the 'Scroll to Top' button.")
for i in range(50):  # Generate dummy content
    if i == 25:
        if st.session_state.scroll_to_header:
            scroll_to_here(0, key='header')  # Scroll to the top of the page
            st.session_state.scroll_to_header = False  # Reset the state after scrolling
        st.header("Or scroll here")
    st.text(f"Line {i + 1}: This is some dummy content.")

# Step 5: Add a button to trigger the scroll to top action. Both ways work... personal preference
st.button("Scroll to Top", on_click=scroll)
if st.button("Scroll to Top 2"):
    st.session_state.scroll_to_top = True
    st.rerun()
    
# Step 5: Add a button to trigger the scroll to header action. Both ways work... personal preference    
st.button("Scroll to Header", on_click=scrollheader)
if st.button("Scroll to Header 2"):
    st.session_state.scroll_to_header = True
    st.rerun()

Metadata

Release files for streamlit-scroll-to-top 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-scroll-to-top 0.0.4
File Size Uploaded
streamlit_scroll_to_top-0.0.4.tar.gz 402.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-scroll-to-top 0.0.4
File Interpreter ABI Platform
streamlit_scroll_to_top-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 2.7 MB

Release files / streamlit_scroll_to_top-0.0.4.tar.gz

Download URL streamlit_scroll_to_top-0.0.4.tar.gz
Size 402.9 kB
Tags Source
SHA-256 checksum
How to use checksums
148bbaa31ca91444a94d4b1f846bcdc0de5f6b2eccaf9e815805754892a9d7f6
BLAKE2b-256 checksum
How to use checksums
d20626f13270ea77f0fddec15b11955dad1c1130897c87f6a4c3040221051ef7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / streamlit_scroll_to_top-0.0.4-py3-none-any.whl

Download URL streamlit_scroll_to_top-0.0.4-py3-none-any.whl
Size 2.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
4e894ee1377fd0d65c799c39463d382d9a72fb9c592dde566ea3f6c109154128
BLAKE2b-256 checksum
How to use checksums
ce899099f3c5fab4b7ecef73a16bb80a59d6501d5b8ae904df162700460d15e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page