Skip to content

Commit

Permalink
main : small change to persistance using dict
Browse files Browse the repository at this point in the history
  • Loading branch information
my1e5 committed Mar 13, 2023
1 parent 511072e commit 1c0971a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions examples/persistance_using_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
import dearpygui.dearpygui as dpg
dpg.create_context()

try:
with open("app-state-dict.json", "r") as f:
state = json.load(f)
except:
state = {
STATE_FILENAME = "app-state-dict.json"

def save_state(state):
with open(STATE_FILENAME, "w") as f:
json.dump(state, f, indent=4)

def load_state():
try:
with open(STATE_FILENAME, "r") as f:
state = json.load(f)
except:
state = {
"name": "My App",
"settings": {
"foo": 0,
"bar": "DPG is awesome!",
"baz": 3.1415
}
}
}
return state


state = load_state()

with dpg.window():
dpg.set_primary_window(dpg.last_item(), True)
Expand All @@ -23,13 +34,11 @@
dpg.add_input_float(label="Baz", default_value=state["settings"]["baz"], callback=lambda s, d: state["settings"].__setitem__("baz", d))
dpg.add_button(label="Print app state", callback=lambda: print(state))


dpg.create_viewport(title="App Persistance Demo using Dict", height=400, width=500)
dpg.setup_dearpygui()
dpg.show_viewport()
try:
dpg.start_dearpygui()
finally:
with open("app-state-dict.json", "w") as f:
json.dump(state, f, indent=4)
save_state(state)
dpg.destroy_context()

0 comments on commit 1c0971a

Please sign in to comment.