I have a multipage app. One page is the dashboard where all my clients are shown by name, and the second one is a details page for an individual client.
On the dashobard page I want to show a link for each customer and when one clicks on it the desired behaviour is that the details page is opened for that customer.
Is it possible that when a page_link is clicked a “on_click” function is execuded that allows e.g. to write the customer id into the session state so it can be used to look up that customer.
I already got it working to pass the customer id as query parameters, but this opens a new session even when staying in the same tab. This new session causes a lot of cached stuff to rerun which sadly takes way to long for a smooth user experience.
Yes authentication happens and every user only can see what they are supposed to see. The app’s users are not the customers themselves though but advisors/account managers that naturally have to be able to access all of their customers on their dashboard page and jump to the details page that opens for the selected customer.
import streamlit as st
def user_page(user):
st.switch_page(f'./pages/{user}.py')
def main():
st.header('Dashboard')
users = ["", "alice", "bob"]
selected_user = st.selectbox("Choose a customer", users)
if selected_user != '':
user_page(selected_user)
st.subheader('Customers')
for user in users:
if user == '':
continue
if user not in st.session_state:
st.session_state[user] = {"user": user, "deposit": 1000}
st.write(st.session_state[user])
if __name__ == "__main__":
main()
./pages/alice.py
import streamlit as st
user = 'alice'
if user not in st.session_state:
st.session_state[user] = {"user": user, "deposit": 1000}
if st.button('Dashboard'):
st.switch_page('app.py')
st.header(f'Welcome {user.title()}')
new_depo = st.number_input('New deposit', value=0, step=1, min_value=0)
if new_depo > 0:
current_depo = new_depo + st.session_state[user]['deposit']
st.session_state[user].update({'deposit': current_depo})
st.write(st.session_state[user])
./pages/bob.py
This is similar to alice.py
import streamlit as st
user = 'bob'
if user not in st.session_state:
st.session_state[user] = {"user": user, "deposit": 1000}
if st.button('Dashboard'):
st.switch_page('app.py')
st.header(f'Welcome {user.title()}')
new_depo = st.number_input('New deposit', value=0, step=1, min_value=0)
if new_depo > 0:
current_depo = new_depo + st.session_state[user]['deposit']
st.session_state[user].update({'deposit': current_depo})
st.write(st.session_state[user])
Thanks for your answer but if I understand you right this still means that a separate .py file has to be created for every user which is not feasible for an unknown number of users (potentially 50 and more per advisor and there are several advisors who should also be able to add new users in the app). What I need is a generic user detail page that can then be initialised with the selected user’s data at runtime.
One way I figured out myself is that I create a button for each user and write the user id into the session state, but I was hoping that there were more elegant ways to do that, e.g. over a dropdown or markdown links.
Thinking about it, one of the best ways to do it could be giving an on_click parameter to st.page_link that allows running a callable to write everything required into the session state. But I guess that would be a feature request and has to be placed somewhere else, right?
You can create a generic details page, save the client’s ID on st.session_state and use st.switch_page to go to the details page. st.session_state should stay the same after you use st.switch_page, but you would need to write a bit more code to use and save url parameters.
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.