Transferring parameters between multipage apps while staying in the same session

Hi,

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.

1 Like

Are you not using a user authentication to enter/log-in on the app?

  • Why would every client/customer see all the clients/customers in the app?
  • What if customer “A” clicked the link for customer 'B"?

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.

2 Likes

Here is an example on how to handle the session state across multi-page with a page per customer. Maintain a session state indexed by user.

In the dashboard, you have customers and both have 1000 deposits.

In the selectbox you can choose which customer to check/update. Let’s choose alice and we got this.

Say alice makes a new deposit of 100.

Her deposit is now 1100.

Let’s go back to dashboard by pressing that dashboard button.

See alice’s deposit is kept by session state.

Codes

app.py [dashboard]

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

config.toml

[client]

showSidebarNavigation = false

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.