2023-01-01 10:08:40 +00:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class FormComponent:
|
|
|
|
def get_expected_parent(self):
|
|
|
|
return gr.components.Form
|
2023-01-01 10:08:40 +00:00
|
|
|
|
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
gr.Dropdown.get_expected_parent = FormComponent.get_expected_parent
|
2023-01-01 10:08:40 +00:00
|
|
|
|
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class ToolButton(FormComponent, gr.Button):
|
|
|
|
"""Small button with single emoji as text, fits inside gradio forms"""
|
2023-01-21 05:36:07 +00:00
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
classes = kwargs.pop("elem_classes", [])
|
|
|
|
super().__init__(*args, elem_classes=["tool", *classes], **kwargs)
|
2023-01-21 05:36:07 +00:00
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "button"
|
|
|
|
|
|
|
|
|
2023-08-21 05:48:22 +00:00
|
|
|
class ResizeHandleRow(gr.Row):
|
|
|
|
"""Same as gr.Row but fits inside gradio forms"""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
self.elem_classes.append("resize-handle-row")
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "row"
|
|
|
|
|
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class FormRow(FormComponent, gr.Row):
|
2023-01-01 10:08:40 +00:00
|
|
|
"""Same as gr.Row but fits inside gradio forms"""
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "row"
|
2023-01-03 06:04:29 +00:00
|
|
|
|
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class FormColumn(FormComponent, gr.Column):
|
|
|
|
"""Same as gr.Column but fits inside gradio forms"""
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "column"
|
|
|
|
|
|
|
|
|
|
|
|
class FormGroup(FormComponent, gr.Group):
|
2023-08-04 04:50:17 +00:00
|
|
|
"""Same as gr.Group but fits inside gradio forms"""
|
2023-01-03 06:04:29 +00:00
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "group"
|
2023-01-07 06:56:37 +00:00
|
|
|
|
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class FormHTML(FormComponent, gr.HTML):
|
2023-01-07 06:56:37 +00:00
|
|
|
"""Same as gr.HTML but fits inside gradio forms"""
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "html"
|
|
|
|
|
2023-01-10 20:47:02 +00:00
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class FormColorPicker(FormComponent, gr.ColorPicker):
|
2023-01-10 20:47:02 +00:00
|
|
|
"""Same as gr.ColorPicker but fits inside gradio forms"""
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "colorpicker"
|
2023-01-22 12:38:39 +00:00
|
|
|
|
2023-01-26 20:29:27 +00:00
|
|
|
|
2023-03-20 13:09:36 +00:00
|
|
|
class DropdownMulti(FormComponent, gr.Dropdown):
|
2023-01-26 20:29:27 +00:00
|
|
|
"""Same as gr.Dropdown but always multiselect"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(multiselect=True, **kwargs)
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "dropdown"
|
2023-04-29 09:52:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DropdownEditable(FormComponent, gr.Dropdown):
|
|
|
|
"""Same as gr.Dropdown but allows editing value"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(allow_custom_value=True, **kwargs)
|
|
|
|
|
|
|
|
def get_block_name(self):
|
|
|
|
return "dropdown"
|
|
|
|
|
2023-08-10 08:20:46 +00:00
|
|
|
|
|
|
|
class InputAccordion(gr.Checkbox):
|
2023-08-10 10:02:50 +00:00
|
|
|
"""A gr.Accordion that can be used as an input - returns True if open, False if closed.
|
|
|
|
|
|
|
|
Actaully just a hidden checkbox, but creates an accordion that follows and is followed by the state of the checkbox.
|
|
|
|
"""
|
|
|
|
|
2023-08-10 08:20:46 +00:00
|
|
|
global_index = 0
|
|
|
|
|
|
|
|
def __init__(self, value, **kwargs):
|
|
|
|
self.accordion_id = kwargs.get('elem_id')
|
|
|
|
if self.accordion_id is None:
|
2023-08-10 12:32:54 +00:00
|
|
|
self.accordion_id = f"input-accordion-{InputAccordion.global_index}"
|
|
|
|
InputAccordion.global_index += 1
|
2023-08-10 08:20:46 +00:00
|
|
|
|
2023-08-12 09:39:59 +00:00
|
|
|
kwargs_checkbox = {
|
|
|
|
**kwargs,
|
|
|
|
"elem_id": f"{self.accordion_id}-checkbox",
|
|
|
|
"visible": False,
|
|
|
|
}
|
|
|
|
super().__init__(value, **kwargs_checkbox)
|
2023-08-10 08:20:46 +00:00
|
|
|
|
|
|
|
self.change(fn=None, _js='function(checked){ inputAccordionChecked("' + self.accordion_id + '", checked); }', inputs=[self])
|
|
|
|
|
2023-08-12 09:39:59 +00:00
|
|
|
kwargs_accordion = {
|
|
|
|
**kwargs,
|
|
|
|
"elem_id": self.accordion_id,
|
|
|
|
"label": kwargs.get('label', 'Accordion'),
|
|
|
|
"elem_classes": ['input-accordion'],
|
|
|
|
"open": value,
|
|
|
|
}
|
|
|
|
self.accordion = gr.Accordion(**kwargs_accordion)
|
2023-08-10 08:20:46 +00:00
|
|
|
|
|
|
|
def extra(self):
|
2023-08-10 10:02:50 +00: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)
|
|
|
|
|
|
|
|
...
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2023-08-10 08:20:46 +00:00
|
|
|
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"
|
|
|
|
|