Spaces:
Runtime error
Runtime error
File size: 4,337 Bytes
ac6c40f 37b8c09 57616af 37b8c09 57616af 37b8c09 57616af 37b8c09 ac6c40f 37b8c09 ac6c40f 37b8c09 ac6c40f 37b8c09 ac6c40f 37b8c09 ac6c40f 57616af ac6c40f 37b8c09 ac6c40f 57616af ac6c40f 37b8c09 ac6c40f 57616af ac6c40f 37b8c09 ac6c40f 37b8c09 ac6c40f 57616af ac6c40f 37b8c09 ac6c40f 37b8c09 ac6c40f 37b8c09 ac6c40f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import streamlit as st
# Streamlit widgets with persistence
def is_filled(key_list):
state_filled_key = "_".join(key_list) + "_filled"
def on_change_action():
st.session_state.save_state[state_filled_key] = True
return on_change_action
def update_card_dict(key_list, use_default=None):
state_key = "_".join(key_list)
if st.session_state.save_state.get(state_key + "_filled", False) or use_default:
card_key = key_list[-1]
current_dict = st.session_state.card_dict
for key in key_list[:-1]:
current_dict = current_dict[key]
current_dict[card_key] = st.session_state.save_state.get(state_key, use_default)
def make_multiselect(
key_list, label, options, format_func=lambda x: x, help="", default=None
):
key = "_".join(key_list)
if key in st.session_state:
st.session_state.save_state[key] = st.session_state[key]
elif default is not None:
st.session_state.save_state[key] = default
res = st.multiselect(
label=label,
options=options,
format_func=format_func,
key=key,
default=st.session_state.save_state.get(key, []),
on_change=is_filled(key_list),
help=help,
)
update_card_dict(key_list)
return res
def make_selectbox(
key_list, label, options, format_func=lambda x: x, help="", index=None
):
key = "_".join(key_list)
if key in st.session_state:
st.session_state.save_state[key] = st.session_state[key]
elif index is not None:
st.session_state.save_state[key] = options[index]
res = st.selectbox(
label=label,
options=options,
format_func=format_func,
key=key,
index=options.index(
st.session_state.save_state.get(key, options[0])
), # if st.session_state.save_state.get(key, options[0]) in options else 0,
on_change=is_filled(key_list),
help=help,
)
update_card_dict(
key_list, use_default=st.session_state.save_state.get(key, options[0])
) # use the default value even without interactions
return res
def make_radio(key_list, label, options, format_func=lambda x: x, help="", index=None):
key = "_".join(key_list)
if key in st.session_state:
st.session_state.save_state[key] = st.session_state[key]
elif index is not None:
st.session_state.save_state[key] = options[index]
res = st.radio(
label=label,
options=options,
format_func=format_func,
key=key,
index=options.index(st.session_state.save_state.get(key, options[0])),
on_change=is_filled(key_list),
help=help,
)
update_card_dict(
key_list, use_default=st.session_state.save_state.get(key, options[0])
) # use the default value even without interactions
return res
def make_text_input(key_list, label, help="", value=None):
key = "_".join(key_list)
if key in st.session_state:
st.session_state.save_state[key] = st.session_state[key]
elif value is not None:
st.session_state.save_state[key] = value
res = st.text_input(
label=label,
key=key,
value=st.session_state.save_state.get(key, ""),
on_change=is_filled(key_list),
help=help,
)
update_card_dict(key_list)
return res
def make_text_area(key_list, label, help="", value=None):
key = "_".join(key_list)
if key in st.session_state:
st.session_state.save_state[key] = st.session_state[key]
elif value is not None:
st.session_state.save_state[key] = value
res = st.text_area(
label=label,
key=key,
value=st.session_state.save_state.get(key, ""),
on_change=is_filled(key_list),
help=help,
)
update_card_dict(key_list)
return res
def make_checkbox(key_list, label, help="", value=None):
key = "_".join(key_list)
if key in st.session_state:
st.session_state.save_state[key] = st.session_state[key]
elif value is not None:
st.session_state.save_state[key] = value
res = st.checkbox(
label=label,
key=key,
value=st.session_state.save_state.get(key, False),
on_change=is_filled(key_list),
help=help,
)
update_card_dict(key_list)
return res
|