Skip to content

Instantly share code, notes, and snippets.

@hdary85
Created January 23, 2024 00:54
Show Gist options
  • Save hdary85/2c4fa659e945717556f519e250d7975c to your computer and use it in GitHub Desktop.
Save hdary85/2c4fa659e945717556f519e250d7975c to your computer and use it in GitHub Desktop.
Widget
import ipywidgets as widgets
from IPython.display import display
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_widgets(nested_dict):
print("Original Dictionary:")
print(nested_dict)
key_input = widgets.Text(description="Enter keys (comma-separated):")
value_input = widgets.Text(description="Enter new value:")
update_button = widgets.Button(description="Update")
display(key_input, value_input, update_button)
def on_button_click(b):
try:
keys = key_input.value.split(',')
new_value = eval(value_input.value) # Convert input to appropriate data type
update_nested_dict(nested_dict, keys, new_value)
print("Update successful!")
print("Updated Dictionary:")
print(nested_dict)
except Exception as e:
print(f"Error: {e}")
update_button.on_click(on_button_click)
# Example usage:
my_nested_dict = {'outer': {'inner': {'value': 42}}}
display_and_update_widgets(my_nested_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment