Caching and state
Optimize performance and add statefulness to your app!
Caching
Jeamlit provides powerful cache primitives for data and global resources. They allow your app to stay performant even when loading data from the web, manipulating large datasets, or performing expensive computations.
Cache data
Function decorator to cache functions that return data (e.g. dataframe transforms, database queries, ML inference).
// share a ML model - init
var mlModel = Jt.cache().computeIfAbsent("model", k -> loadMlModel());
// share a DB connection
var conn = Jt.cache().computeIfAbsent("connection", k -> connectToDb());
Browser and server state
Jeamlit re-executes your script with each user interaction. Widgets have built-in statefulness between reruns, but Session State lets you do more!
Session State
Save data between reruns and across pages.
if ((Jt.sessionState().contains("ml_model")) {
Jt.sessionState().put("ml_model", loadModel());
}
var mlModel = Jt.sessionState().get("ml_model");
// one liner alternative
var mlModel = Jt.sessionState.computeIfAbsent("ml_model", k -> loadModel());
Query parameters
Read the query parameters that are shown in the browser's URL bar.
List<String> people = Jt.urlQueryParameters().get("people");
Still have questions?
Go to our discussions forum for helpful information and advice from Jeamlit experts.