Summary
I’m trying to use st.multiselect to append data to a dictionary(dict_options), inserting a new key and using options from a list as values.
When I use text_input, I can create the mulstiselect (with option1 and option2 by default), but when I change the selection (let’s say, I add option3) everything restarts and I miss the multiselect.
Maybe I can use st.cache for this, the problem is when I add @st.cache_data
and I try to get a new value, I get a warning:
CachedStFunctionWarning: Your script uses
st.multiselect()
to write to your Streamlit app from within some cached code atnewc()
. This code will only be called when we detect a cache “miss”, which can lead to unexpected results.How to fix this:
- Move the
st.multiselect()
call outsidenewc()
.- Or, if you know what you’re doing, use
@st.cache_data(suppress_st_warning=True)
to suppress the warning
If I feel brave and I use @st.cache_data(suppress_st_warning=True)
I get an error:
TypeError: CacheDataAPI.call() got an unexpected keyword argument ‘suppress_st_warning’
Steps to reproduce
Code snippet:
import streamlit as st
#@st.cache_data(suppress_st_warning=True)
def newc(_col2, dict_options, new_country, options):
dict_options[new_country] = ['option1','option2']
dict_options[new_country] = _col2.multiselect(f'Select the options you want for {new_country}', options, dict_options.get(new_country), disabled=False)
return dict_options, new_country
def main():
dict_options = {}
options = ['option1','option2','option3','option4']
col1, col2, col3 = st.columns([8, 18, 1])
new_country = col1.text_input('New country', value='UK', label_visibility="collapsed")
if col1.button('Add New Country'):
dict_options, new_country = newc(col2, dict_options, new_country, options)
if __name__== "__main__" :
main()
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
- Run the code snippet
- Just clic on the ‘Add new country’ button and try to add ‘option3’ or ‘option4’.
- Try to uncomment the decorator on line 3, with or without
suppress_st_warning=True
you’ll get different warnings/errors.
Expected behavior:
I would like to add a new dictionary key:value pair, for example
UK : ['option1','option2','option3']
Actual behavior:
Well, one of the biggest issues for me with streamlit: it reloads and I miss the new data, and so far I just don’t know how to use st.cache with multiselect… the code is written, I’m pretty sure I’m close, I just need some help.
Debug info
- Streamlit version: (get it with
$ streamlit version
) 1.19.0 - Python version: (get it with
$ python --version
) 3.11.2 - Using Conda? PipEnv? PyEnv? Pex? Nope
- OS version: windows 11
- Browser version: Firefox 110.0.1