@nou7lh st.session_state has a very similar API to a normal python dictionary, so to get a specific entry you can either do: st.session_state["page_number"] (note that this will raise a KeyError if there is no page_number)
or st.session_state.get("page_number", 0) (this will either return the page_number, if itās been set, or return 0, the default.
I tried with st.session_state['page_number'] and it worked !
Here is the code Iām using :
import pandas as pd
import streamlit as st
# Number of entries per screen
N = 15
st.markdown("# Demonstrating use of Next button with Session State")
# A variable to keep track of which product we are currently displaying
if 'page_number' not in st.session_state:
st.session_state['page_number'] = 0
data = pd.read_csv("csv\cars.csv", delimiter=';', skiprows=[1])
last_page = len(data) // N
# Add a next button and a previous button
prev, _, next = st.columns([1, 10, 1])
if next.button("Next"):
if st.session_state['page_number'] + 1 > last_page:
st.session_state['page_number'] = 0
else:
st.session_state['page_number'] += 1
if prev.button("Previous"):
if st.session_state['page_number'] - 1 < 0:
st.session_state['page_number'] = last_page
else:
st.session_state['page_number'] -= 1
# Get start and end indices of the next page of the dataframe
start_idx = st.session_state['page_number'] * N
end_idx = (1 + st.session_state['page_number']) * N
# Index into the sub dataframe
sub_df = data.iloc[start_idx:end_idx]
st.write(sub_df)
With st.session_state.get("page_number", 0) instead, I donāt know why I kept getting errors like this one :
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking āAccept allā, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.