stable-diffusion-webui/modules/ui_components.py

171 lines
4.7 KiB
Python
Raw Normal View History

2023-12-03 04:42:06 +08:00
from functools import wraps
2023-01-01 18:08:40 +08:00
import gradio as gr
2023-12-03 22:10:13 +08:00
from modules import gradio_extensions # noqa: F401
2023-01-01 18:08:40 +08:00
2023-03-20 21:09:36 +08:00
class FormComponent:
2023-12-03 04:42:06 +08:00
webui_do_not_create_gradio_pyi_thank_you = True
2023-03-20 21:09:36 +08:00
def get_expected_parent(self):
return gr.components.Form
2023-01-01 18:08:40 +08:00
2023-03-20 21:09:36 +08:00
gr.Dropdown.get_expected_parent = FormComponent.get_expected_parent
2023-01-01 18:08:40 +08:00
2023-12-03 04:42:06 +08:00
class ToolButton(gr.Button, FormComponent):
2023-03-20 21:09:36 +08:00
"""Small button with single emoji as text, fits inside gradio forms"""
2023-12-03 04:42:06 +08:00
@wraps(gr.Button.__init__)
def __init__(self, value="", *args, elem_classes=None, **kwargs):
elem_classes = elem_classes or []
2023-12-03 22:10:13 +08:00
super().__init__(*args, elem_classes=["tool", *elem_classes], value=value, **kwargs)
def get_block_name(self):
return "button"
class ResizeHandleRow(gr.Row):
"""Same as gr.Row but fits inside gradio forms"""
2023-12-03 04:42:06 +08:00
webui_do_not_create_gradio_pyi_thank_you = True
2023-12-03 04:42:06 +08:00
@wraps(gr.Row.__init__)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.elem_classes.append("resize-handle-row")
def get_block_name(self):
return "row"
2023-12-03 04:42:06 +08:00
class FormRow(gr.Row, FormComponent):
2023-01-01 18:08:40 +08:00
"""Same as gr.Row but fits inside gradio forms"""
def get_block_name(self):
return "row"
2023-12-03 04:42:06 +08:00
class FormColumn(gr.Column, FormComponent):
2023-03-20 21:09:36 +08:00
"""Same as gr.Column but fits inside gradio forms"""
def get_block_name(self):
return "column"
2023-12-03 04:42:06 +08:00
class FormGroup(gr.Group, FormComponent):
2023-08-04 12:50:17 +08:00
"""Same as gr.Group but fits inside gradio forms"""
def get_block_name(self):
return "group"
2023-12-03 04:42:06 +08:00
class FormHTML(gr.HTML, FormComponent):
"""Same as gr.HTML but fits inside gradio forms"""
def get_block_name(self):
return "html"
2023-12-03 04:42:06 +08:00
class FormColorPicker(gr.ColorPicker, FormComponent):
"""Same as gr.ColorPicker but fits inside gradio forms"""
def get_block_name(self):
return "colorpicker"
2023-12-03 04:42:06 +08:00
class DropdownMulti(gr.Dropdown, FormComponent):
"""Same as gr.Dropdown but always multiselect"""
2023-12-03 04:42:06 +08:00
@wraps(gr.Dropdown.__init__)
def __init__(self, **kwargs):
2023-12-03 04:42:06 +08:00
kwargs['multiselect'] = True
super().__init__(**kwargs)
def get_block_name(self):
return "dropdown"
2023-12-03 04:42:06 +08:00
class DropdownEditable(gr.Dropdown, FormComponent):
"""Same as gr.Dropdown but allows editing value"""
2023-12-03 04:42:06 +08:00
@wraps(gr.Dropdown.__init__)
def __init__(self, **kwargs):
2023-12-03 04:42:06 +08:00
kwargs['allow_custom_value'] = True
super().__init__(**kwargs)
def get_block_name(self):
return "dropdown"
2023-12-03 04:42:06 +08:00
class InputAccordionImpl(gr.Checkbox):
2023-08-10 18:02:50 +08:00
"""A gr.Accordion that can be used as an input - returns True if open, False if closed.
2024-03-04 14:37:23 +08:00
Actually just a hidden checkbox, but creates an accordion that follows and is followed by the state of the checkbox.
2023-08-10 18:02:50 +08:00
"""
2023-12-03 04:42:06 +08:00
webui_do_not_create_gradio_pyi_thank_you = True
global_index = 0
2023-12-03 04:42:06 +08:00
@wraps(gr.Checkbox.__init__)
def __init__(self, value=None, setup=False, **kwargs):
if not setup:
super().__init__(value=value, **kwargs)
return
self.accordion_id = kwargs.get('elem_id')
if self.accordion_id is None:
2023-12-03 04:42:06 +08:00
self.accordion_id = f"input-accordion-{InputAccordionImpl.global_index}"
InputAccordionImpl.global_index += 1
kwargs_checkbox = {
**kwargs,
"elem_id": f"{self.accordion_id}-checkbox",
"visible": False,
}
2023-12-03 04:42:06 +08:00
super().__init__(value=value, **kwargs_checkbox)
self.change(fn=None, _js='function(checked){ inputAccordionChecked("' + self.accordion_id + '", checked); }', inputs=[self])
kwargs_accordion = {
**kwargs,
"elem_id": self.accordion_id,
"label": kwargs.get('label', 'Accordion'),
"elem_classes": ['input-accordion'],
"open": value,
}
2023-12-03 04:42:06 +08:00
self.accordion = gr.Accordion(**kwargs_accordion)
def extra(self):
2023-08-10 18:02:50 +08:00
"""Allows you to put something into the label of the accordion.
Use it like this:
```
with InputAccordion(False, label="Accordion") as acc:
with acc.extra():
FormHTML(value="hello", min_width=0)
...
```
"""
return gr.Column(elem_id=self.accordion_id + '-extra', elem_classes='input-accordion-extra', min_width=0)
def __enter__(self):
self.accordion.__enter__()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.accordion.__exit__(exc_type, exc_val, exc_tb)
def get_block_name(self):
return "checkbox"
2023-12-03 04:42:06 +08:00
def InputAccordion(value=None, **kwargs):
return InputAccordionImpl(value=value, setup=True, **kwargs)