Untitled

 avatar
unknown
plain_text
5 months ago
1.4 kB
2
Indexable
import streamlit as st
import pandas as pd

# Sample data (replace with your DataFrame)
data = {'Column1': range(1, 101), 'Column2': range(101, 201)}
df = pd.DataFrame(data)

# Set the number of rows per page
rows_per_page = 10

# Calculate the total number of pages
total_pages = len(df) // rows_per_page + (1 if len(df) % rows_per_page > 0 else 0)

# Get the current page number from Streamlit's session state, default to 1
if 'current_page' not in st.session_state:
    st.session_state.current_page = 1

# Define the pagination control
def next_page():
    if st.session_state.current_page < total_pages:
        st.session_state.current_page += 1

def prev_page():
    if st.session_state.current_page > 1:
        st.session_state.current_page -= 1

# Calculate the starting and ending index of the current page
start_idx = (st.session_state.current_page - 1) * rows_per_page
end_idx = start_idx + rows_per_page

# Display the current page of the DataFrame
st.write(f"Page {st.session_state.current_page} of {total_pages}")
st.table(df.iloc[start_idx:end_idx])

# Create two columns for "Previous" and "Next" buttons, side by side
col1, col2 = st.columns([1, 1])

# Place buttons in the columns
with col1:
    st.button("Previous", on_click=prev_page)
with col2:
    st.button("Next", on_click=next_page)
Editor is loading...
Leave a Comment