I’m trying to apply chained callbacks to my dropdown menus where the key-value pairs are updated via a nested default dictionary. I’m not accessing dict.keys() but rather nested dictionary key values. I want each dropdown to update with only the keys corresponding to the prior nested dict key value. However, dash seems to be dynamically updating each key value every time I re-run the program.
I.e. keys_fin[1][0] always = ‘Resource Consumption’ in Jupyter, but in dash, it changes the order each time.
Is there a way to keep these keys_fin values fixed and not have them change each time I re-run the program?
Code, not including callback cells and dropdowns.
df0 = pd.read_csv(’/Users/david/Downloads/Env_with_financial.csv’,header=None)
df1 = df0.replace(np.NaN,’’)
df = np.array(df1)
def get_dict_keys(x, df):
return [[k.strip() for idx, k in enumerate(df[i]) if k != ‘’ and idx <= x][-1] for i in range(4)]
#for kurtis: ‘BESGPRO_CDN_jupyter.csv’
def get_json():
# create dict of company data
nested_dict = lambda: defaultdict(nested_dict)
data = nested_dict()
for idx_row, row in enumerate(df):
for idx_val, value in enumerate(row):
if idx_row < 4 or idx_val < 2:
continue
# if data exists
if value != '' and value != '--' and value != '-':
tick = df[:, 0][idx_row]
# find keys and add value
keys_fin = get_dict_keys(idx_val, df)
data[tick][keys_fin[0]][keys_fin[1]][keys_fin[2]][keys_fin[3]] = value
return dict(data)
def key_at_depth(dct, dpt):
if dpt > 0:
return [key for k, subdct in dct.items() for key in key_at_depth(subdct, dpt-1)]
else:
return dct.keys()
def get_data_arr(data, key1, key2, key3, key4):
tickers, data_points = [], []
for t, _ in data.items():
try:
data_points.append(float(data[t][key1][key2][key3][key4]))
tickers.append(t)
except:
pass
return tickers, data_points
data = get_json()
get all keys at every depth
keys_fin = [list(set(key_at_depth(data, i))) for i in range(5)]