Hi everyone,
I’m building a Streamlit app that connects to a PostgreSQL database and uses @st.cache_resource to cache the database connection. The app displays data across multiple tabs, with various filters applied to each data table (e.g., by Client ID, Name, Date Range, etc.).
Here’s my issue: when multiple users access the app simultaneously, the filter selections don’t work independently. If one user applies filters, it seems to override the filters for others, resetting filter selections for other users.
Has anyone faced a similar issue or can offer best practices for handling this?
I have seen some people facing issues like this due to bugs in their code. Best practice is avoiding mixing application-scoped state and session-scoped state, which is in itself quite a broad topic. I don’t think more specific advice can be provided based on your high level description.
def get_clients():
clients_dao = ClientsDAO()
data = clients_dao.get_clients()
return data
def load_clients():
clients = get_clients()
client_data_to_skip = {"outbound_file_types", "inbound_file_types", "transactions", "subscription"}
clients_data = [c.to_dict() for c in clients]
clients_data = [{k: v for k, v in data.items() if k not in client_data_to_skip} for data in clients_data]
clients_df = pd.DataFrame(clients_data)
clients_df["id"] = clients_df["id"].astype(int)
clients_df = clients_df.sort_values(by="created_at", ascending=False)
return clients_df, clients
I ran your code with my own implementation of ClientsDAO and was unable to reproduce the issue. The filters selected in different sessions remain separated, as well as the filtered data.
That is the scenario I tested. Well, unless I am misunderstanding what you mean by “two users filter at the same time” . Because filtering is a matter of a click, if you mean two users clicking the same widget within the same tenth of a second, then no. Should I?
Yes, at the same moment in time. When two users start working with a multiselect filter at the same time. User1 starts selecting items in the multiselect and at the same time user2 starts selecting. In my case, user2’s filters just reset from time to time
No way. It works as expected for me even when two users are changing the same multiselect at the same time. Furthermore, I don’t see how the code you posted can possibly cause the issue.
One thing that might make the widgets reset is changing the data while the app is being used, but the application doesn’t do that so it would be unrelated to simultaneous users of the application.