Skip to content

Add docs for cached media element+form replay #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions content/library/api/performance/experimental-memo.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Supported static `st` elements in cache-decorated functions include:
- `st.alert`
- `st.altair_chart`
- `st.area_chart`
- `st.audio`
- `st.bar_chart`
- `st.ballons`
- `st.bokeh_chart`
Expand All @@ -84,8 +85,12 @@ Supported static `st` elements in cache-decorated functions include:
- `st.expander`
- `st.experimental_get_query_params`
- `st.experimental_set_query_params`
- `st.experimental_show`
- `st.form`
- `st.form_submit_button`
- `st.graphviz_chart`
- `st.help`
- `st.image`
- `st.info`
- `st.json`
- `st.latex`
Expand All @@ -101,33 +106,9 @@ Supported static `st` elements in cache-decorated functions include:
- `st.table`
- `st.text`
- `st.vega_lite_chart`
- `st.video`
- `st.warning`

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:

```python
import numpy as np
import pandas as pd
import streamlit as st

@st.experimental_memo
def load_data(rows):
chart_data = pd.DataFrame(
np.random.randn(rows, 10),
columns=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)
# Contains an unsupported st command
st.video("https://www.youtube.com/watch?v=wpDuY9I2fDg")
# Streamlit will throw a CachedStFunctionWarning

return chart_data

df = load_data(20)
st.dataframe(df)
```

<Image src="/images/cached-st-function-warning.png" clean />

### Replay input widgets in cache-decorated functions

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.
Expand Down
29 changes: 6 additions & 23 deletions content/library/api/performance/experimental-singleton.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Supported static `st` elements in cache-decorated functions include:
- `st.alert`
- `st.altair_chart`
- `st.area_chart`
- `st.audio`
- `st.bar_chart`
- `st.ballons`
- `st.bokeh_chart`
Expand All @@ -61,8 +62,12 @@ Supported static `st` elements in cache-decorated functions include:
- `st.expander`
- `st.experimental_get_query_params`
- `st.experimental_set_query_params`
- `st.experimental_show`
- `st.form`
- `st.form_submit_button`
- `st.graphviz_chart`
- `st.help`
- `st.image`
- `st.info`
- `st.json`
- `st.latex`
Expand All @@ -78,31 +83,9 @@ Supported static `st` elements in cache-decorated functions include:
- `st.table`
- `st.text`
- `st.vega_lite_chart`
- `st.video`
- `st.warning`

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:

```python
import numpy as np
import pandas as pd
import streamlit as st
from transformers import AutoModel

@st.experimental_singleton
def get_model(model_type):
# Contains an unsupported st command
st.video("https://www.youtube.com/watch?v=wpDuY9I2fDg")
# Streamlit will throw a CachedStFunctionWarning

# Create a model of the specified type
return AutoModel.from_pretrained(model_type)

bert_model = get_model("distilbert-base-uncased")
st.help(bert_model) # Display the model's docstring
```

<Image src="/images/cached-st-function-warning-singleton.png" clean />

### Replay input widgets in cache-decorated functions

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.
Expand Down