Optimize Streamlit Deployment | Towards Data Science

[ad_1]


Caching

There are two important scenarios with caching, (1) caching with ttl and (2) caching with hashed dictionaries.

First, consider a typical Streamlit call to plot a graph from some other code (stat_charts.py) that makes the actual plot with some function (graph1()). In this case, the program generates the graph anew each time. In some cases, this can cause your app to crash or use up allotted resources.

# Streamlit with Plotly, no cachingimport streamlit as st
from figs.stat_charts.py import Achart
st.plotly_chart(Achart().graph1())

According to the docs, you might try to use the @st.cache decorator with a function wrapper. However, after a few hours of repeated calls, memory problems will accumulate and crash the app as described in this blog post.

# with basic caching, eventually crash app@st.cache()
def make_fig():
some_fig = Achart().graph1()
return some_fig
def show_objects():
st.plotly_chart(make_fig())

Instead, make sure to include parameters for max_entries and ttl to manage the cache size. For my part, I am going forward with these parameters as a default unless there is reason not to.

# with basic cache controls@st.cache(max_entries=10, ttl=3600)
def make_fig():
some_fig = Achart().graph1()
return some_fig
def show_objects():
st.plotly_chart(make_fig())
Figure 4 — A second issue with caching.

Second, depending on your functions, you may run into a CachedObjectMutationWarning which basically means something inside the function is changing every time. According to the documentation, mutating a cached function is generally undesirable but there is a workaround from this blog post.

CachedObjectMutationWarning: Return value of overview_data() was mutated between runs.By default, Streamlit's cache should be treated as immutable, or it may behave in unexpected ways. You received this warning because Streamlit detected that an object returned by overview_data() was mutated outside of overview_data().

A solution from the Streamlit blog, which is quite clever, returns the plot into the cache as a hashed dictionary key-value pair. Afterwards, subsequent calls are to the dictionary. This is super cool!

# cache plots in hashed dictionary@st.cache(hash_funcs={dict: lambda _: None})
def make_fig():
some_fig = Achart().graph1()
cached_dict = {'f1': some_fig}
return cached_dict
def show_objects():
charts = make_fig()
st.plotly_chart(charts['f1'])

Read More …

[ad_2]


Write a comment