I would like to better understand the logic behind session_state because I am in trouble. I noticed that if I put a dictionary in session_state, let’s say mem, then I delete a key directly, for example:
del st.session_state.mem[‘ingr’]
if I enter the same key again through a widget, the previous value associated with that key reappears again, despite having deleted it before.
How do you permanently delete a value in a data structure stored in session_state?
it’s not exactly an easy code to follow. Basically the user enters a recipe in the form of a dictionary in which each ingredient corresponds to a value. The user must be able to go back to the recipe, add, remove and change values.
################ FUNCTION ################
def changed_ingr():
if st.session_state.ricetta_memoria_changed:
if len(st.session_state.ricetta_memoria_changed)>len(st.session_state.ricetta_memoria[1]):
ingr=set(st.session_state.ricetta_memoria_changed)-set(list(st.session_state.ricetta_memoria[1].keys()))
ingr=list(ingr)[0]
st.session_state.ricetta_memoria[1][ingr]=0.0
elif len(st.session_state.ricetta_memoria_changed)<len(st.session_state.ricetta_memoria[1]):
ingr=set(list(st.session_state.ricetta_memoria[1].keys()))-set(st.session_state.ricetta_memoria_changed)
ingr=list(ingr)[0]
del st.session_state.ricetta_memoria[1][ingr] # fai l'update della ricetta in memoria
################### MAIN ##################
if "ricetta_memoria" not in st.session_state:
st.session_state.ricetta_memoria = ["",ricetta,dati_temp_immessi]
select=st.multiselect('Scegli gli ingredienti della tua ricetta',df.index.tolist(),on_change=changed_ingr,
default=list(st.session_state.ricetta_memoria[1].keys()), key="ricetta_memoria_changed")
for key in list(st.session_state.ricetta_memoria[1].keys()):
st.session_state.ricetta_memoria[1][key]=st.number_input(key,min_value=0.,
value=st.session_state.ricetta_memoria[1][key])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.