Question about project-structure

my projects consist of severeal folders, with a main.py file running the programm and calling different functions in those folders.
folder

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?

Hi @Eric,

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:

Below is an example of the directory structure of the example multipage app.

β”œβ”€ .streamlit
β”‚  └─ config.toml
β”œβ”€ pages
β”‚  β”œβ”€ 1_🍎_Apples.py
β”‚  β”œβ”€ 2_🍊_Oranges.py
β”‚  └─ 3_🍐_Pear.py
β”œβ”€ tools
β”‚  └─ utility.py
β”œβ”€ Home.py
β”œβ”€ README.md
β”œβ”€ packages.txt
β”œβ”€ requirements.txt
└─ utilities.py

Resources to check out about Multipage apps:

Sorry, but i don’t understand how this structure is going to prevent rerunning my functionalities.

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

1 Like

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!

1 Like

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