File size: 8,347 Bytes
e1a6cb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from __future__ import annotations
from gradio.components.base import Component
from gradio.events import Events
import json

class GradioDesigner(Component):
    """
    A visual designer component for building Gradio layouts with all components
    """
    
    EVENTS = [Events.change, Events.input]
    
    # Complete component definitions with their properties
    COMPONENT_DEFINITIONS = {
        "Textbox": {
            "properties": ["label", "placeholder", "lines", "max_length", "type", "value"],
            "defaults": {"label": "Text Input", "placeholder": "Enter text...", "lines": 1, "value": ""},
            "icon": "πŸ“",
            "category": "Input"
        },
        "TextArea": {
            "properties": ["label", "placeholder", "lines", "max_length", "value"],
            "defaults": {"label": "Text Area", "placeholder": "Enter multiple lines...", "lines": 3, "value": ""},
            "icon": "πŸ“„",
            "category": "Input"
        },
        "Button": {
            "properties": ["value", "variant", "size"],
            "defaults": {"value": "Click me", "variant": "secondary", "size": "sm"},
            "icon": "πŸ”˜",
            "category": "Action"
        },
        "Slider": {
            "properties": ["minimum", "maximum", "step", "value", "label"],
            "defaults": {"label": "Slider", "minimum": 0, "maximum": 100, "step": 1, "value": 50},
            "icon": "🎚️",
            "category": "Input"
        },
        "Number": {
            "properties": ["label", "value", "precision"],
            "defaults": {"label": "Number", "value": 0, "precision": 0},
            "icon": "πŸ”’",
            "category": "Input"
        },
        "Checkbox": {
            "properties": ["label", "value"],
            "defaults": {"label": "Checkbox", "value": False},
            "icon": "β˜‘οΈ",
            "category": "Input"
        },
        "CheckboxGroup": {
            "properties": ["label", "choices", "value"],
            "defaults": {"label": "Checkbox Group", "choices": ["Option 1", "Option 2"], "value": []},
            "icon": "β˜‘οΈ",
            "category": "Input"
        },
        "Radio": {
            "properties": ["label", "choices", "value"],
            "defaults": {"label": "Radio", "choices": ["Option 1", "Option 2"], "value": "Option 1"},
            "icon": "πŸ”˜",
            "category": "Input"
        },
        "Dropdown": {
            "properties": ["label", "choices", "value", "multiselect"],
            "defaults": {"label": "Dropdown", "choices": ["Option 1", "Option 2"], "value": "Option 1", "multiselect": False},
            "icon": "πŸ“‹",
            "category": "Input"
        },
        "Toggle": {
            "properties": ["label", "value"],
            "defaults": {"label": "Toggle", "value": False},
            "icon": "πŸ”„",
            "category": "Input"
        },
        "ColorPicker": {
            "properties": ["label", "value"],
            "defaults": {"label": "Color Picker", "value": "#ff0000"},
            "icon": "🎨",
            "category": "Input"
        },
        "Date": {
            "properties": ["label", "value"],
            "defaults": {"label": "Date", "value": "2025-01-01"},
            "icon": "πŸ“…",
            "category": "Input"
        },
        "Time": {
            "properties": ["label", "value"],
            "defaults": {"label": "Time", "value": "12:00"},
            "icon": "⏰",
            "category": "Input"
        },
        "File": {
            "properties": ["label", "file_types"],
            "defaults": {"label": "Upload File", "file_types": [".txt", ".pdf"]},
            "icon": "πŸ“",
            "category": "Input"
        },
        "Image": {
            "properties": ["label", "type", "tool", "interactive"],
            "defaults": {"label": "Image", "type": "pil", "interactive": True},
            "icon": "πŸ–ΌοΈ",
            "category": "Media"
        },
        "Video": {
            "properties": ["label", "format"],
            "defaults": {"label": "Video", "format": "mp4"},
            "icon": "πŸŽ₯",
            "category": "Media"
        },
        "Audio": {
            "properties": ["label"],
            "defaults": {"label": "Audio"},
            "icon": "🎡",
            "category": "Media"
        },
        "Dataframe": {
            "properties": ["headers", "datatype", "value"],
            "defaults": {"headers": ["Column 1", "Column 2"], "datatype": ["str", "str"], "value": []},
            "icon": "πŸ“Š",
            "category": "Data"
        },
        "JSON": {
            "properties": ["value"],
            "defaults": {"value": "{}"},
            "icon": "πŸ“‹",
            "category": "Data"
        },
        "Markdown": {
            "properties": ["value"],
            "defaults": {"value": "# Markdown Text"},
            "icon": "πŸ“",
            "category": "Display"
        },
        "HTML": {
            "properties": ["value"],
            "defaults": {"value": "<p>HTML Content</p>"},
            "icon": "🌐",
            "category": "Display"
        },
        "Label": {
            "properties": ["value"],
            "defaults": {"value": "Label Text"},
            "icon": "🏷️",
            "category": "Display"
        },
        "Progress": {
            "properties": ["value"],
            "defaults": {"value": 0.5},
            "icon": "πŸ“ˆ",
            "category": "Display"
        }
    }
    
    def __init__(
        self,
        value: dict | None = None,
        label: str | None = None,
        **kwargs,
    ):
        self.value = value or {"components": [], "layout": "blocks"}
        super().__init__(label=label, **kwargs)

    def preprocess(self, payload: dict | None) -> dict | None:
        """Process the layout configuration from frontend"""
        if payload is None:
            return None
        return payload

    def postprocess(self, value: dict | None) -> dict | None:
        """Send layout configuration to frontend"""
        if value is None:
            return {"components": [], "layout": "blocks"}
        return value
    
    def api_info(self) -> dict[str, list[str]]:
        """API info for the component"""
        return {
            "info": {
                "type": "object",
                "properties": {
                    "components": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "id": {"type": "string"},
                                "type": {"type": "string"},
                                "position": {
                                    "type": "object",
                                    "properties": {
                                        "x": {"type": "number"},
                                        "y": {"type": "number"}
                                    }
                                },
                                "size": {
                                    "type": "object", 
                                    "properties": {
                                        "width": {"type": "number"},
                                        "height": {"type": "number"}
                                    }
                                },
                                "props": {"type": "object"}
                            }
                        }
                    },
                    "layout": {"type": "string"}
                }
            },
            "serialized_info": False
        }
    
    def get_component_definitions(self):
        """Get all component definitions for frontend"""
        return self.COMPONENT_DEFINITIONS
    
    def example_payload(self) -> dict:
        return {
            "components": [
                {
                    "id": "textbox_1",
                    "type": "Textbox", 
                    "position": {"x": 100, "y": 50},
                    "size": {"width": 200, "height": 100},
                    "props": {"label": "Input", "placeholder": "Enter text..."}
                }
            ],
            "layout": "blocks"
        }
    
    def example_value(self) -> dict:
        return self.example_payload()