I am wondering is there a way that I can update page file without reloading other modules?
Let’s say I have 2 files,
main_page.py
utils.py
I do all the calculations in utils.py file and this takes some time (say 1min), and I import utils in main_page.py file. I also edit UI in main_page.py, and whenever I make some changes I would like to see the updated UI. So I save main_page.py and click button of rerun in browser, but it will reload utils.py and I have to wait for 1 min.
Is there any way I can rerun main_page.py only without reloading utils.py?
Thanks for your reply. The code can be very simple.
utils.py
import time
print("I am here!")
def my_calc():
time.sleep(5)
return 100
a = my_calc()
main_page.py
import streamlit as st
from utils import a
st.write("Main page")
st.write(a)
The idea is all complex calculation is done in utils.py file, and the UI and simple calculation is done in main_page.py file. I would like to edit main_page.py file (say add some button, show numbers calculated from utils.py 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 utils.py file and do all the calculations again and takes long time. Is there any way I can update main_page.py file without reloading utils.py file?
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 utils.py 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 main_page.py file just needs to use the main thread to fetch stored calculation results from utils.py 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 main_page.py file, and when the calculation finishes, the main_page.py file will automatically fetch the updated results stored in utils.py file.
So my code is actually more complicated than above. The simplified version is like below.
utils.py
class DBCache:
def __init__(self, name):
self.name = name
self.has_data = False
self.data = None
def reset(self):
data = generate_data()
self.has_data = True
self.data = data
def get_data(self):
return self.data
my_cache = DBCache("my_cache")
## some codes to create a thread and schedule reset() function in my_cache object every 2 hours.
main_page.py
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 main_page.py file. Ideally, if I update the main_page.py file and I click rerun button in browser and it does not reload utils.py 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 utils.py file, so I have to wait for 20 mins to get the results.
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 main_page.py file and rerun without reloading utils.py 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.
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.