File size: 9,914 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
import gradio as gr
from app import demo as app
import os
_docs = {'GradioDesigner': {'description': 'A visual designer component for building Gradio layouts with all components', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': None}, 'label': {'type': 'str | None', 'default': 'None', 'description': None}}, 'postprocess': {'value': {'type': 'dict | None', 'description': None}}, 'preprocess': {'return': {'type': 'dict | None', 'description': None}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the GradioDesigner changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'input': {'type': None, 'default': None, 'description': 'This listener is triggered when the user changes the value of the GradioDesigner.'}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'GradioDesigner': []}}}
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
with gr.Blocks(
css=abs_path,
theme=gr.themes.Default(
font_mono=[
gr.themes.GoogleFont("Inconsolata"),
"monospace",
],
),
) as demo:
gr.Markdown(
"""
# `gradio_gradiodesigner`
<div style="display: flex; gap: 7px;">
<img alt="Static Badge" src="https://img.shields.io/badge/version%20-%200.0.1%20-%20orange">
</div>
gradio designer
""", elem_classes=["md-custom"], header_links=True)
app.render()
gr.Markdown(
"""
## Installation
```bash
pip install gradio_gradiodesigner
```
## Usage
```python
import gradio as gr
from gradio_gradiodesigner import GradioDesigner
import json
def analyze_design(design_config):
\"\"\"Analyze the design configuration\"\"\"
if not design_config or not isinstance(design_config, dict):
return "No design configuration provided"
components = design_config.get('components', [])
# Count components by type
component_types = {}
for comp in components:
comp_type = comp.get('type', 'Unknown')
component_types[comp_type] = component_types.get(comp_type, 0) + 1
# Calculate coverage area
if components:
positions = [(comp['position']['x'], comp['position']['y']) for comp in components]
min_x, min_y = min(pos[0] for pos in positions), min(pos[1] for pos in positions)
max_x, max_y = max(pos[0] for pos in positions), max(pos[1] for pos in positions)
coverage = f"{max_x - min_x} x {max_y - min_y} pixels"
else:
coverage = "No components"
analysis = f\"\"\"π **Design Analysis**
**Component Summary:**
β’ Total components: {len(components)}
β’ Component types: {dict(component_types)}
β’ Canvas coverage: {coverage}
**Component Details:**
\"\"\"
for i, comp in enumerate(components, 1):
analysis += f"\n{i}. **{comp['type']}** (`{comp['id']}`)"
analysis += f"\n - Position: ({comp['position']['x']}, {comp['position']['y']})"
analysis += f"\n - Size: {comp['size']['width']}Γ{comp['size']['height']}"
if comp.get('props', {}).get('label'):
analysis += f"\n - Label: \"{comp['props']['label']}\""
return analysis
def generate_gradio_code(design_config):
\"\"\"Generate complete Gradio code from design\"\"\"
if not design_config or not isinstance(design_config, dict):
return "# No design to generate code from"
components = design_config.get('components', [])
code = '''import gradio as gr
def process_input(*args):
\"\"\"Process the inputs from your app\"\"\"
return "Hello from your generated app!"
with gr.Blocks(title="Generated Gradio App") as demo:
gr.Markdown("# π Generated Gradio App")
gr.Markdown("This app was generated from your visual design!")
'''
# Sort components by position (top to bottom, left to right)
sorted_components = sorted(components, key=lambda c: (c['position']['y'], c['position']['x']))
component_vars = []
for comp in sorted_components:
comp_type = comp.get('type', 'Textbox')
comp_id = comp.get('id', 'component')
props = comp.get('props', {})
# Build component declaration
prop_parts = []
for key, value in props.items():
if key in ['label', 'placeholder', 'value'] and isinstance(value, str):
prop_parts.append(f'{key}="{value}"')
elif key in ['minimum', 'maximum', 'step', 'lines', 'max_length', 'precision'] and isinstance(value, (int, float)):
prop_parts.append(f'{key}={value}')
elif key == 'choices' and isinstance(value, list):
prop_parts.append(f'{key}={value}')
elif isinstance(value, bool):
prop_parts.append(f'{key}={value}')
prop_string = ", ".join(prop_parts) if prop_parts else ""
code += f" {comp_id} = gr.{comp_type}({prop_string})\n"
component_vars.append(comp_id)
# Add a simple interaction if there are components
if component_vars:
inputs = [var for var in component_vars if not var.startswith('button')]
outputs = [var for var in component_vars if var.startswith('button')]
if not outputs:
outputs = inputs[:1] # Use first input as output if no buttons
if inputs and outputs:
code += f"\n # Add interactions\n"
code += f" # Example: connect inputs to outputs\n"
code += f" # {outputs[0]}.click(process_input, inputs=[{', '.join(inputs)}], outputs=[{outputs[0]}])\n"
code += '''
if __name__ == "__main__":
demo.launch()
'''
return code
with gr.Blocks(title="Gradio Visual Designer Pro", theme=gr.themes.Soft()) as demo:
gr.Markdown(\"\"\"
# π¨ Gradio Visual Designer Pro
**Build your Gradio apps visually!** Drag and drop components, customize properties, and generate production-ready code.
**Features:** 25+ Gradio components β’ Real-time editing β’ Code generation β’ Export options
\"\"\")
with gr.Row():
designer = GradioDesigner(
label="Visual App Designer",
value={"components": [], "layout": "blocks"}
)
with gr.Row():
with gr.Column(scale=1):
analysis_output = gr.Markdown(
value="Design analysis will appear here...",
label="Design Analysis"
)
with gr.Column(scale=1):
code_output = gr.Code(
label="Generated Gradio Code",
language="python",
value="# Design your app above to see generated code",
lines=20
)
with gr.Row():
analyze_btn = gr.Button("π Analyze Design", variant="secondary")
generate_btn = gr.Button("π Generate Code", variant="primary")
clear_btn = gr.Button("ποΈ Clear All", variant="stop")
# Event handlers
designer.change(
fn=analyze_design,
inputs=[designer],
outputs=[analysis_output]
)
analyze_btn.click(
fn=analyze_design,
inputs=[designer],
outputs=[analysis_output]
)
generate_btn.click(
fn=generate_gradio_code,
inputs=[designer],
outputs=[code_output]
)
clear_btn.click(
fn=lambda: {"components": [], "layout": "blocks"},
outputs=[designer]
)
if __name__ == "__main__":
demo.launch()
```
""", elem_classes=["md-custom"], header_links=True)
gr.Markdown("""
## `GradioDesigner`
### Initialization
""", elem_classes=["md-custom"], header_links=True)
gr.ParamViewer(value=_docs["GradioDesigner"]["members"]["__init__"], linkify=[])
gr.Markdown("### Events")
gr.ParamViewer(value=_docs["GradioDesigner"]["events"], linkify=['Event'])
gr.Markdown("""
### User function
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
```python
def predict(
value: dict | None
) -> dict | None:
return value
```
""", elem_classes=["md-custom", "GradioDesigner-user-fn"], header_links=True)
demo.load(None, js=r"""function() {
const refs = {};
const user_fn_refs = {
GradioDesigner: [], };
requestAnimationFrame(() => {
Object.entries(user_fn_refs).forEach(([key, refs]) => {
if (refs.length > 0) {
const el = document.querySelector(`.${key}-user-fn`);
if (!el) return;
refs.forEach(ref => {
el.innerHTML = el.innerHTML.replace(
new RegExp("\\b"+ref+"\\b", "g"),
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
);
})
}
})
Object.entries(refs).forEach(([key, refs]) => {
if (refs.length > 0) {
const el = document.querySelector(`.${key}`);
if (!el) return;
refs.forEach(ref => {
el.innerHTML = el.innerHTML.replace(
new RegExp("\\b"+ref+"\\b", "g"),
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
);
})
}
})
})
}
""")
demo.launch()
|