Last active
January 23, 2024 00:21
-
-
Save hdary85/6547cae924b8095a02336534f5ebe3a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save the Streamlit code in a separate file, e.g., my_streamlit_app.py | |
# my_streamlit_app.py | |
import streamlit as st | |
def update_nested_dict(nested_dict, keys, new_value): | |
current_dict = nested_dict | |
for key in keys[:-1]: | |
current_dict = current_dict[key] | |
current_dict[keys[-1]] = new_value | |
def display_and_update_gui(nested_dict): | |
st.title("Nested Dictionary Updater") | |
st.text("Original Dictionary:") | |
st.json(nested_dict) | |
key_input = st.text_input("Enter the keys (comma-separated):") | |
keys = key_input.split(',') | |
new_value_input = st.text_input("Enter the new value:") | |
if st.button("Update"): | |
try: | |
new_value = eval(new_value_input) # Convert input to the appropriate data type | |
update_nested_dict(nested_dict, keys, new_value) | |
st.success("Update successful!") | |
except Exception as e: | |
st.error(f"Error: {e}") | |
st.text("Updated Dictionary:") | |
st.json(nested_dict) | |
# Example usage: | |
my_nested_dict = {'outer': {'inner': {'value': 42}}} | |
display_and_update_gui(my_nested_dict) | |
%reload_ext streamlit | |
%streamlit run my_streamlit_app.py | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment