Spaces:
Running
on
Zero
Features of the New UI Designer:
Browse filesDynamic Field Addition: Users can dynamically add fields with custom types (Textbox, Number, Dropdown, Slider, Checkbox) and set appropriate attributes such as labels, placeholders, min/max values, and dropdown options.
Visibility Control: Depending on the selected field type, only the relevant attributes are shown (e.g., placeholders for Textboxes, min/max values for Number/Slider fields, and choices for Dropdowns).
Preview Added Fields: The gr.JSON component allows the user to preview all the fields they have added in JSON format, making it easier to review the design.
Code Generation: Once all fields are added, users can generate the Gradio code, which includes the corresponding input components and a basic function that processes user inputs.
Example Workflow:
Select a component type (e.g., Textbox) and specify a label like "Name". Optionally set a placeholder (e.g., "Enter your name").
Add another component type (e.g., Slider) and set labels and min/max values.
Add as many fields as required, and the list of added components will be shown.
Finally, click the "Generate Code" button to generate the Python code for your Gradio interface.
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
import json
|
3 |
|
4 |
# Function to generate Gradio code dynamically
|
5 |
def generate_gradio_code(interface_name, components):
|
@@ -39,6 +38,30 @@ def generate_gradio_code(interface_name, components):
|
|
39 |
|
40 |
return code
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Gradio UI for designing the interface
|
43 |
def ui():
|
44 |
with gr.Blocks() as demo:
|
@@ -57,47 +80,32 @@ def ui():
|
|
57 |
# Components list to track fields
|
58 |
components_list = gr.State([])
|
59 |
|
60 |
-
|
61 |
-
new_component = {"type": component_type, "label": field_label}
|
62 |
-
|
63 |
-
if component_type == "Textbox":
|
64 |
-
new_component["placeholder"] = placeholder_text
|
65 |
-
elif component_type in ["Number", "Slider"]:
|
66 |
-
new_component["min_value"] = min_value
|
67 |
-
new_component["max_value"] = max_value
|
68 |
-
elif component_type == "Dropdown":
|
69 |
-
new_component["choices"] = choices.split(',')
|
70 |
-
|
71 |
-
components.append(new_component)
|
72 |
-
return gr.JSON.update(value=components), components
|
73 |
-
|
74 |
-
def update_visibility(component_type):
|
75 |
-
return (
|
76 |
-
gr.update(visible=component_type == "Textbox"),
|
77 |
-
gr.update(visible=component_type in ["Slider", "Number"]),
|
78 |
-
gr.update(visible=component_type == "Dropdown"),
|
79 |
-
gr.update(visible=component_type in ["Slider", "Number"])
|
80 |
-
)
|
81 |
-
|
82 |
add_button = gr.Button("Add Field")
|
83 |
component_preview = gr.JSON(label="Component List")
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
89 |
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Preview and generate code button
|
94 |
generate_button = gr.Button("Generate Code")
|
95 |
generated_code = gr.Code(label="Generated Code")
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
101 |
|
102 |
return demo
|
103 |
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
# Function to generate Gradio code dynamically
|
4 |
def generate_gradio_code(interface_name, components):
|
|
|
38 |
|
39 |
return code
|
40 |
|
41 |
+
# Function to handle dynamic field addition
|
42 |
+
def add_field(component_type, field_label, placeholder_text, min_value, max_value, choices, components):
|
43 |
+
new_component = {"type": component_type, "label": field_label}
|
44 |
+
|
45 |
+
if component_type == "Textbox":
|
46 |
+
new_component["placeholder"] = placeholder_text
|
47 |
+
elif component_type in ["Number", "Slider"]:
|
48 |
+
new_component["min_value"] = min_value
|
49 |
+
new_component["max_value"] = max_value
|
50 |
+
elif component_type == "Dropdown":
|
51 |
+
new_component["choices"] = choices.split(',')
|
52 |
+
|
53 |
+
components.append(new_component)
|
54 |
+
return components, components
|
55 |
+
|
56 |
+
# Function to update the visibility of specific options based on the component type
|
57 |
+
def update_visibility(component_type):
|
58 |
+
return (
|
59 |
+
gr.update(visible=component_type == "Textbox"),
|
60 |
+
gr.update(visible=component_type in ["Slider", "Number"]),
|
61 |
+
gr.update(visible=component_type == "Dropdown"),
|
62 |
+
gr.update(visible=component_type in ["Slider", "Number"])
|
63 |
+
)
|
64 |
+
|
65 |
# Gradio UI for designing the interface
|
66 |
def ui():
|
67 |
with gr.Blocks() as demo:
|
|
|
80 |
# Components list to track fields
|
81 |
components_list = gr.State([])
|
82 |
|
83 |
+
# Add field button and component preview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
add_button = gr.Button("Add Field")
|
85 |
component_preview = gr.JSON(label="Component List")
|
86 |
|
87 |
+
add_button.click(
|
88 |
+
add_field,
|
89 |
+
inputs=[component_type, field_label, placeholder_text, min_value, max_value, choices, components_list],
|
90 |
+
outputs=[component_preview, components_list]
|
91 |
+
)
|
92 |
|
93 |
+
# Update visibility based on the field type
|
94 |
+
component_type.change(
|
95 |
+
update_visibility,
|
96 |
+
inputs=[component_type],
|
97 |
+
outputs=[placeholder_text, min_value, choices, max_value]
|
98 |
+
)
|
99 |
|
100 |
# Preview and generate code button
|
101 |
generate_button = gr.Button("Generate Code")
|
102 |
generated_code = gr.Code(label="Generated Code")
|
103 |
|
104 |
+
generate_button.click(
|
105 |
+
generate_gradio_code,
|
106 |
+
inputs=[interface_name, components_list],
|
107 |
+
outputs=generated_code
|
108 |
+
)
|
109 |
|
110 |
return demo
|
111 |
|