How to update page file without reloading modules?

Hi folks,

I am wondering is there a way that I can update page file without reloading other modules?

Letโ€™s say I have 2 files,

I do all the calculations in file and this takes some time (say 1min), and I import utils in file. I also edit UI in, and whenever I make some changes I would like to see the updated UI. So I save and click button of rerun in browser, but it will reload and I have to wait for 1 min.

Is there any way I can rerun only without reloading

Thank you.

1 Like

How can we help you without us seeing the code?

1 Like

Thanks for your reply. The code can be very simple.

import time
print("I am here!")

def my_calc():
    return 100

a = my_calc()

import streamlit as st
from utils import a

st.write("Main page")


The idea is all complex calculation is done in file, and the UI and simple calculation is done in file. I would like to edit file (say add some button, show numbers calculated from file, etc.) and quickly have a look at what the UI look like. But whenever I make updates, and click rerun button in browser, it will reload file and do all the calculations again and takes long time. Is there any way I can update file without reloading file?

1 Like

There are many reasons why doing heavy work when importing a module. You just found out one of them. What the best alternative is depends onโ€ฆ a lot of things that we donโ€™t know about your code.

A typical approach in streamlit apps, that may or may not work for your specific needs, may be just importing my_calc and call it from the main script only when needed.

Thanks for your reply. I thought it is a simple question that can be resolved by some trick (some config or button) I do not know. But it sounds like more complicated than I thought and it has to be resolved by some work to redesign my code. So I will give more details on my code.

My app needs to fetch data from some database and do some heavy calculation which takes long time (20 mins), and show the results in certain format. And the app needs to refresh the data and calculation and show new results every 2 hours. I know streamlit has cache_data decorator but it is not good as when a new round of fetching data and calculations start, the user needs to wait. So my design is I have a file that uses a second thread to do all fetching data and calculation work in the background (scheduled at every 2 hours) and store the results, and the file just needs to use the main thread to fetch stored calculation results from file and display it with some UI. In this way it just takes 20 mins when the app launch, and after that when the new round of fetching data and calculation starts, it will not intervene the file, and when the calculation finishes, the file will automatically fetch the updated results stored in file.

So my code is actually more complicated than above. The simplified version is like below.

class DBCache:
    def __init__(self, name): = name
        self.has_data = False = None

    def reset(self):
        data = generate_data()
        self.has_data = True = data

    def get_data(self):

my_cache = DBCache("my_cache")

## some codes to create a thread and schedule reset() function in my_cache object every 2 hours.

import streamlit as st
from utils import my_cache

data = my_cache.get_data()

## some codes to design UI and show data based on user inputs (some simple calculation with data)

So back to your suggestion, I cannot call reset function when needed because it has to be run first then I have the data to present in file. Ideally, if I update the file and I click rerun button in browser and it does not reload file, it can get stored results in object my_cache. But it seems the way it works is that streamlit will clear everything when I click rerun button and reload file, so I have to wait for 20 mins to get the results.

1 Like

Hi @xzhu43

It seems the app is a computationally intensive one that may require some additional architecting to have it run quickly.

I learned from @asehmi that such app may be built by separating the compute intensive part by leveraging a FastAPI backend and a Streamlit frontend

A relevant thread:

A tutorial blog:

1 Like

You need to understand the main concept of streamlit.

Thanks for your insights! Indeed, I can explicitly separate the computation part and UI part by calling API in streamlit app. What I did is kind of a simplified version of this archetecture but wrap both 2 parts within the app. I guess that is the solution if there is no simpler one.

I thought I can update file and rerun without reloading file bcause it actually worked in this way in my previous app. But when I started working on this new app and I found it did not work the same way even though I used the same python and streamlit version. I thought this feature is controlled by some config, but it looks like the way it worked in my previous app is an accident or bug although I do not know what caused it.

1 Like