Thank you so much!! I got it to work, I used your approach but adjusted it for multiple dictionaries held in session_state The only thing I need to adjust is the copy_value function; below I just wanted to test and see if it worked so I only tested one variable, and not copying an entire dictionary. UPDATE: I was able to get it working for the entire dictionary, it’s in my functions used how I did it, and I used it with st.button on Page2!
Functions used
def defaults():
st.session_state['cantilever_dict'] = {
'amp_invols':1.15821e-07,
'def_invols':1.15821e-07,
'soft_amp':0.3,
'drive_freq':279173,
'res_freq':279173,
'k':20,
'q_factor':450
}
st.session_state['force_dict'] = {
'es_force':3.72358e-08,
'delta_freq':-170,
'tau':50e-6
}
st.session_state['simulation_dict'] = {
'trigger':0.0004, # 0.4 ms\n",
'total_time':0.004, # 4 ms total\n",
'sampling_rate':1e7
}
st.session_state['others'] = {
'beta':0.4,
'tau_stretched':50e-6,
'exponential_type':'Single Exponential',
'tau1':50e-6,
'tau2':500e-6
}
st.session_state['CC_cantilever_dict'] = {
'amp_invols':1.15821e-07,
'def_invols':1.15821e-07,
'soft_amp':0.3,
'drive_freq':279173,
'res_freq':279173,
'k':20,
'q_factor':450
}
st.session_state['CC_force_dict'] = {
'es_force':3.72358e-08,
'delta_freq':-170,
'tau':50e-6
}
st.session_state['CC_simulation_dict'] = {
'trigger':0.0004, # 0.4 ms\n",
'total_time':0.004, # 4 ms total\n",
'sampling_rate':1e7
}
st.session_state['CC_others'] = {
'beta':0.4,
'tau_stretched':50e-6,
'exponential_type':'Single Exponential',
'tau1':50e-6,
'tau2':500e-6
}
def update_dict(dict_name, key):
st.session_state[dict_name][key] = st.session_state[key]
def copy_values(ourDict, dictUsed):
target_dict = st.session_state[ourDict]
source_dict = st.session_state[dictUsed]
for key in target_dict:
if key in source_dict:
target_dict[key] = source_dict[key]
else:
st.write('Didnt work babes')
def copy_ALL_values(dict1A, dict2A, dict1B, dict2B, dict1C, dict2C, dict1D, dict2D):
copy_values(dict1A, dict2A)
copy_values(dict1B, dict2B)
copy_values(dict1C, dict2C)
copy_values(dict1D, dict2D)
Page1
with st.sidebar:
cant_parms = st.session_state['cantilever_dict']
force_parms = st.session_state['force_dict']
sim_parms = st.session_state['simulation_dict']
other_parms = st.session_state['others']
for key in cant_parms:
st.session_state[key] = cant_parms[key]
for key in force_parms:
st.session_state[key] = force_parms[key]
for key in sim_parms:
st.session_state[key] = sim_parms[key]
for key in other_parms:
st.session_state[key] = other_parms[key]
exponential_type = st.selectbox('', ('Single Exponential','Stretched Exponential', 'Bi Exponential'), key='exponential_type', on_change=update_dict, args=['others', 'exponential_type'])
if other_parms['exponential_type'] == 'Stretched Exponential':
beta = st.number_input('Beta', key='beta',on_change=update_dict, args=['others', 'beta'], format='%f')
tau_stretched = st.number_input('Tau', key='tau_stretched',on_change=update_dict, args=['others', 'tau_stretched'], format='%e')
if other_parms['exponential_type'] == 'Bi Exponential':
tau1 = st.number_input('Tau 1', key='tau1', on_change=update_dict, args=['others', 'tau1'], format='%e')
tau2 = st.number_input('Tau 2', key='tau2',on_change=update_dict, args=['others', 'tau2'], format='%e')
with st.expander('Cantilever Parameters', expanded = True):
amp_invols = st.number_input('Amplitude (volts to meters)', key='amp_invols', on_change=update_dict, args=['cantilever_dict', 'amp_invols'], format='%e')
def_invols = st.number_input('Deflection (volts to meters)', key='def_invols', on_change=update_dict, args=['cantilever_dict', 'def_invols'], format='%e')
soft_amp = st.number_input('Soft Amplitude', key='soft_amp', on_change=update_dict, args=['cantilever_dict', 'soft_amp'], format='%f')
drive_freq = st.number_input('Driving Frequency', key='drive_freq', on_change=update_dict, args=['cantilever_dict', 'drive_freq'])
res_freq = st.number_input('Resonance Frequency', key='res_freq', on_change=update_dict, args=['cantilever_dict', 'res_freq'])
k = st.number_input('Spring Constant', key='k', on_change=update_dict, args=['cantilever_dict', 'k'])
q_factor = st.number_input('Cantilever Quality', key='q_factor', on_change=update_dict, args=['cantilever_dict', 'q_factor'])
with st.expander('Force Parameters', expanded = False):
es_force = st.number_input('Electrostatic Force', key='es_force', on_change=update_dict, args=['force_dict', 'es_force'], format='%e')
delta_freq = st.number_input('Delta Frequency', key='delta_freq', on_change=update_dict, args=['force_dict', 'delta_freq'])
if other_parms['exponential_type'] == 'Single Exponential':
tau = st.number_input('Tau', key='tau', on_change=update_dict, args=['force_dict', 'tau'], format='%e')
else:
tau = 50e-6
with st.expander('Simulation Parameters', expanded = False):
trigger = st.number_input('Trigger (ms)', key='trigger', on_change=update_dict, args=['simulation_dict', 'trigger'], format='%f')
total_time = st.number_input('Total Time(ms)', key='total_time', on_change=update_dict, args=['simulation_dict', 'total_time'], format='%f')
sampling_rate = st.number_input('Sampling Rate (ms)', key='sampling_rate', on_change=update_dict, args=['simulation_dict', 'sampling_rate'], format='%e')
Page2 (Where the button is used)
cant_parms = st.session_state['CC_cantilever_dict']
force_parms = st.session_state['CC_force_dict']
sim_parms = st.session_state['CC_simulation_dict']
other_parms = st.session_state['CC_others']
for key in cant_parms:
st.session_state[key] = cant_parms[key]
for key in force_parms:
st.session_state[key] = force_parms[key]
for key in sim_parms:
st.session_state[key] = sim_parms[key]
for key in other_parms:
st.session_state[key] = other_parms[key]
exponential_type = st.selectbox('', ('Single Exponential','Stretched Exponential', 'Bi Exponential'), key='exponential_type', on_change=update_dict, args=['CC_others', 'exponential_type'])
if other_parms['exponential_type'] == 'Stretched Exponential':
beta = st.number_input('Beta', key='beta',on_change=update_dict, args=['CC_others', 'beta'], format='%f')
tau_stretched = st.number_input('Tau', key='tau_stretched',on_change=update_dict, args=['others', 'tau_stretched'], format='%e')
if other_parms['exponential_type'] == 'Bi Exponential':
tau1 = st.number_input('Tau 1', key='tau1', on_change=update_dict, args=['CC_others', 'tau1'], format='%e')
tau2 = st.number_input('Tau 2', key='tau2',on_change=update_dict, args=['CC_others', 'tau2'], format='%e')
with st.expander('Cantilever Parameters', expanded = True):
amp_invols = st.number_input('Amplitude (volts to meters)', key='amp_invols', on_change=update_dict, args=['CC_cantilever_dict', 'amp_invols'], format='%e')
def_invols = st.number_input('Deflection (volts to meters)', key='def_invols', on_change=update_dict, args=['CC_cantilever_dict', 'def_invols'], format='%e')
soft_amp = st.number_input('Soft Amplitude', key='soft_amp', on_change=update_dict, args=['CC_cantilever_dict', 'soft_amp'], format='%f')
drive_freq = st.number_input('Driving Frequency', key='drive_freq', on_change=update_dict, args=['CC_cantilever_dict', 'drive_freq'])
res_freq = st.number_input('Resonance Frequency', key='res_freq', on_change=update_dict, args=['CC_cantilever_dict', 'res_freq'])
k = st.number_input('Spring Constant', key='k', on_change=update_dict, args=['CC_cantilever_dict', 'k'])
q_factor = st.number_input('Cantilever Quality', key='q_factor', on_change=update_dict, args=['CC_cantilever_dict', 'q_factor'])
with st.expander('Force Parameters', expanded = False):
es_force = st.number_input('Electrostatic Force', key='es_force', on_change=update_dict, args=['CC_force_dict', 'es_force'], format='%e')
delta_freq = st.number_input('Delta Frequency', key='delta_freq', on_change=update_dict, args=['CC_force_dict', 'delta_freq'])
if other_parms['exponential_type'] == 'Single Exponential':
tau = st.number_input('Tau', key='tau', on_change=update_dict, args=['CC_force_dict', 'tau'], format='%e')
else:
tau = 50e-6
with st.expander('Simulation Parameters', expanded = False):
trigger = st.number_input('Trigger (ms)', key='trigger', on_change=update_dict, args=['CC_simulation_dict', 'trigger'], format='%f')
total_time = st.number_input('Total Time(ms)', key='total_time', on_change=update_dict, args=['CC_simulation_dict', 'total_time'], format='%f')
sampling_rate = st.number_input('Sampling Rate (ms)', key='sampling_rate', on_change=update_dict, args=['CC_simulation_dict', 'sampling_rate'], format='%e')
st.button('Copy from Sim', on_click=copy_ALL_values, args=['CC_cantilever_dict','cantilever_dict','CC_force_dict','force_dict','CC_simulation_dict','simulation_dict','CC_others','others'])