ui_loadsave.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import json
  2. import os
  3. import gradio as gr
  4. from modules import errors
  5. from modules.ui_components import ToolButton
  6. class UiLoadsave:
  7. """allows saving and restorig default values for gradio components"""
  8. def __init__(self, filename):
  9. self.filename = filename
  10. self.ui_settings = {}
  11. self.component_mapping = {}
  12. self.error_loading = False
  13. self.finalized_ui = False
  14. self.ui_defaults_view = None
  15. self.ui_defaults_apply = None
  16. self.ui_defaults_review = None
  17. try:
  18. if os.path.exists(self.filename):
  19. self.ui_settings = self.read_from_file()
  20. except Exception as e:
  21. self.error_loading = True
  22. errors.display(e, "loading settings")
  23. def add_component(self, path, x):
  24. """adds component to the registry of tracked components"""
  25. assert not self.finalized_ui
  26. def apply_field(obj, field, condition=None, init_field=None):
  27. key = f"{path}/{field}"
  28. if getattr(obj, 'custom_script_source', None) is not None:
  29. key = f"customscript/{obj.custom_script_source}/{key}"
  30. if getattr(obj, 'do_not_save_to_config', False):
  31. return
  32. saved_value = self.ui_settings.get(key, None)
  33. if saved_value is None:
  34. self.ui_settings[key] = getattr(obj, field)
  35. elif condition and not condition(saved_value):
  36. pass
  37. else:
  38. setattr(obj, field, saved_value)
  39. if init_field is not None:
  40. init_field(saved_value)
  41. if field == 'value' and key not in self.component_mapping:
  42. self.component_mapping[key] = x
  43. if type(x) in [gr.Slider, gr.Radio, gr.Checkbox, gr.Textbox, gr.Number, gr.Dropdown, ToolButton, gr.Button] and x.visible:
  44. apply_field(x, 'visible')
  45. if type(x) == gr.Slider:
  46. apply_field(x, 'value')
  47. apply_field(x, 'minimum')
  48. apply_field(x, 'maximum')
  49. apply_field(x, 'step')
  50. if type(x) == gr.Radio:
  51. apply_field(x, 'value', lambda val: val in x.choices)
  52. if type(x) == gr.Checkbox:
  53. apply_field(x, 'value')
  54. if type(x) == gr.Textbox:
  55. apply_field(x, 'value')
  56. if type(x) == gr.Number:
  57. apply_field(x, 'value')
  58. if type(x) == gr.Dropdown:
  59. def check_dropdown(val):
  60. if getattr(x, 'multiselect', False):
  61. return all(value in x.choices for value in val)
  62. else:
  63. return val in x.choices
  64. apply_field(x, 'value', check_dropdown, getattr(x, 'init_field', None))
  65. def check_tab_id(tab_id):
  66. tab_items = list(filter(lambda e: isinstance(e, gr.TabItem), x.children))
  67. if type(tab_id) == str:
  68. tab_ids = [t.id for t in tab_items]
  69. return tab_id in tab_ids
  70. elif type(tab_id) == int:
  71. return 0 <= tab_id < len(tab_items)
  72. else:
  73. return False
  74. if type(x) == gr.Tabs:
  75. apply_field(x, 'selected', check_tab_id)
  76. def add_block(self, x, path=""):
  77. """adds all components inside a gradio block x to the registry of tracked components"""
  78. if hasattr(x, 'children'):
  79. if isinstance(x, gr.Tabs) and x.elem_id is not None:
  80. # Tabs element can't have a label, have to use elem_id instead
  81. self.add_component(f"{path}/Tabs@{x.elem_id}", x)
  82. for c in x.children:
  83. self.add_block(c, path)
  84. elif x.label is not None:
  85. self.add_component(f"{path}/{x.label}", x)
  86. elif isinstance(x, gr.Button) and x.value is not None:
  87. self.add_component(f"{path}/{x.value}", x)
  88. def read_from_file(self):
  89. with open(self.filename, "r", encoding="utf8") as file:
  90. return json.load(file)
  91. def write_to_file(self, current_ui_settings):
  92. with open(self.filename, "w", encoding="utf8") as file:
  93. json.dump(current_ui_settings, file, indent=4)
  94. def dump_defaults(self):
  95. """saves default values to a file unless tjhe file is present and there was an error loading default values at start"""
  96. if self.error_loading and os.path.exists(self.filename):
  97. return
  98. self.write_to_file(self.ui_settings)
  99. def iter_changes(self, current_ui_settings, values):
  100. """
  101. given a dictionary with defaults from a file and current values from gradio elements, returns
  102. an iterator over tuples of values that are not the same between the file and the current;
  103. tuple contents are: path, old value, new value
  104. """
  105. for (path, component), new_value in zip(self.component_mapping.items(), values):
  106. old_value = current_ui_settings.get(path)
  107. choices = getattr(component, 'choices', None)
  108. if isinstance(new_value, int) and choices:
  109. if new_value >= len(choices):
  110. continue
  111. new_value = choices[new_value]
  112. if new_value == old_value:
  113. continue
  114. if old_value is None and new_value == '' or new_value == []:
  115. continue
  116. yield path, old_value, new_value
  117. def ui_view(self, *values):
  118. text = ["<table><thead><tr><th>Path</th><th>Old value</th><th>New value</th></thead><tbody>"]
  119. for path, old_value, new_value in self.iter_changes(self.read_from_file(), values):
  120. if old_value is None:
  121. old_value = "<span class='ui-defaults-none'>None</span>"
  122. text.append(f"<tr><td>{path}</td><td>{old_value}</td><td>{new_value}</td></tr>")
  123. if len(text) == 1:
  124. text.append("<tr><td colspan=3>No changes</td></tr>")
  125. text.append("</tbody>")
  126. return "".join(text)
  127. def ui_apply(self, *values):
  128. num_changed = 0
  129. current_ui_settings = self.read_from_file()
  130. for path, _, new_value in self.iter_changes(current_ui_settings.copy(), values):
  131. num_changed += 1
  132. current_ui_settings[path] = new_value
  133. if num_changed == 0:
  134. return "No changes."
  135. self.write_to_file(current_ui_settings)
  136. return f"Wrote {num_changed} changes."
  137. def create_ui(self):
  138. """creates ui elements for editing defaults UI, without adding any logic to them"""
  139. gr.HTML(
  140. f"This page allows you to change default values in UI elements on other tabs.<br />"
  141. f"Make your changes, press 'View changes' to review the changed default values,<br />"
  142. f"then press 'Apply' to write them to {self.filename}.<br />"
  143. f"New defaults will apply after you restart the UI.<br />"
  144. )
  145. with gr.Row():
  146. self.ui_defaults_view = gr.Button(value='View changes', elem_id="ui_defaults_view", variant="secondary")
  147. self.ui_defaults_apply = gr.Button(value='Apply', elem_id="ui_defaults_apply", variant="primary")
  148. self.ui_defaults_review = gr.HTML("")
  149. def setup_ui(self):
  150. """adds logic to elements created with create_ui; all add_block class must be made before this"""
  151. assert not self.finalized_ui
  152. self.finalized_ui = True
  153. self.ui_defaults_view.click(fn=self.ui_view, inputs=list(self.component_mapping.values()), outputs=[self.ui_defaults_review])
  154. self.ui_defaults_apply.click(fn=self.ui_apply, inputs=list(self.component_mapping.values()), outputs=[self.ui_defaults_review])