I have built a object detection model on Streamlit that allows users to upload their video and it does object detection, saves the video in a folder, and displays it on a viewer using st.video().
However, the issue is that even when the user closes the browser, the old video will still be there in the folder, so when many people use it, it continues to pile up.
Is there anyway to delete folders when the user closes the browser?
Any suggestions welcome!
I read that tempfile will only delete the temp directory when the program is shut down. In the case of streamlit, when the user closes the browser, the script is still running in the cloud. So does that mean that it does not get deleted? Or we have to manually delete it in the code.
Code looks something like this:
# create temp dir for storing video and outputs
temp_dir = tempfile.TemporaryDirectory()
RESULTS_PATH = temp_dir.name
# save video to RESULTS PATH .....
temp_dir.cleanup() # manually remove temp_dir
Perhaps I can start a timer to execute the temp_dir.cleanup() after a certain time? Not ideal though.
Another way is using context manager to auto remove the temp directory. However, this requires that my entire streamlit code be subsumed under the with context manager?
# create temp dir for storing video and outputs
with tempfile.TemporaryDirectory() as temp_dir :
print('created temporary directory', temp_dir )
# after the context manager, temp_dir will be auto deleted
In general, my recommendation is to use the context manager version, so that the cleanup will happen automatically even if you have an error that gets raised. I suspect that you won’t actually have to put your entire streamlit app inside of the context manager, but only some of it, but if you’d prefer you can always move your code to separate functions, and call those in the context manager rather than writing the code directly inside the context manager.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hello there 👋🏻
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.