Populating the cache without opening the browser after initial deployment


How to programmatically populate the cache without opening the browser after initial deployment (docker container) so that the first user who opens the app through a browser will not wait for the long executing operation.

Steps to reproduce

import streamlit as st
import time

def long_running_code():
    return 1


In the ideal scenario, a user visiting the application after 2 minutes of the deployment point does not wait 5 seconds to see the result.

Config used in the run:

headless = true

Here is a thread related to that idea.

As in that thread, you can use Selenium to open your app and populate your cache. If you have some expensive operation that needs to be run nightly, that’s a simple thing to schedule from some internet-connected machine somewhere. If you don’t need it on a schedule but want it in response to a change to the source code or some other non-predictable, non-scheduled change to data, you’d have to have some function monitoring that source for changes to trigger your Selenium script to open your app and get the cache populated. I don’t know of anything slicker than that, but there is a link to a Feature Request on GitHub that you can vote on if you’d like Streamlit to have some form of this functionality natively.

1 Like

Thanks Debbie, that was exactly what I was looking for!

selenium is a huge dependency and also an hacky solution.

Is there a better any solution for this?

I don’t know of one. Here is the current feature request from the linked thread if you want to vote on it.