ui_components.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import gradio as gr
  2. class FormComponent:
  3. def get_expected_parent(self):
  4. return gr.components.Form
  5. gr.Dropdown.get_expected_parent = FormComponent.get_expected_parent
  6. class ToolButton(FormComponent, gr.Button):
  7. """Small button with single emoji as text, fits inside gradio forms"""
  8. def __init__(self, *args, **kwargs):
  9. classes = kwargs.pop("elem_classes", [])
  10. super().__init__(*args, elem_classes=["tool", *classes], **kwargs)
  11. def get_block_name(self):
  12. return "button"
  13. class ResizeHandleRow(gr.Row):
  14. """Same as gr.Row but fits inside gradio forms"""
  15. def __init__(self, **kwargs):
  16. super().__init__(**kwargs)
  17. self.elem_classes.append("resize-handle-row")
  18. def get_block_name(self):
  19. return "row"
  20. class FormRow(FormComponent, gr.Row):
  21. """Same as gr.Row but fits inside gradio forms"""
  22. def get_block_name(self):
  23. return "row"
  24. class FormColumn(FormComponent, gr.Column):
  25. """Same as gr.Column but fits inside gradio forms"""
  26. def get_block_name(self):
  27. return "column"
  28. class FormGroup(FormComponent, gr.Group):
  29. """Same as gr.Group but fits inside gradio forms"""
  30. def get_block_name(self):
  31. return "group"
  32. class FormHTML(FormComponent, gr.HTML):
  33. """Same as gr.HTML but fits inside gradio forms"""
  34. def get_block_name(self):
  35. return "html"
  36. class FormColorPicker(FormComponent, gr.ColorPicker):
  37. """Same as gr.ColorPicker but fits inside gradio forms"""
  38. def get_block_name(self):
  39. return "colorpicker"
  40. class DropdownMulti(FormComponent, gr.Dropdown):
  41. """Same as gr.Dropdown but always multiselect"""
  42. def __init__(self, **kwargs):
  43. super().__init__(multiselect=True, **kwargs)
  44. def get_block_name(self):
  45. return "dropdown"
  46. class DropdownEditable(FormComponent, gr.Dropdown):
  47. """Same as gr.Dropdown but allows editing value"""
  48. def __init__(self, **kwargs):
  49. super().__init__(allow_custom_value=True, **kwargs)
  50. def get_block_name(self):
  51. return "dropdown"
  52. class InputAccordion(gr.Checkbox):
  53. """A gr.Accordion that can be used as an input - returns True if open, False if closed.
  54. Actually just a hidden checkbox, but creates an accordion that follows and is followed by the state of the checkbox.
  55. """
  56. global_index = 0
  57. def __init__(self, value, **kwargs):
  58. self.accordion_id = kwargs.get('elem_id')
  59. if self.accordion_id is None:
  60. self.accordion_id = f"input-accordion-{InputAccordion.global_index}"
  61. InputAccordion.global_index += 1
  62. kwargs_checkbox = {
  63. **kwargs,
  64. "elem_id": f"{self.accordion_id}-checkbox",
  65. "visible": False,
  66. }
  67. super().__init__(value, **kwargs_checkbox)
  68. self.change(fn=None, _js='function(checked){ inputAccordionChecked("' + self.accordion_id + '", checked); }', inputs=[self])
  69. kwargs_accordion = {
  70. **kwargs,
  71. "elem_id": self.accordion_id,
  72. "label": kwargs.get('label', 'Accordion'),
  73. "elem_classes": ['input-accordion'],
  74. "open": value,
  75. }
  76. self.accordion = gr.Accordion(**kwargs_accordion)
  77. def extra(self):
  78. """Allows you to put something into the label of the accordion.
  79. Use it like this:
  80. ```
  81. with InputAccordion(False, label="Accordion") as acc:
  82. with acc.extra():
  83. FormHTML(value="hello", min_width=0)
  84. ...
  85. ```
  86. """
  87. return gr.Column(elem_id=self.accordion_id + '-extra', elem_classes='input-accordion-extra', min_width=0)
  88. def __enter__(self):
  89. self.accordion.__enter__()
  90. return self
  91. def __exit__(self, exc_type, exc_val, exc_tb):
  92. self.accordion.__exit__(exc_type, exc_val, exc_tb)
  93. def get_block_name(self):
  94. return "checkbox"