my projects consist of severeal folders, with a main.py file running the programm and calling different functions in those folders.
In one usecase the main.py performs a database query and passes a pandas dataframe to sentiment.py. Inside the sentiment.py streamlit is beeing called to present the data.
Now each time i filter the data in streamlit, the main.py runs again, causing another database-query.
What would be the best or easiest way to prevent this from happening?
Perhaps you could structure your app as a multipage app. This could be done by creating a directory called pages and moving cleanup.py, sentiment.py and sql.py into it. As for various utility functions you can house it within a utility.py and import them.
Iβve created a simple example of a multipage app that you can try out:
Sorry, I forgot to also mention that you should also look into st.session_state which allows variables to be shared between pages that in turn allows carrying per-session state across multiple pages.
Every app interaction (e.g. changing values of input widgets) would cause the app to re-run. To prevent that youβll have to use session state in your app.
Below are additional resources that you can look into which provides ample examples as well:
I think the most important thing you need to do is simply use st.experimental_memo to cache the results of your database query. st.experimental_memo - Streamlit Docs
I donβt see any obvious issues with your structure, but this is the classic reason to use st.experimental_memo β your script is going to re-run lots of times as you interact with the app, and you donβt want to rerun everything, so you put things like database queries inside of a function decorated with @st.experimental_memo
Thats what i was lokking for! I ttried the cache decorated before, thinking it would do the same, but never got it wokring. This works perfectly. Thanks!
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.