Using the key parameter in an input widget, a session state key is formed. Can this key be set within a dictionary initialized in session_state directly?
import streamlit as st
if 'vars' not in st.session_state:
st.session_state.vars = {}
name=st.text_input("Name", key="name")
count_default = st.number_input('Count Default', key='count_def')
# need to set key inside dictionary in session_state
count_nested = st.number_input('Count Nested', key=vars['count_nes'])
st.write(st.session_state)
# SAMPLE OUTPUT NEEDED
# {
# 'name': 'Ramesh Kumar',
# 'count_def': 1,
# 'vars': {
# 'count_nes': 2
# }
# }
I’m aware of explicitly setting the value using:
st.session_state.vars['count_nes'] = count_nested
but I am working with many such fields and am looking to easily categorize them in dictionaries.
An alternative is to add a common prefix to these keys and have a function to look for them in st.session_state
when needed.