I do realize that the session states can pass on the variables but those exist only till the session(browser) is open. Is there a way to store a variable like a counter value from the current session and then load the same value again in a new session(close and rerun the app).
Hi @Ishan_Mistry,
That’s a great use case for a database! Unfortunately, there’s not a built-in database in streamlit yet, but hopefully that will be built in in a future version. In the meantime, there are a number of great options for a third-party database, and we have tutorials about connecting your streamlit app to many of them Connect to data sources - Streamlit Docs.
Im not sure its exactly what you’re looking for, but I like to allow the user an option to save a large amount of settings as json, and then load it…
the problem is that it doesnt really work with upload widgets and its a bit problematic with my apps…
but anyways, here is my implementaion:
def get_session_state_dict():
session_state_dict = {
k: v
for k, v in st.session_state.items()
if "button" not in k and "file_uploader" not in k and "FormSubmitter" not in k
}
st.download_button(
label="Save Current Settings as a File",
data=json.dumps(session_state_dict , default=str),
file_name="Settings.json",
)
upload_settings_widget = st.file_uploader(
label="Upload Previously Saved Settings File",
type=["json"],
accept_multiple_files=False,
)
if upload_settings_widget:
with upload_settings_widget.getvalue() as f:
uploaded_settings = json.loads(f)
failed = []
succeeded = []
button_apply_uploaded_settings = st.button(
"Apply Settings",
on_click=apply_uploaded_settings,
args=(uploaded_settings,),
)
def apply_uploaded_settings(json_settings):
failed = []
succeeded = []
for k, v in json_settings.items():
try:
st.session_state[k] = v
succeeded.append(k)
except:
failed.append(k)
st.success(
f"Successfully uploaded {str(len(succeeded))} out of {str(len(succeeded) + len(failed))} settings"
)
if len(failed) > 0:
st.error(
f"Failed to upload the following settings: {failed}"
)
Sorry if its a mess, I am away from my laptop
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.