I need to manually enter keys and values and fill the dictionary with them. But every time I enter a new key and value, the previous one is erased. How can I save all the entered values in the dictionary?
with st.beta_container():
d = {}
col1, col2 = st.beta_columns(2)
with col1:
k = st.text_input('Key')
with col2:
v = st.text_input('Value')
button = st.button('Add')
if button:
if k and v:
d[k] = v
st.write(d)
Although it feels a bit boilerplate-y, you can accomplish what you want using the following:
import streamlit as st
@st.cache(allow_output_mutation=True)
def persistdata():
return {}
with st.beta_container():
d = persistdata()
col1, col2 = st.beta_columns(2)
with col1:
k = st.text_input("Key")
with col2:
v = st.text_input("Value")
button = st.button("Add")
if button:
if k and v:
d[k] = v
st.write(d)
Essentially, the st.cache decorator is telling Streamlit to persist this across runs, and allow_output_mutation=True allows you to modify the cached object. So the net result is the values persist over time.