How do i define nested session_state in dictionary?

Help. I want to create a Budgeting Apps in Streamlit. I need the following dictionaries in Streamlit session_state:

asset
-asset1
--asset1_name1
--asset1_name2
-asset2
--asset2_name1
--asset2_name2
liabilities
-liabilities1
-liabilities2

the dictionaries above is required so i can summarize per each header such as total_per_asset1 or total_per_asset

i have the following functions to show the number_input and store its value to session_state altogether:

def input_number(labelnya,session_name,value=None,help=None,format='%g'):
    valuenya = st.number_input(labelnya,value=value,help=help,format=format)
    valuenya = 0 if valuenya is None else valuenya
    if session_name not in st.session_state:
        st.session_state[session_name] = valuenya #how do i store the nested session_state
    print("All Session:\n{}".format(st.session_state))
    return valuenya

how do i start to resolve this?

Not sure if i understood correctly… you could create an Asset dataclass to store its name and value, and then keep a list or a dict of Asset objects in the st.session_state.

hi,

i’ve updated the thread. How do i set Asset in st.session_state?

Hi @Cignitor, you could create a common dictionary, like:

if "tmpdict" not in st.session_state: st.session_state.tmpdict = { 
"asset1": {"Name1": "", "Name2": ""},
"asset2": {"Name1": "", "Name2": ""},
"asset3": {"Name1": "", "Name2": ""},
"liabilities1": {},
}

And access / set whichever element like:

st.session_state.tdict["asset2"]["Name1"] = "a2n1"

Cheers

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.