Skip to content

Commit 777f458

Browse files
authored
Merge pull request #549 from streamlit/cached-form-media-replay
Add docs for cached media element+form replay
2 parents 5c54443 + 685f39f commit 777f458

File tree

2 files changed

+12
-48
lines changed

2 files changed

+12
-48
lines changed

content/library/api/performance/experimental-memo.md

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Supported static `st` elements in cache-decorated functions include:
6868
- `st.alert`
6969
- `st.altair_chart`
7070
- `st.area_chart`
71+
- `st.audio`
7172
- `st.bar_chart`
7273
- `st.ballons`
7374
- `st.bokeh_chart`
@@ -84,8 +85,12 @@ Supported static `st` elements in cache-decorated functions include:
8485
- `st.expander`
8586
- `st.experimental_get_query_params`
8687
- `st.experimental_set_query_params`
88+
- `st.experimental_show`
89+
- `st.form`
90+
- `st.form_submit_button`
8791
- `st.graphviz_chart`
8892
- `st.help`
93+
- `st.image`
8994
- `st.info`
9095
- `st.json`
9196
- `st.latex`
@@ -101,33 +106,9 @@ Supported static `st` elements in cache-decorated functions include:
101106
- `st.table`
102107
- `st.text`
103108
- `st.vega_lite_chart`
109+
- `st.video`
104110
- `st.warning`
105111
106-
Forms and media elements are not supported in cache-decorated functions. If you use them, the code will only be called when we detect a cache "miss", which can lead to unexpected results. Which is why Streamlit will throw a `CachedStFunctionWarning`, like the one below:
107-
108-
```python
109-
import numpy as np
110-
import pandas as pd
111-
import streamlit as st
112-
113-
@st.experimental_memo
114-
def load_data(rows):
115-
chart_data = pd.DataFrame(
116-
np.random.randn(rows, 10),
117-
columns=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
118-
)
119-
# Contains an unsupported st command
120-
st.video("https://www.youtube.com/watch?v=wpDuY9I2fDg")
121-
# Streamlit will throw a CachedStFunctionWarning
122-
123-
return chart_data
124-
125-
df = load_data(20)
126-
st.dataframe(df)
127-
```
128-
129-
<Image src="/images/cached-st-function-warning.png" clean />
130-
131112
### Replay input widgets in cache-decorated functions
132113
133114
In addition to static elements, functions decorated with `@st.experimental_memo` can also contain [input widgets](/library/api-reference/widgets)! Replaying input widgets is disabled by default. To enable it, you can set the `experimental_allow_widgets` parameter for `@st.experimental_memo` to `True`. The example below enables widget replaying, and shows the use of a checkbox widget within a cache-decorated function.

content/library/api/performance/experimental-singleton.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Supported static `st` elements in cache-decorated functions include:
4545
- `st.alert`
4646
- `st.altair_chart`
4747
- `st.area_chart`
48+
- `st.audio`
4849
- `st.bar_chart`
4950
- `st.ballons`
5051
- `st.bokeh_chart`
@@ -61,8 +62,12 @@ Supported static `st` elements in cache-decorated functions include:
6162
- `st.expander`
6263
- `st.experimental_get_query_params`
6364
- `st.experimental_set_query_params`
65+
- `st.experimental_show`
66+
- `st.form`
67+
- `st.form_submit_button`
6468
- `st.graphviz_chart`
6569
- `st.help`
70+
- `st.image`
6671
- `st.info`
6772
- `st.json`
6873
- `st.latex`
@@ -78,31 +83,9 @@ Supported static `st` elements in cache-decorated functions include:
7883
- `st.table`
7984
- `st.text`
8085
- `st.vega_lite_chart`
86+
- `st.video`
8187
- `st.warning`
8288

83-
Forms and media elements are not supported in cache-decorated functions. If you use them, the code will only be called when we detect a cache "miss", which can lead to unexpected results. Which is why Streamlit will throw a `CachedStFunctionWarning`, like the one below:
84-
85-
```python
86-
import numpy as np
87-
import pandas as pd
88-
import streamlit as st
89-
from transformers import AutoModel
90-
91-
@st.experimental_singleton
92-
def get_model(model_type):
93-
# Contains an unsupported st command
94-
st.video("https://www.youtube.com/watch?v=wpDuY9I2fDg")
95-
# Streamlit will throw a CachedStFunctionWarning
96-
97-
# Create a model of the specified type
98-
return AutoModel.from_pretrained(model_type)
99-
100-
bert_model = get_model("distilbert-base-uncased")
101-
st.help(bert_model) # Display the model's docstring
102-
```
103-
104-
<Image src="/images/cached-st-function-warning-singleton.png" clean />
105-
10689
### Replay input widgets in cache-decorated functions
10790

10891
In addition to static elements, functions decorated with `@st.experimental_singleton` can also contain [input widgets](/library/api-reference/widgets)! Replaying input widgets is disabled by default. To enable it, you can set the `experimental_allow_widgets` parameter for `@st.experimental_singleton` to `True`. The example below enables widget replaying, and shows the use of a checkbox widget within a cache-decorated function.

0 commit comments

Comments
 (0)