Skip to main content

A Streamlit custom component to allow one click scrolling to different locations on your page

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_scroll_to_top-0.0.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_scroll_to_top-0.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_scroll_to_top-0.0.2.tar.gz.

File metadata

  • Download URL: streamlit_scroll_to_top-0.0.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for streamlit_scroll_to_top-0.0.2.tar.gz
Algorithm Hash digest
SHA256 db0611f760477eeb4b06c114b2fc67869e0f1543b6635a6f7a447baf7a4fefe5
MD5 dc253906d587f3d5979f5ca17ce0b798
BLAKE2b-256 4cf6864639787f45db7e809bd3a878ffec0ef5b627f03c5a0d82cf2abb1e008b

See more details on using hashes here.

File details

Details for the file streamlit_scroll_to_top-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_scroll_to_top-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b022626ff0a013dd2d894228d7663fb38cdb22e1012b95b73ae9a417be5a58fd
MD5 b7b12d34535da6adaa4f66082d8784d3
BLAKE2b-256 33a97e28128ea3cfa955648b86ad1f5a9180eed355140c02a562250b9b26aee8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page