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!