Question about project-structure

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

In one usecase the performs a database query and passes a pandas dataframe to Inside the streamlit is beeing called to present the data.

Now each time i filter the data in streamlit, the 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, and into it. As for various utility functions you can house it within a 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_🍎
β”‚  β”œβ”€ 2_🍊
β”‚  └─ 3_🍐
β”œβ”€ tools
β”‚  └─
β”œβ”€ packages.txt
β”œβ”€ requirements.txt

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