Upload folder using huggingface_hub
Browse files- README.md +65 -117
- app.py +64 -116
- custom.css +11 -7
- custom.js +66 -17
- requirements.txt +1 -1
- space.py +64 -116
- src/README.md +65 -117
- src/custom.css +11 -7
- src/custom.js +66 -17
- src/demo/app.py +64 -116
- src/demo/custom.css +11 -7
- src/demo/custom.js +66 -17
- src/demo/requirements.txt +1 -1
- src/demo/space.py +64 -116
- src/pyproject.toml +2 -2
README.md
CHANGED
|
@@ -36,9 +36,9 @@ The **PropertySheet** component for Gradio allows you to automatically generate
|
|
| 36 |
- **Theme-Aware**: Designed to look and feel native in all Gradio themes.
|
| 37 |
- **Dynamic Updates**: Supports advanced patterns where changing one field (e.g., a model selector) can dynamically update the options of another field (e.g., a sampler dropdown).
|
| 38 |
|
| 39 |
-
|
| 40 |
## Installation
|
| 41 |
|
|
|
|
| 42 |
```bash
|
| 43 |
pip install gradio_propertysheet
|
| 44 |
```
|
|
@@ -47,183 +47,117 @@ pip install gradio_propertysheet
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import os
|
| 50 |
-
|
| 51 |
import gradio as gr
|
| 52 |
from dataclasses import dataclass, field, asdict
|
| 53 |
from typing import Literal
|
| 54 |
from gradio_propertysheet import PropertySheet
|
| 55 |
-
from
|
| 56 |
-
|
| 57 |
|
| 58 |
-
# --- 1. Dataclass Definitions ---
|
| 59 |
-
|
| 60 |
-
# Dataclasses for the Original Sidebar Demo
|
| 61 |
@dataclass
|
| 62 |
class ModelSettings:
|
| 63 |
-
"""Settings for loading models, VAEs, etc."""
|
| 64 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 65 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 66 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 67 |
-
|
| 68 |
@dataclass
|
| 69 |
class SamplingSettings:
|
| 70 |
-
"""Settings for the image sampling process."""
|
| 71 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 72 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 73 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
| 74 |
-
|
| 75 |
@dataclass
|
| 76 |
class RenderConfig:
|
| 77 |
-
"""Main configuration object for rendering, grouping all settings."""
|
| 78 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 79 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 80 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 81 |
-
|
| 82 |
@dataclass
|
| 83 |
class Lighting:
|
| 84 |
-
"""Lighting settings for the environment."""
|
| 85 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 86 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
| 87 |
-
|
| 88 |
@dataclass
|
| 89 |
class EnvironmentConfig:
|
| 90 |
-
"""Main configuration for the environment."""
|
| 91 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 92 |
lighting: Lighting = field(default_factory=Lighting)
|
| 93 |
-
|
| 94 |
-
# Dataclasses for the Flyout Demo
|
| 95 |
@dataclass
|
| 96 |
class EulerSettings:
|
| 97 |
-
"""Settings specific to the Euler sampler."""
|
| 98 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
| 99 |
-
|
| 100 |
@dataclass
|
| 101 |
class DPM_Settings:
|
| 102 |
-
"""Settings specific to DPM samplers."""
|
| 103 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 104 |
|
| 105 |
-
# --- 2. Data Mappings and Initial Instances ---
|
| 106 |
-
|
| 107 |
-
# Data for Original Sidebar Demo
|
| 108 |
initial_render_config = RenderConfig()
|
| 109 |
initial_env_config = EnvironmentConfig()
|
| 110 |
-
|
| 111 |
-
# Data for Flyout Demo
|
| 112 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 113 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 114 |
|
| 115 |
-
# --- 3. CSS & JS Injection function ---
|
| 116 |
-
|
| 117 |
def inject_assets():
|
| 118 |
"""
|
| 119 |
-
This function prepares the payload of CSS
|
| 120 |
-
demo.load() event listener when the Gradio app starts.
|
| 121 |
"""
|
| 122 |
-
|
| 123 |
css_code = ""
|
| 124 |
js_code = ""
|
| 125 |
-
|
| 126 |
-
# Read from files
|
| 127 |
try:
|
| 128 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 129 |
css_code += f.read() + "\n"
|
| 130 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 131 |
js_code += f.read() + "\n"
|
| 132 |
except FileNotFoundError as e:
|
| 133 |
print(f"Warning: Could not read asset file: {e}")
|
| 134 |
-
|
| 135 |
-
return {"js": js_code, "css": css_code}
|
| 136 |
-
|
| 137 |
|
| 138 |
# --- 4. Gradio App Build ---
|
| 139 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 140 |
-
|
| 141 |
gr.Markdown("# PropertySheet Component Demos")
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
with gr.Tabs():
|
| 144 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 145 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
| 146 |
-
|
| 147 |
render_state = gr.State(value=initial_render_config)
|
| 148 |
env_state = gr.State(value=initial_env_config)
|
| 149 |
sidebar_visible = gr.State(False)
|
| 150 |
-
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=3):
|
| 153 |
generate = gr.Button("Show Settings", variant="primary")
|
| 154 |
with gr.Row():
|
| 155 |
output_render_json = gr.JSON(label="Live Render State")
|
| 156 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 157 |
-
|
| 158 |
with gr.Column(scale=1):
|
| 159 |
-
render_sheet = PropertySheet(
|
| 160 |
-
|
| 161 |
-
label="Render Settings",
|
| 162 |
-
width=400,
|
| 163 |
-
height=550,
|
| 164 |
-
visible=False,
|
| 165 |
-
root_label="Generator"
|
| 166 |
-
)
|
| 167 |
-
environment_sheet = PropertySheet(
|
| 168 |
-
value=initial_env_config,
|
| 169 |
-
label="Environment Settings",
|
| 170 |
-
width=400,
|
| 171 |
-
open=False,
|
| 172 |
-
visible=False,
|
| 173 |
-
root_label="General"
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 177 |
new_visibility = not is_visible
|
| 178 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 179 |
-
return (
|
| 180 |
-
new_visibility,
|
| 181 |
-
gr.update(visible=new_visibility, value=render_cfg),
|
| 182 |
-
gr.update(visible=new_visibility, value=env_cfg),
|
| 183 |
-
gr.update(value=button_text)
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 187 |
-
if updated_config is None:
|
| 188 |
-
|
| 189 |
-
if updated_config.model.model_type != "Custom":
|
| 190 |
-
updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
| 191 |
return updated_config, asdict(updated_config), updated_config
|
| 192 |
-
|
| 193 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 194 |
-
if updated_config is None:
|
| 195 |
-
return current_state, asdict(current_state), current_state
|
| 196 |
return updated_config, asdict(updated_config), current_state
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
outputs=[sidebar_visible, render_sheet, environment_sheet, generate]
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
render_sheet.change(
|
| 205 |
-
fn=handle_render_change,
|
| 206 |
-
inputs=[render_sheet, render_state],
|
| 207 |
-
outputs=[render_sheet, output_render_json, render_state]
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
environment_sheet.change(
|
| 211 |
-
fn=handle_env_change,
|
| 212 |
-
inputs=[environment_sheet, env_state],
|
| 213 |
-
outputs=[environment_sheet, output_env_json, env_state]
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
-
demo.load(
|
| 217 |
-
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 218 |
-
inputs=[render_state, env_state],
|
| 219 |
-
outputs=[output_render_json, output_env_json]
|
| 220 |
-
)
|
| 221 |
|
| 222 |
with gr.TabItem("Flyout Popup Demo"):
|
| 223 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 224 |
|
|
|
|
| 225 |
flyout_visible = gr.State(False)
|
| 226 |
active_anchor_id = gr.State(None)
|
|
|
|
| 227 |
|
| 228 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 229 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
@@ -234,54 +168,60 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 234 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 235 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 236 |
|
| 237 |
-
|
| 238 |
-
with gr.Row(elem_classes=["close-btn-row"]):
|
| 239 |
-
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 240 |
-
flyout_sheet = PropertySheet(visible=False, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 241 |
|
|
|
|
| 242 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 243 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 244 |
-
|
|
|
|
| 245 |
else:
|
| 246 |
-
|
|
|
|
| 247 |
|
| 248 |
def update_ear_visibility(selection, settings_map):
|
| 249 |
has_settings = settings_map.get(selection) is not None
|
| 250 |
return gr.update(visible=has_settings)
|
| 251 |
|
| 252 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 253 |
-
if updated_settings is None or active_id is None:
|
|
|
|
| 254 |
if active_id == sampler_dd.elem_id:
|
| 255 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 256 |
elif active_id == model_dd.elem_id:
|
| 257 |
model_settings_map_py[model_val] = updated_settings
|
| 258 |
|
| 259 |
def close_the_flyout():
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
sampler_dd.change(
|
| 263 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 264 |
inputs=[sampler_dd],
|
| 265 |
outputs=[sampler_ear_btn]
|
| 266 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 267 |
|
| 268 |
sampler_ear_btn.click(
|
| 269 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 270 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 271 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 272 |
-
).then(fn=None, inputs=
|
| 273 |
|
| 274 |
model_dd.change(
|
| 275 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 276 |
inputs=[model_dd],
|
| 277 |
outputs=[model_ear_btn]
|
| 278 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 279 |
|
| 280 |
model_ear_btn.click(
|
| 281 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 282 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 283 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 284 |
-
).then(fn=None, inputs=
|
| 285 |
|
| 286 |
flyout_sheet.change(
|
| 287 |
fn=on_flyout_change,
|
|
@@ -292,16 +232,24 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 292 |
close_btn.click(
|
| 293 |
fn=close_the_flyout,
|
| 294 |
inputs=None,
|
| 295 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 296 |
-
)
|
| 297 |
|
| 298 |
def initial_flyout_setup(sampler_val, model_val):
|
| 299 |
return {
|
| 300 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 301 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 302 |
-
}
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
if __name__ == "__main__":
|
| 307 |
demo.launch()
|
|
|
|
| 36 |
- **Theme-Aware**: Designed to look and feel native in all Gradio themes.
|
| 37 |
- **Dynamic Updates**: Supports advanced patterns where changing one field (e.g., a model selector) can dynamically update the options of another field (e.g., a sampler dropdown).
|
| 38 |
|
|
|
|
| 39 |
## Installation
|
| 40 |
|
| 41 |
+
|
| 42 |
```bash
|
| 43 |
pip install gradio_propertysheet
|
| 44 |
```
|
|
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import os
|
| 50 |
+
import json
|
| 51 |
import gradio as gr
|
| 52 |
from dataclasses import dataclass, field, asdict
|
| 53 |
from typing import Literal
|
| 54 |
from gradio_propertysheet import PropertySheet
|
| 55 |
+
from gradio_htmlinjector import HTMLInjector
|
|
|
|
| 56 |
|
| 57 |
+
# --- 1. Dataclass Definitions (unchanged) ---
|
|
|
|
|
|
|
| 58 |
@dataclass
|
| 59 |
class ModelSettings:
|
|
|
|
| 60 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 61 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 62 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
| 63 |
@dataclass
|
| 64 |
class SamplingSettings:
|
|
|
|
| 65 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 66 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 67 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
|
|
|
| 68 |
@dataclass
|
| 69 |
class RenderConfig:
|
|
|
|
| 70 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 71 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 72 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class Lighting:
|
|
|
|
| 75 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 76 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
|
|
|
| 77 |
@dataclass
|
| 78 |
class EnvironmentConfig:
|
|
|
|
| 79 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 80 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 81 |
@dataclass
|
| 82 |
class EulerSettings:
|
|
|
|
| 83 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
|
|
|
| 84 |
@dataclass
|
| 85 |
class DPM_Settings:
|
|
|
|
| 86 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 87 |
|
| 88 |
+
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
|
|
|
|
|
|
| 89 |
initial_render_config = RenderConfig()
|
| 90 |
initial_env_config = EnvironmentConfig()
|
|
|
|
|
|
|
| 91 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 92 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 93 |
|
| 94 |
+
# --- 3. CSS & JS Injection function (unchanged) ---
|
|
|
|
| 95 |
def inject_assets():
|
| 96 |
"""
|
| 97 |
+
This function prepares the payload of CSS, JS, and Body HTML for injection.
|
|
|
|
| 98 |
"""
|
| 99 |
+
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 100 |
css_code = ""
|
| 101 |
js_code = ""
|
| 102 |
+
|
|
|
|
| 103 |
try:
|
| 104 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 105 |
css_code += f.read() + "\n"
|
| 106 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 107 |
js_code += f.read() + "\n"
|
| 108 |
except FileNotFoundError as e:
|
| 109 |
print(f"Warning: Could not read asset file: {e}")
|
| 110 |
+
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# --- 4. Gradio App Build ---
|
| 113 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 114 |
+
html_injector = HTMLInjector()
|
| 115 |
gr.Markdown("# PropertySheet Component Demos")
|
| 116 |
|
| 117 |
+
with gr.Row():
|
| 118 |
+
# --- Flyout popup ---
|
| 119 |
+
with gr.Column(elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]) as flyout_panel_source:
|
| 120 |
+
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 121 |
+
flyout_sheet = PropertySheet(visible=True, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 122 |
+
|
| 123 |
with gr.Tabs():
|
| 124 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 125 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
|
|
|
| 126 |
render_state = gr.State(value=initial_render_config)
|
| 127 |
env_state = gr.State(value=initial_env_config)
|
| 128 |
sidebar_visible = gr.State(False)
|
|
|
|
| 129 |
with gr.Row():
|
| 130 |
with gr.Column(scale=3):
|
| 131 |
generate = gr.Button("Show Settings", variant="primary")
|
| 132 |
with gr.Row():
|
| 133 |
output_render_json = gr.JSON(label="Live Render State")
|
| 134 |
output_env_json = gr.JSON(label="Live Environment State")
|
|
|
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
+
render_sheet = PropertySheet(value=initial_render_config, label="Render Settings", width=400, height=550, visible=False, root_label="Generator")
|
| 137 |
+
environment_sheet = PropertySheet(value=initial_env_config, label="Environment Settings", width=400, open=False, visible=False, root_label="General")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 139 |
new_visibility = not is_visible
|
| 140 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 141 |
+
return (new_visibility, gr.update(visible=new_visibility, value=render_cfg), gr.update(visible=new_visibility, value=env_cfg), gr.update(value=button_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 143 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
| 144 |
+
if updated_config.model.model_type != "Custom": updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
|
|
|
|
|
|
| 145 |
return updated_config, asdict(updated_config), updated_config
|
|
|
|
| 146 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 147 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
|
|
|
| 148 |
return updated_config, asdict(updated_config), current_state
|
| 149 |
+
generate.click(fn=change_visibility, inputs=[sidebar_visible, render_state, env_state], outputs=[sidebar_visible, render_sheet, environment_sheet, generate])
|
| 150 |
+
render_sheet.change(fn=handle_render_change, inputs=[render_sheet, render_state], outputs=[render_sheet, output_render_json, render_state])
|
| 151 |
+
environment_sheet.change(fn=handle_env_change, inputs=[environment_sheet, env_state], outputs=[environment_sheet, output_env_json, env_state])
|
| 152 |
+
demo.load(fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)), inputs=[render_state, env_state], outputs=[output_render_json, output_env_json])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
with gr.TabItem("Flyout Popup Demo"):
|
| 155 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 156 |
|
| 157 |
+
# --- State Management ---
|
| 158 |
flyout_visible = gr.State(False)
|
| 159 |
active_anchor_id = gr.State(None)
|
| 160 |
+
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 161 |
|
| 162 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 163 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
|
|
| 168 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 169 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 170 |
|
| 171 |
+
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
# --- Event Logic ---
|
| 174 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 175 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 176 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 177 |
+
return False, None, gr.update(), gr.update(value=js_data)
|
| 178 |
else:
|
| 179 |
+
js_data = json.dumps({"isVisible": True, "anchorId": clicked_dropdown_id})
|
| 180 |
+
return True, clicked_dropdown_id, gr.update(value=settings_obj), gr.update(value=js_data)
|
| 181 |
|
| 182 |
def update_ear_visibility(selection, settings_map):
|
| 183 |
has_settings = settings_map.get(selection) is not None
|
| 184 |
return gr.update(visible=has_settings)
|
| 185 |
|
| 186 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 187 |
+
if updated_settings is None or active_id is None:
|
| 188 |
+
return
|
| 189 |
if active_id == sampler_dd.elem_id:
|
| 190 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 191 |
elif active_id == model_dd.elem_id:
|
| 192 |
model_settings_map_py[model_val] = updated_settings
|
| 193 |
|
| 194 |
def close_the_flyout():
|
| 195 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 196 |
+
return False, None, gr.update(value=js_data)
|
| 197 |
+
|
| 198 |
+
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 199 |
|
| 200 |
sampler_dd.change(
|
| 201 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 202 |
inputs=[sampler_dd],
|
| 203 |
outputs=[sampler_ear_btn]
|
| 204 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 205 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 206 |
|
| 207 |
sampler_ear_btn.click(
|
| 208 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 209 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 210 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 211 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 212 |
|
| 213 |
model_dd.change(
|
| 214 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 215 |
inputs=[model_dd],
|
| 216 |
outputs=[model_ear_btn]
|
| 217 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 218 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 219 |
|
| 220 |
model_ear_btn.click(
|
| 221 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 222 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 223 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 224 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 225 |
|
| 226 |
flyout_sheet.change(
|
| 227 |
fn=on_flyout_change,
|
|
|
|
| 232 |
close_btn.click(
|
| 233 |
fn=close_the_flyout,
|
| 234 |
inputs=None,
|
| 235 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 236 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 237 |
|
| 238 |
def initial_flyout_setup(sampler_val, model_val):
|
| 239 |
return {
|
| 240 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 241 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
# --- App Load ---
|
| 245 |
+
demo.load(
|
| 246 |
+
fn=inject_assets, inputs=None, outputs=[html_injector]
|
| 247 |
+
).then(
|
| 248 |
+
fn=initial_flyout_setup, inputs=[sampler_dd, model_dd], outputs=[sampler_ear_btn, model_ear_btn]
|
| 249 |
+
).then(
|
| 250 |
+
fn=None, inputs=None, outputs=None,
|
| 251 |
+
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 252 |
+
)
|
| 253 |
|
| 254 |
if __name__ == "__main__":
|
| 255 |
demo.launch()
|
app.py
CHANGED
|
@@ -1,181 +1,115 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
from dataclasses import dataclass, field, asdict
|
| 5 |
from typing import Literal
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
-
from
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# --- 1. Dataclass Definitions ---
|
| 11 |
-
|
| 12 |
-
# Dataclasses for the Original Sidebar Demo
|
| 13 |
@dataclass
|
| 14 |
class ModelSettings:
|
| 15 |
-
"""Settings for loading models, VAEs, etc."""
|
| 16 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 17 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 18 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 19 |
-
|
| 20 |
@dataclass
|
| 21 |
class SamplingSettings:
|
| 22 |
-
"""Settings for the image sampling process."""
|
| 23 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 24 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 25 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
| 26 |
-
|
| 27 |
@dataclass
|
| 28 |
class RenderConfig:
|
| 29 |
-
"""Main configuration object for rendering, grouping all settings."""
|
| 30 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 31 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 32 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 33 |
-
|
| 34 |
@dataclass
|
| 35 |
class Lighting:
|
| 36 |
-
"""Lighting settings for the environment."""
|
| 37 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 38 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
| 39 |
-
|
| 40 |
@dataclass
|
| 41 |
class EnvironmentConfig:
|
| 42 |
-
"""Main configuration for the environment."""
|
| 43 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 44 |
lighting: Lighting = field(default_factory=Lighting)
|
| 45 |
-
|
| 46 |
-
# Dataclasses for the Flyout Demo
|
| 47 |
@dataclass
|
| 48 |
class EulerSettings:
|
| 49 |
-
"""Settings specific to the Euler sampler."""
|
| 50 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
| 51 |
-
|
| 52 |
@dataclass
|
| 53 |
class DPM_Settings:
|
| 54 |
-
"""Settings specific to DPM samplers."""
|
| 55 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 56 |
|
| 57 |
-
# --- 2. Data Mappings and Initial Instances ---
|
| 58 |
-
|
| 59 |
-
# Data for Original Sidebar Demo
|
| 60 |
initial_render_config = RenderConfig()
|
| 61 |
initial_env_config = EnvironmentConfig()
|
| 62 |
-
|
| 63 |
-
# Data for Flyout Demo
|
| 64 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 65 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 66 |
|
| 67 |
-
# --- 3. CSS & JS Injection function ---
|
| 68 |
-
|
| 69 |
def inject_assets():
|
| 70 |
"""
|
| 71 |
-
This function prepares the payload of CSS
|
| 72 |
-
demo.load() event listener when the Gradio app starts.
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
css_code = ""
|
| 76 |
js_code = ""
|
| 77 |
-
|
| 78 |
-
# Read from files
|
| 79 |
try:
|
| 80 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 81 |
css_code += f.read() + "\n"
|
| 82 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 83 |
js_code += f.read() + "\n"
|
| 84 |
except FileNotFoundError as e:
|
| 85 |
print(f"Warning: Could not read asset file: {e}")
|
| 86 |
-
|
| 87 |
-
return {"js": js_code, "css": css_code}
|
| 88 |
-
|
| 89 |
|
| 90 |
# --- 4. Gradio App Build ---
|
| 91 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 92 |
-
|
| 93 |
gr.Markdown("# PropertySheet Component Demos")
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
with gr.Tabs():
|
| 96 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 97 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
| 98 |
-
|
| 99 |
render_state = gr.State(value=initial_render_config)
|
| 100 |
env_state = gr.State(value=initial_env_config)
|
| 101 |
sidebar_visible = gr.State(False)
|
| 102 |
-
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column(scale=3):
|
| 105 |
generate = gr.Button("Show Settings", variant="primary")
|
| 106 |
with gr.Row():
|
| 107 |
output_render_json = gr.JSON(label="Live Render State")
|
| 108 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 109 |
-
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
-
render_sheet = PropertySheet(
|
| 112 |
-
|
| 113 |
-
label="Render Settings",
|
| 114 |
-
width=400,
|
| 115 |
-
height=550,
|
| 116 |
-
visible=False,
|
| 117 |
-
root_label="Generator"
|
| 118 |
-
)
|
| 119 |
-
environment_sheet = PropertySheet(
|
| 120 |
-
value=initial_env_config,
|
| 121 |
-
label="Environment Settings",
|
| 122 |
-
width=400,
|
| 123 |
-
open=False,
|
| 124 |
-
visible=False,
|
| 125 |
-
root_label="General"
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 129 |
new_visibility = not is_visible
|
| 130 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 131 |
-
return (
|
| 132 |
-
new_visibility,
|
| 133 |
-
gr.update(visible=new_visibility, value=render_cfg),
|
| 134 |
-
gr.update(visible=new_visibility, value=env_cfg),
|
| 135 |
-
gr.update(value=button_text)
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 139 |
-
if updated_config is None:
|
| 140 |
-
|
| 141 |
-
if updated_config.model.model_type != "Custom":
|
| 142 |
-
updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
| 143 |
return updated_config, asdict(updated_config), updated_config
|
| 144 |
-
|
| 145 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 146 |
-
if updated_config is None:
|
| 147 |
-
return current_state, asdict(current_state), current_state
|
| 148 |
return updated_config, asdict(updated_config), current_state
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
outputs=[sidebar_visible, render_sheet, environment_sheet, generate]
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
render_sheet.change(
|
| 157 |
-
fn=handle_render_change,
|
| 158 |
-
inputs=[render_sheet, render_state],
|
| 159 |
-
outputs=[render_sheet, output_render_json, render_state]
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
environment_sheet.change(
|
| 163 |
-
fn=handle_env_change,
|
| 164 |
-
inputs=[environment_sheet, env_state],
|
| 165 |
-
outputs=[environment_sheet, output_env_json, env_state]
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
demo.load(
|
| 169 |
-
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 170 |
-
inputs=[render_state, env_state],
|
| 171 |
-
outputs=[output_render_json, output_env_json]
|
| 172 |
-
)
|
| 173 |
|
| 174 |
with gr.TabItem("Flyout Popup Demo"):
|
| 175 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 176 |
|
|
|
|
| 177 |
flyout_visible = gr.State(False)
|
| 178 |
active_anchor_id = gr.State(None)
|
|
|
|
| 179 |
|
| 180 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 181 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
@@ -186,54 +120,60 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 186 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 187 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 188 |
|
| 189 |
-
|
| 190 |
-
with gr.Row(elem_classes=["close-btn-row"]):
|
| 191 |
-
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 192 |
-
flyout_sheet = PropertySheet(visible=False, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 193 |
|
|
|
|
| 194 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 195 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 196 |
-
|
|
|
|
| 197 |
else:
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
def update_ear_visibility(selection, settings_map):
|
| 201 |
has_settings = settings_map.get(selection) is not None
|
| 202 |
return gr.update(visible=has_settings)
|
| 203 |
|
| 204 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 205 |
-
if updated_settings is None or active_id is None:
|
|
|
|
| 206 |
if active_id == sampler_dd.elem_id:
|
| 207 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 208 |
elif active_id == model_dd.elem_id:
|
| 209 |
model_settings_map_py[model_val] = updated_settings
|
| 210 |
|
| 211 |
def close_the_flyout():
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
sampler_dd.change(
|
| 215 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 216 |
inputs=[sampler_dd],
|
| 217 |
outputs=[sampler_ear_btn]
|
| 218 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 219 |
|
| 220 |
sampler_ear_btn.click(
|
| 221 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 222 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 223 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 224 |
-
).then(fn=None, inputs=
|
| 225 |
|
| 226 |
model_dd.change(
|
| 227 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 228 |
inputs=[model_dd],
|
| 229 |
outputs=[model_ear_btn]
|
| 230 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 231 |
|
| 232 |
model_ear_btn.click(
|
| 233 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 234 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 235 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 236 |
-
).then(fn=None, inputs=
|
| 237 |
|
| 238 |
flyout_sheet.change(
|
| 239 |
fn=on_flyout_change,
|
|
@@ -244,16 +184,24 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 244 |
close_btn.click(
|
| 245 |
fn=close_the_flyout,
|
| 246 |
inputs=None,
|
| 247 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 248 |
-
)
|
| 249 |
|
| 250 |
def initial_flyout_setup(sampler_val, model_val):
|
| 251 |
return {
|
| 252 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 253 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 254 |
-
}
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
| 259 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import gradio as gr
|
| 4 |
from dataclasses import dataclass, field, asdict
|
| 5 |
from typing import Literal
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
+
from gradio_htmlinjector import HTMLInjector
|
| 8 |
|
| 9 |
+
# --- 1. Dataclass Definitions (unchanged) ---
|
|
|
|
|
|
|
|
|
|
| 10 |
@dataclass
|
| 11 |
class ModelSettings:
|
|
|
|
| 12 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 13 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 14 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
| 15 |
@dataclass
|
| 16 |
class SamplingSettings:
|
|
|
|
| 17 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 18 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 19 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
|
|
|
| 20 |
@dataclass
|
| 21 |
class RenderConfig:
|
|
|
|
| 22 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 23 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 24 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
| 25 |
@dataclass
|
| 26 |
class Lighting:
|
|
|
|
| 27 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 28 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
|
|
|
| 29 |
@dataclass
|
| 30 |
class EnvironmentConfig:
|
|
|
|
| 31 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 32 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 33 |
@dataclass
|
| 34 |
class EulerSettings:
|
|
|
|
| 35 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
|
|
|
| 36 |
@dataclass
|
| 37 |
class DPM_Settings:
|
|
|
|
| 38 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 39 |
|
| 40 |
+
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
|
|
|
|
|
|
| 41 |
initial_render_config = RenderConfig()
|
| 42 |
initial_env_config = EnvironmentConfig()
|
|
|
|
|
|
|
| 43 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 44 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 45 |
|
| 46 |
+
# --- 3. CSS & JS Injection function (unchanged) ---
|
|
|
|
| 47 |
def inject_assets():
|
| 48 |
"""
|
| 49 |
+
This function prepares the payload of CSS, JS, and Body HTML for injection.
|
|
|
|
| 50 |
"""
|
| 51 |
+
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 52 |
css_code = ""
|
| 53 |
js_code = ""
|
| 54 |
+
|
|
|
|
| 55 |
try:
|
| 56 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 57 |
css_code += f.read() + "\n"
|
| 58 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 59 |
js_code += f.read() + "\n"
|
| 60 |
except FileNotFoundError as e:
|
| 61 |
print(f"Warning: Could not read asset file: {e}")
|
| 62 |
+
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# --- 4. Gradio App Build ---
|
| 65 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 66 |
+
html_injector = HTMLInjector()
|
| 67 |
gr.Markdown("# PropertySheet Component Demos")
|
| 68 |
|
| 69 |
+
with gr.Row():
|
| 70 |
+
# --- Flyout popup ---
|
| 71 |
+
with gr.Column(elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]) as flyout_panel_source:
|
| 72 |
+
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 73 |
+
flyout_sheet = PropertySheet(visible=True, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 74 |
+
|
| 75 |
with gr.Tabs():
|
| 76 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 77 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
|
|
|
| 78 |
render_state = gr.State(value=initial_render_config)
|
| 79 |
env_state = gr.State(value=initial_env_config)
|
| 80 |
sidebar_visible = gr.State(False)
|
|
|
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column(scale=3):
|
| 83 |
generate = gr.Button("Show Settings", variant="primary")
|
| 84 |
with gr.Row():
|
| 85 |
output_render_json = gr.JSON(label="Live Render State")
|
| 86 |
output_env_json = gr.JSON(label="Live Environment State")
|
|
|
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
+
render_sheet = PropertySheet(value=initial_render_config, label="Render Settings", width=400, height=550, visible=False, root_label="Generator")
|
| 89 |
+
environment_sheet = PropertySheet(value=initial_env_config, label="Environment Settings", width=400, open=False, visible=False, root_label="General")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 91 |
new_visibility = not is_visible
|
| 92 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 93 |
+
return (new_visibility, gr.update(visible=new_visibility, value=render_cfg), gr.update(visible=new_visibility, value=env_cfg), gr.update(value=button_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 95 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
| 96 |
+
if updated_config.model.model_type != "Custom": updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
|
|
|
|
|
|
| 97 |
return updated_config, asdict(updated_config), updated_config
|
|
|
|
| 98 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 99 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
|
|
|
| 100 |
return updated_config, asdict(updated_config), current_state
|
| 101 |
+
generate.click(fn=change_visibility, inputs=[sidebar_visible, render_state, env_state], outputs=[sidebar_visible, render_sheet, environment_sheet, generate])
|
| 102 |
+
render_sheet.change(fn=handle_render_change, inputs=[render_sheet, render_state], outputs=[render_sheet, output_render_json, render_state])
|
| 103 |
+
environment_sheet.change(fn=handle_env_change, inputs=[environment_sheet, env_state], outputs=[environment_sheet, output_env_json, env_state])
|
| 104 |
+
demo.load(fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)), inputs=[render_state, env_state], outputs=[output_render_json, output_env_json])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
with gr.TabItem("Flyout Popup Demo"):
|
| 107 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 108 |
|
| 109 |
+
# --- State Management ---
|
| 110 |
flyout_visible = gr.State(False)
|
| 111 |
active_anchor_id = gr.State(None)
|
| 112 |
+
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 113 |
|
| 114 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 115 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
|
|
| 120 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 121 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 122 |
|
| 123 |
+
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# --- Event Logic ---
|
| 126 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 127 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 128 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 129 |
+
return False, None, gr.update(), gr.update(value=js_data)
|
| 130 |
else:
|
| 131 |
+
js_data = json.dumps({"isVisible": True, "anchorId": clicked_dropdown_id})
|
| 132 |
+
return True, clicked_dropdown_id, gr.update(value=settings_obj), gr.update(value=js_data)
|
| 133 |
|
| 134 |
def update_ear_visibility(selection, settings_map):
|
| 135 |
has_settings = settings_map.get(selection) is not None
|
| 136 |
return gr.update(visible=has_settings)
|
| 137 |
|
| 138 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 139 |
+
if updated_settings is None or active_id is None:
|
| 140 |
+
return
|
| 141 |
if active_id == sampler_dd.elem_id:
|
| 142 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 143 |
elif active_id == model_dd.elem_id:
|
| 144 |
model_settings_map_py[model_val] = updated_settings
|
| 145 |
|
| 146 |
def close_the_flyout():
|
| 147 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 148 |
+
return False, None, gr.update(value=js_data)
|
| 149 |
+
|
| 150 |
+
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 151 |
|
| 152 |
sampler_dd.change(
|
| 153 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 154 |
inputs=[sampler_dd],
|
| 155 |
outputs=[sampler_ear_btn]
|
| 156 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 157 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 158 |
|
| 159 |
sampler_ear_btn.click(
|
| 160 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 161 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 162 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 163 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 164 |
|
| 165 |
model_dd.change(
|
| 166 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 167 |
inputs=[model_dd],
|
| 168 |
outputs=[model_ear_btn]
|
| 169 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 170 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 171 |
|
| 172 |
model_ear_btn.click(
|
| 173 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 174 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 175 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 176 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 177 |
|
| 178 |
flyout_sheet.change(
|
| 179 |
fn=on_flyout_change,
|
|
|
|
| 184 |
close_btn.click(
|
| 185 |
fn=close_the_flyout,
|
| 186 |
inputs=None,
|
| 187 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 188 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 189 |
|
| 190 |
def initial_flyout_setup(sampler_val, model_val):
|
| 191 |
return {
|
| 192 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 193 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
# --- App Load ---
|
| 197 |
+
demo.load(
|
| 198 |
+
fn=inject_assets, inputs=None, outputs=[html_injector]
|
| 199 |
+
).then(
|
| 200 |
+
fn=initial_flyout_setup, inputs=[sampler_dd, model_dd], outputs=[sampler_ear_btn, model_ear_btn]
|
| 201 |
+
).then(
|
| 202 |
+
fn=None, inputs=None, outputs=None,
|
| 203 |
+
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 204 |
+
)
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch()
|
custom.css
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
-
.flyout-context-area {
|
| 2 |
-
position: relative !important;
|
| 3 |
-
overflow: visible !important;
|
| 4 |
}
|
| 5 |
|
| 6 |
-
.flyout-sheet {
|
| 7 |
-
position:
|
| 8 |
flex-grow: 0 !important;
|
| 9 |
-
min-width: unset !important;
|
| 10 |
width: 350px !important;
|
| 11 |
align-self: center !important;
|
| 12 |
z-index: 1000;
|
|
@@ -20,7 +20,7 @@
|
|
| 20 |
.fake-input-container {
|
| 21 |
border: var(--input-border-width) solid var(--border-color-primary);
|
| 22 |
border-radius: var(--input-radius);
|
| 23 |
-
background: var(--
|
| 24 |
padding: 0 !important;
|
| 25 |
align-items: stretch !important;
|
| 26 |
box-shadow: var(--input-shadow);
|
|
@@ -77,4 +77,8 @@
|
|
| 77 |
|
| 78 |
.flyout-close-btn button:hover {
|
| 79 |
color: var(--body-text-color) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
|
|
|
| 1 |
+
.flyout-context-area {
|
| 2 |
+
position: relative !important;
|
| 3 |
+
overflow: visible !important;
|
| 4 |
}
|
| 5 |
|
| 6 |
+
.flyout-sheet {
|
| 7 |
+
position: fixed !important;
|
| 8 |
flex-grow: 0 !important;
|
| 9 |
+
min-width: unset !important;
|
| 10 |
width: 350px !important;
|
| 11 |
align-self: center !important;
|
| 12 |
z-index: 1000;
|
|
|
|
| 20 |
.fake-input-container {
|
| 21 |
border: var(--input-border-width) solid var(--border-color-primary);
|
| 22 |
border-radius: var(--input-radius);
|
| 23 |
+
background: var(--block-background-fill) !important;
|
| 24 |
padding: 0 !important;
|
| 25 |
align-items: stretch !important;
|
| 26 |
box-shadow: var(--input-shadow);
|
|
|
|
| 77 |
|
| 78 |
.flyout-close-btn button:hover {
|
| 79 |
color: var(--body-text-color) !important;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.flyout-source-hidden {
|
| 83 |
+
display: none !important;
|
| 84 |
}
|
custom.js
CHANGED
|
@@ -1,20 +1,69 @@
|
|
| 1 |
-
function
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
const leftPosition = (anchorRect.left - containerRect.left) + (anchorRect.width / 2) - (flyoutWidth / 2);
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
}
|
| 20 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
function reparent_flyout() {
|
| 2 |
+
//console.log("Attempting to reparent flyout...");
|
| 3 |
+
const source = document.getElementById('flyout_panel_source');
|
| 4 |
+
const target = document.getElementById('injected_flyout_container');
|
| 5 |
+
|
| 6 |
+
if (source && target) {
|
| 7 |
+
while (source.firstChild) {
|
| 8 |
+
target.appendChild(source.firstChild);
|
| 9 |
+
}
|
| 10 |
+
source.remove();
|
| 11 |
+
//console.log("SUCCESS: Flyout panel has been reparented.");
|
| 12 |
+
} else {
|
| 13 |
+
console.error("ERROR: Reparenting failed. Source or target element not found.");
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function position_flyout(anchorId) {
|
| 18 |
+
if (!anchorId) {
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const anchorElem = document.getElementById(anchorId);
|
| 23 |
+
const flyoutElem = document.getElementById('injected_flyout_container');
|
| 24 |
+
|
| 25 |
+
if (anchorElem && flyoutElem) {
|
| 26 |
+
//console.log("JS: Positioning flyout relative to:", anchorId);
|
| 27 |
+
const anchorRect = anchorElem.getBoundingClientRect();
|
| 28 |
+
const flyoutWidth = flyoutElem.offsetWidth;
|
| 29 |
+
const flyoutHeight = flyoutElem.offsetHeight;
|
| 30 |
+
|
| 31 |
+
let topPosition = anchorRect.top + (anchorRect.height / 2) - (flyoutHeight / 2);
|
| 32 |
+
let leftPosition = anchorRect.left + (anchorRect.width / 2) - (flyoutWidth / 2);
|
| 33 |
|
| 34 |
+
const windowWidth = window.innerWidth;
|
| 35 |
+
const windowHeight = window.innerHeight;
|
| 36 |
+
if (leftPosition < 8) leftPosition = 8;
|
| 37 |
+
if (topPosition < 8) topPosition = 8;
|
| 38 |
+
if (leftPosition + flyoutWidth > windowWidth) leftPosition = windowWidth - flyoutWidth - 8;
|
| 39 |
+
if (topPosition + flyoutHeight > windowHeight) topPosition = windowHeight - flyoutHeight - 8;
|
| 40 |
+
|
| 41 |
+
flyoutElem.style.top = `${topPosition}px`;
|
| 42 |
+
flyoutElem.style.left = `${leftPosition}px`;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// This is the new main function called by Gradio's .then() event
|
| 47 |
+
function update_flyout_from_state(jsonData) {
|
| 48 |
+
//console.log("JS: update_flyout_from_state() called with data:", jsonData);
|
| 49 |
+
|
| 50 |
+
if (!jsonData) return;
|
| 51 |
+
|
| 52 |
+
const state = JSON.parse(jsonData);
|
| 53 |
+
const { isVisible, anchorId } = state;
|
| 54 |
+
const flyout = document.getElementById('injected_flyout_container');
|
| 55 |
+
|
| 56 |
+
if (!flyout) {
|
| 57 |
+
console.error("ERROR: Cannot update UI. Flyout container not found.");
|
| 58 |
+
return;
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
//console.log("JS: Parsed state:", { isVisible, anchorId });
|
|
|
|
| 62 |
|
| 63 |
+
if (isVisible) {
|
| 64 |
+
flyout.style.display = 'flex';
|
| 65 |
+
position_flyout(anchorId);
|
| 66 |
+
} else {
|
| 67 |
+
flyout.style.display = 'none';
|
| 68 |
+
}
|
| 69 |
+
}
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
gradio_propertysheet
|
| 2 |
-
|
|
|
|
| 1 |
gradio_propertysheet
|
| 2 |
+
gradio_htmlinjector
|
space.py
CHANGED
|
@@ -39,183 +39,117 @@ pip install gradio_propertysheet
|
|
| 39 |
|
| 40 |
```python
|
| 41 |
import os
|
| 42 |
-
|
| 43 |
import gradio as gr
|
| 44 |
from dataclasses import dataclass, field, asdict
|
| 45 |
from typing import Literal
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
-
from
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# --- 1. Dataclass Definitions ---
|
| 51 |
-
|
| 52 |
-
# Dataclasses for the Original Sidebar Demo
|
| 53 |
@dataclass
|
| 54 |
class ModelSettings:
|
| 55 |
-
\"\"\"Settings for loading models, VAEs, etc.\"\"\"
|
| 56 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 57 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 58 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 59 |
-
|
| 60 |
@dataclass
|
| 61 |
class SamplingSettings:
|
| 62 |
-
\"\"\"Settings for the image sampling process.\"\"\"
|
| 63 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 64 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 65 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
| 66 |
-
|
| 67 |
@dataclass
|
| 68 |
class RenderConfig:
|
| 69 |
-
\"\"\"Main configuration object for rendering, grouping all settings.\"\"\"
|
| 70 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 71 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 72 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 73 |
-
|
| 74 |
@dataclass
|
| 75 |
class Lighting:
|
| 76 |
-
\"\"\"Lighting settings for the environment.\"\"\"
|
| 77 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 78 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
| 79 |
-
|
| 80 |
@dataclass
|
| 81 |
class EnvironmentConfig:
|
| 82 |
-
\"\"\"Main configuration for the environment.\"\"\"
|
| 83 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 84 |
lighting: Lighting = field(default_factory=Lighting)
|
| 85 |
-
|
| 86 |
-
# Dataclasses for the Flyout Demo
|
| 87 |
@dataclass
|
| 88 |
class EulerSettings:
|
| 89 |
-
\"\"\"Settings specific to the Euler sampler.\"\"\"
|
| 90 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
| 91 |
-
|
| 92 |
@dataclass
|
| 93 |
class DPM_Settings:
|
| 94 |
-
\"\"\"Settings specific to DPM samplers.\"\"\"
|
| 95 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 96 |
|
| 97 |
-
# --- 2. Data Mappings and Initial Instances ---
|
| 98 |
-
|
| 99 |
-
# Data for Original Sidebar Demo
|
| 100 |
initial_render_config = RenderConfig()
|
| 101 |
initial_env_config = EnvironmentConfig()
|
| 102 |
-
|
| 103 |
-
# Data for Flyout Demo
|
| 104 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 105 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 106 |
|
| 107 |
-
# --- 3. CSS & JS Injection function ---
|
| 108 |
-
|
| 109 |
def inject_assets():
|
| 110 |
\"\"\"
|
| 111 |
-
This function prepares the payload of CSS
|
| 112 |
-
demo.load() event listener when the Gradio app starts.
|
| 113 |
\"\"\"
|
| 114 |
-
|
| 115 |
css_code = ""
|
| 116 |
js_code = ""
|
| 117 |
-
|
| 118 |
-
# Read from files
|
| 119 |
try:
|
| 120 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 121 |
css_code += f.read() + "\n"
|
| 122 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 123 |
js_code += f.read() + "\n"
|
| 124 |
except FileNotFoundError as e:
|
| 125 |
print(f"Warning: Could not read asset file: {e}")
|
| 126 |
-
|
| 127 |
-
return {"js": js_code, "css": css_code}
|
| 128 |
-
|
| 129 |
|
| 130 |
# --- 4. Gradio App Build ---
|
| 131 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 132 |
-
|
| 133 |
gr.Markdown("# PropertySheet Component Demos")
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
with gr.Tabs():
|
| 136 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 137 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
| 138 |
-
|
| 139 |
render_state = gr.State(value=initial_render_config)
|
| 140 |
env_state = gr.State(value=initial_env_config)
|
| 141 |
sidebar_visible = gr.State(False)
|
| 142 |
-
|
| 143 |
with gr.Row():
|
| 144 |
with gr.Column(scale=3):
|
| 145 |
generate = gr.Button("Show Settings", variant="primary")
|
| 146 |
with gr.Row():
|
| 147 |
output_render_json = gr.JSON(label="Live Render State")
|
| 148 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 149 |
-
|
| 150 |
with gr.Column(scale=1):
|
| 151 |
-
render_sheet = PropertySheet(
|
| 152 |
-
|
| 153 |
-
label="Render Settings",
|
| 154 |
-
width=400,
|
| 155 |
-
height=550,
|
| 156 |
-
visible=False,
|
| 157 |
-
root_label="Generator"
|
| 158 |
-
)
|
| 159 |
-
environment_sheet = PropertySheet(
|
| 160 |
-
value=initial_env_config,
|
| 161 |
-
label="Environment Settings",
|
| 162 |
-
width=400,
|
| 163 |
-
open=False,
|
| 164 |
-
visible=False,
|
| 165 |
-
root_label="General"
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 169 |
new_visibility = not is_visible
|
| 170 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 171 |
-
return (
|
| 172 |
-
new_visibility,
|
| 173 |
-
gr.update(visible=new_visibility, value=render_cfg),
|
| 174 |
-
gr.update(visible=new_visibility, value=env_cfg),
|
| 175 |
-
gr.update(value=button_text)
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 179 |
-
if updated_config is None:
|
| 180 |
-
|
| 181 |
-
if updated_config.model.model_type != "Custom":
|
| 182 |
-
updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
| 183 |
return updated_config, asdict(updated_config), updated_config
|
| 184 |
-
|
| 185 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 186 |
-
if updated_config is None:
|
| 187 |
-
return current_state, asdict(current_state), current_state
|
| 188 |
return updated_config, asdict(updated_config), current_state
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
outputs=[sidebar_visible, render_sheet, environment_sheet, generate]
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
render_sheet.change(
|
| 197 |
-
fn=handle_render_change,
|
| 198 |
-
inputs=[render_sheet, render_state],
|
| 199 |
-
outputs=[render_sheet, output_render_json, render_state]
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
environment_sheet.change(
|
| 203 |
-
fn=handle_env_change,
|
| 204 |
-
inputs=[environment_sheet, env_state],
|
| 205 |
-
outputs=[environment_sheet, output_env_json, env_state]
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
demo.load(
|
| 209 |
-
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 210 |
-
inputs=[render_state, env_state],
|
| 211 |
-
outputs=[output_render_json, output_env_json]
|
| 212 |
-
)
|
| 213 |
|
| 214 |
with gr.TabItem("Flyout Popup Demo"):
|
| 215 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 216 |
|
|
|
|
| 217 |
flyout_visible = gr.State(False)
|
| 218 |
active_anchor_id = gr.State(None)
|
|
|
|
| 219 |
|
| 220 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 221 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
@@ -226,54 +160,60 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 226 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 227 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 228 |
|
| 229 |
-
|
| 230 |
-
with gr.Row(elem_classes=["close-btn-row"]):
|
| 231 |
-
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 232 |
-
flyout_sheet = PropertySheet(visible=False, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 233 |
|
|
|
|
| 234 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 235 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 236 |
-
|
|
|
|
| 237 |
else:
|
| 238 |
-
|
|
|
|
| 239 |
|
| 240 |
def update_ear_visibility(selection, settings_map):
|
| 241 |
has_settings = settings_map.get(selection) is not None
|
| 242 |
return gr.update(visible=has_settings)
|
| 243 |
|
| 244 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 245 |
-
if updated_settings is None or active_id is None:
|
|
|
|
| 246 |
if active_id == sampler_dd.elem_id:
|
| 247 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 248 |
elif active_id == model_dd.elem_id:
|
| 249 |
model_settings_map_py[model_val] = updated_settings
|
| 250 |
|
| 251 |
def close_the_flyout():
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
sampler_dd.change(
|
| 255 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 256 |
inputs=[sampler_dd],
|
| 257 |
outputs=[sampler_ear_btn]
|
| 258 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 259 |
|
| 260 |
sampler_ear_btn.click(
|
| 261 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 262 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 263 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 264 |
-
).then(fn=None, inputs=
|
| 265 |
|
| 266 |
model_dd.change(
|
| 267 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 268 |
inputs=[model_dd],
|
| 269 |
outputs=[model_ear_btn]
|
| 270 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 271 |
|
| 272 |
model_ear_btn.click(
|
| 273 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 274 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 275 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 276 |
-
).then(fn=None, inputs=
|
| 277 |
|
| 278 |
flyout_sheet.change(
|
| 279 |
fn=on_flyout_change,
|
|
@@ -284,16 +224,24 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 284 |
close_btn.click(
|
| 285 |
fn=close_the_flyout,
|
| 286 |
inputs=None,
|
| 287 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 288 |
-
)
|
| 289 |
|
| 290 |
def initial_flyout_setup(sampler_val, model_val):
|
| 291 |
return {
|
| 292 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 293 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
if __name__ == "__main__":
|
| 299 |
demo.launch()
|
|
|
|
| 39 |
|
| 40 |
```python
|
| 41 |
import os
|
| 42 |
+
import json
|
| 43 |
import gradio as gr
|
| 44 |
from dataclasses import dataclass, field, asdict
|
| 45 |
from typing import Literal
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
+
from gradio_htmlinjector import HTMLInjector
|
| 48 |
|
| 49 |
+
# --- 1. Dataclass Definitions (unchanged) ---
|
|
|
|
|
|
|
|
|
|
| 50 |
@dataclass
|
| 51 |
class ModelSettings:
|
|
|
|
| 52 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 53 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 54 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
| 55 |
@dataclass
|
| 56 |
class SamplingSettings:
|
|
|
|
| 57 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 58 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 59 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
|
|
|
| 60 |
@dataclass
|
| 61 |
class RenderConfig:
|
|
|
|
| 62 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 63 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 64 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
| 65 |
@dataclass
|
| 66 |
class Lighting:
|
|
|
|
| 67 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 68 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
|
|
|
| 69 |
@dataclass
|
| 70 |
class EnvironmentConfig:
|
|
|
|
| 71 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 72 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class EulerSettings:
|
|
|
|
| 75 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
|
|
|
| 76 |
@dataclass
|
| 77 |
class DPM_Settings:
|
|
|
|
| 78 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 79 |
|
| 80 |
+
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
|
|
|
|
|
|
| 81 |
initial_render_config = RenderConfig()
|
| 82 |
initial_env_config = EnvironmentConfig()
|
|
|
|
|
|
|
| 83 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 84 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 85 |
|
| 86 |
+
# --- 3. CSS & JS Injection function (unchanged) ---
|
|
|
|
| 87 |
def inject_assets():
|
| 88 |
\"\"\"
|
| 89 |
+
This function prepares the payload of CSS, JS, and Body HTML for injection.
|
|
|
|
| 90 |
\"\"\"
|
| 91 |
+
popup_html = \"\"\"<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>\"\"\"
|
| 92 |
css_code = ""
|
| 93 |
js_code = ""
|
| 94 |
+
|
|
|
|
| 95 |
try:
|
| 96 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 97 |
css_code += f.read() + "\n"
|
| 98 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 99 |
js_code += f.read() + "\n"
|
| 100 |
except FileNotFoundError as e:
|
| 101 |
print(f"Warning: Could not read asset file: {e}")
|
| 102 |
+
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# --- 4. Gradio App Build ---
|
| 105 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 106 |
+
html_injector = HTMLInjector()
|
| 107 |
gr.Markdown("# PropertySheet Component Demos")
|
| 108 |
|
| 109 |
+
with gr.Row():
|
| 110 |
+
# --- Flyout popup ---
|
| 111 |
+
with gr.Column(elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]) as flyout_panel_source:
|
| 112 |
+
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 113 |
+
flyout_sheet = PropertySheet(visible=True, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 114 |
+
|
| 115 |
with gr.Tabs():
|
| 116 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 117 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
|
|
|
| 118 |
render_state = gr.State(value=initial_render_config)
|
| 119 |
env_state = gr.State(value=initial_env_config)
|
| 120 |
sidebar_visible = gr.State(False)
|
|
|
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column(scale=3):
|
| 123 |
generate = gr.Button("Show Settings", variant="primary")
|
| 124 |
with gr.Row():
|
| 125 |
output_render_json = gr.JSON(label="Live Render State")
|
| 126 |
output_env_json = gr.JSON(label="Live Environment State")
|
|
|
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
+
render_sheet = PropertySheet(value=initial_render_config, label="Render Settings", width=400, height=550, visible=False, root_label="Generator")
|
| 129 |
+
environment_sheet = PropertySheet(value=initial_env_config, label="Environment Settings", width=400, open=False, visible=False, root_label="General")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 131 |
new_visibility = not is_visible
|
| 132 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 133 |
+
return (new_visibility, gr.update(visible=new_visibility, value=render_cfg), gr.update(visible=new_visibility, value=env_cfg), gr.update(value=button_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 135 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
| 136 |
+
if updated_config.model.model_type != "Custom": updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
|
|
|
|
|
|
| 137 |
return updated_config, asdict(updated_config), updated_config
|
|
|
|
| 138 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 139 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
|
|
|
| 140 |
return updated_config, asdict(updated_config), current_state
|
| 141 |
+
generate.click(fn=change_visibility, inputs=[sidebar_visible, render_state, env_state], outputs=[sidebar_visible, render_sheet, environment_sheet, generate])
|
| 142 |
+
render_sheet.change(fn=handle_render_change, inputs=[render_sheet, render_state], outputs=[render_sheet, output_render_json, render_state])
|
| 143 |
+
environment_sheet.change(fn=handle_env_change, inputs=[environment_sheet, env_state], outputs=[environment_sheet, output_env_json, env_state])
|
| 144 |
+
demo.load(fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)), inputs=[render_state, env_state], outputs=[output_render_json, output_env_json])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
with gr.TabItem("Flyout Popup Demo"):
|
| 147 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 148 |
|
| 149 |
+
# --- State Management ---
|
| 150 |
flyout_visible = gr.State(False)
|
| 151 |
active_anchor_id = gr.State(None)
|
| 152 |
+
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 153 |
|
| 154 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 155 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
|
|
| 160 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 161 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 162 |
|
| 163 |
+
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
# --- Event Logic ---
|
| 166 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 167 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 168 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 169 |
+
return False, None, gr.update(), gr.update(value=js_data)
|
| 170 |
else:
|
| 171 |
+
js_data = json.dumps({"isVisible": True, "anchorId": clicked_dropdown_id})
|
| 172 |
+
return True, clicked_dropdown_id, gr.update(value=settings_obj), gr.update(value=js_data)
|
| 173 |
|
| 174 |
def update_ear_visibility(selection, settings_map):
|
| 175 |
has_settings = settings_map.get(selection) is not None
|
| 176 |
return gr.update(visible=has_settings)
|
| 177 |
|
| 178 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 179 |
+
if updated_settings is None or active_id is None:
|
| 180 |
+
return
|
| 181 |
if active_id == sampler_dd.elem_id:
|
| 182 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 183 |
elif active_id == model_dd.elem_id:
|
| 184 |
model_settings_map_py[model_val] = updated_settings
|
| 185 |
|
| 186 |
def close_the_flyout():
|
| 187 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 188 |
+
return False, None, gr.update(value=js_data)
|
| 189 |
+
|
| 190 |
+
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 191 |
|
| 192 |
sampler_dd.change(
|
| 193 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 194 |
inputs=[sampler_dd],
|
| 195 |
outputs=[sampler_ear_btn]
|
| 196 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 197 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 198 |
|
| 199 |
sampler_ear_btn.click(
|
| 200 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 201 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 202 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 203 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 204 |
|
| 205 |
model_dd.change(
|
| 206 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 207 |
inputs=[model_dd],
|
| 208 |
outputs=[model_ear_btn]
|
| 209 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 210 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 211 |
|
| 212 |
model_ear_btn.click(
|
| 213 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 214 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 215 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 216 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 217 |
|
| 218 |
flyout_sheet.change(
|
| 219 |
fn=on_flyout_change,
|
|
|
|
| 224 |
close_btn.click(
|
| 225 |
fn=close_the_flyout,
|
| 226 |
inputs=None,
|
| 227 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 228 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 229 |
|
| 230 |
def initial_flyout_setup(sampler_val, model_val):
|
| 231 |
return {
|
| 232 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 233 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
# --- App Load ---
|
| 237 |
+
demo.load(
|
| 238 |
+
fn=inject_assets, inputs=None, outputs=[html_injector]
|
| 239 |
+
).then(
|
| 240 |
+
fn=initial_flyout_setup, inputs=[sampler_dd, model_dd], outputs=[sampler_ear_btn, model_ear_btn]
|
| 241 |
+
).then(
|
| 242 |
+
fn=None, inputs=None, outputs=None,
|
| 243 |
+
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 244 |
+
)
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.launch()
|
src/README.md
CHANGED
|
@@ -36,9 +36,9 @@ The **PropertySheet** component for Gradio allows you to automatically generate
|
|
| 36 |
- **Theme-Aware**: Designed to look and feel native in all Gradio themes.
|
| 37 |
- **Dynamic Updates**: Supports advanced patterns where changing one field (e.g., a model selector) can dynamically update the options of another field (e.g., a sampler dropdown).
|
| 38 |
|
| 39 |
-
|
| 40 |
## Installation
|
| 41 |
|
|
|
|
| 42 |
```bash
|
| 43 |
pip install gradio_propertysheet
|
| 44 |
```
|
|
@@ -47,183 +47,117 @@ pip install gradio_propertysheet
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import os
|
| 50 |
-
|
| 51 |
import gradio as gr
|
| 52 |
from dataclasses import dataclass, field, asdict
|
| 53 |
from typing import Literal
|
| 54 |
from gradio_propertysheet import PropertySheet
|
| 55 |
-
from
|
| 56 |
-
|
| 57 |
|
| 58 |
-
# --- 1. Dataclass Definitions ---
|
| 59 |
-
|
| 60 |
-
# Dataclasses for the Original Sidebar Demo
|
| 61 |
@dataclass
|
| 62 |
class ModelSettings:
|
| 63 |
-
"""Settings for loading models, VAEs, etc."""
|
| 64 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 65 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 66 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 67 |
-
|
| 68 |
@dataclass
|
| 69 |
class SamplingSettings:
|
| 70 |
-
"""Settings for the image sampling process."""
|
| 71 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 72 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 73 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
| 74 |
-
|
| 75 |
@dataclass
|
| 76 |
class RenderConfig:
|
| 77 |
-
"""Main configuration object for rendering, grouping all settings."""
|
| 78 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 79 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 80 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 81 |
-
|
| 82 |
@dataclass
|
| 83 |
class Lighting:
|
| 84 |
-
"""Lighting settings for the environment."""
|
| 85 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 86 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
| 87 |
-
|
| 88 |
@dataclass
|
| 89 |
class EnvironmentConfig:
|
| 90 |
-
"""Main configuration for the environment."""
|
| 91 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 92 |
lighting: Lighting = field(default_factory=Lighting)
|
| 93 |
-
|
| 94 |
-
# Dataclasses for the Flyout Demo
|
| 95 |
@dataclass
|
| 96 |
class EulerSettings:
|
| 97 |
-
"""Settings specific to the Euler sampler."""
|
| 98 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
| 99 |
-
|
| 100 |
@dataclass
|
| 101 |
class DPM_Settings:
|
| 102 |
-
"""Settings specific to DPM samplers."""
|
| 103 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 104 |
|
| 105 |
-
# --- 2. Data Mappings and Initial Instances ---
|
| 106 |
-
|
| 107 |
-
# Data for Original Sidebar Demo
|
| 108 |
initial_render_config = RenderConfig()
|
| 109 |
initial_env_config = EnvironmentConfig()
|
| 110 |
-
|
| 111 |
-
# Data for Flyout Demo
|
| 112 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 113 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 114 |
|
| 115 |
-
# --- 3. CSS & JS Injection function ---
|
| 116 |
-
|
| 117 |
def inject_assets():
|
| 118 |
"""
|
| 119 |
-
This function prepares the payload of CSS
|
| 120 |
-
demo.load() event listener when the Gradio app starts.
|
| 121 |
"""
|
| 122 |
-
|
| 123 |
css_code = ""
|
| 124 |
js_code = ""
|
| 125 |
-
|
| 126 |
-
# Read from files
|
| 127 |
try:
|
| 128 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 129 |
css_code += f.read() + "\n"
|
| 130 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 131 |
js_code += f.read() + "\n"
|
| 132 |
except FileNotFoundError as e:
|
| 133 |
print(f"Warning: Could not read asset file: {e}")
|
| 134 |
-
|
| 135 |
-
return {"js": js_code, "css": css_code}
|
| 136 |
-
|
| 137 |
|
| 138 |
# --- 4. Gradio App Build ---
|
| 139 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 140 |
-
|
| 141 |
gr.Markdown("# PropertySheet Component Demos")
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
with gr.Tabs():
|
| 144 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 145 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
| 146 |
-
|
| 147 |
render_state = gr.State(value=initial_render_config)
|
| 148 |
env_state = gr.State(value=initial_env_config)
|
| 149 |
sidebar_visible = gr.State(False)
|
| 150 |
-
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=3):
|
| 153 |
generate = gr.Button("Show Settings", variant="primary")
|
| 154 |
with gr.Row():
|
| 155 |
output_render_json = gr.JSON(label="Live Render State")
|
| 156 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 157 |
-
|
| 158 |
with gr.Column(scale=1):
|
| 159 |
-
render_sheet = PropertySheet(
|
| 160 |
-
|
| 161 |
-
label="Render Settings",
|
| 162 |
-
width=400,
|
| 163 |
-
height=550,
|
| 164 |
-
visible=False,
|
| 165 |
-
root_label="Generator"
|
| 166 |
-
)
|
| 167 |
-
environment_sheet = PropertySheet(
|
| 168 |
-
value=initial_env_config,
|
| 169 |
-
label="Environment Settings",
|
| 170 |
-
width=400,
|
| 171 |
-
open=False,
|
| 172 |
-
visible=False,
|
| 173 |
-
root_label="General"
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 177 |
new_visibility = not is_visible
|
| 178 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 179 |
-
return (
|
| 180 |
-
new_visibility,
|
| 181 |
-
gr.update(visible=new_visibility, value=render_cfg),
|
| 182 |
-
gr.update(visible=new_visibility, value=env_cfg),
|
| 183 |
-
gr.update(value=button_text)
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 187 |
-
if updated_config is None:
|
| 188 |
-
|
| 189 |
-
if updated_config.model.model_type != "Custom":
|
| 190 |
-
updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
| 191 |
return updated_config, asdict(updated_config), updated_config
|
| 192 |
-
|
| 193 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 194 |
-
if updated_config is None:
|
| 195 |
-
return current_state, asdict(current_state), current_state
|
| 196 |
return updated_config, asdict(updated_config), current_state
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
outputs=[sidebar_visible, render_sheet, environment_sheet, generate]
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
render_sheet.change(
|
| 205 |
-
fn=handle_render_change,
|
| 206 |
-
inputs=[render_sheet, render_state],
|
| 207 |
-
outputs=[render_sheet, output_render_json, render_state]
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
environment_sheet.change(
|
| 211 |
-
fn=handle_env_change,
|
| 212 |
-
inputs=[environment_sheet, env_state],
|
| 213 |
-
outputs=[environment_sheet, output_env_json, env_state]
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
-
demo.load(
|
| 217 |
-
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 218 |
-
inputs=[render_state, env_state],
|
| 219 |
-
outputs=[output_render_json, output_env_json]
|
| 220 |
-
)
|
| 221 |
|
| 222 |
with gr.TabItem("Flyout Popup Demo"):
|
| 223 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 224 |
|
|
|
|
| 225 |
flyout_visible = gr.State(False)
|
| 226 |
active_anchor_id = gr.State(None)
|
|
|
|
| 227 |
|
| 228 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 229 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
@@ -234,54 +168,60 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 234 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 235 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 236 |
|
| 237 |
-
|
| 238 |
-
with gr.Row(elem_classes=["close-btn-row"]):
|
| 239 |
-
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 240 |
-
flyout_sheet = PropertySheet(visible=False, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 241 |
|
|
|
|
| 242 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 243 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 244 |
-
|
|
|
|
| 245 |
else:
|
| 246 |
-
|
|
|
|
| 247 |
|
| 248 |
def update_ear_visibility(selection, settings_map):
|
| 249 |
has_settings = settings_map.get(selection) is not None
|
| 250 |
return gr.update(visible=has_settings)
|
| 251 |
|
| 252 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 253 |
-
if updated_settings is None or active_id is None:
|
|
|
|
| 254 |
if active_id == sampler_dd.elem_id:
|
| 255 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 256 |
elif active_id == model_dd.elem_id:
|
| 257 |
model_settings_map_py[model_val] = updated_settings
|
| 258 |
|
| 259 |
def close_the_flyout():
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
sampler_dd.change(
|
| 263 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 264 |
inputs=[sampler_dd],
|
| 265 |
outputs=[sampler_ear_btn]
|
| 266 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 267 |
|
| 268 |
sampler_ear_btn.click(
|
| 269 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 270 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 271 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 272 |
-
).then(fn=None, inputs=
|
| 273 |
|
| 274 |
model_dd.change(
|
| 275 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 276 |
inputs=[model_dd],
|
| 277 |
outputs=[model_ear_btn]
|
| 278 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 279 |
|
| 280 |
model_ear_btn.click(
|
| 281 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 282 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 283 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 284 |
-
).then(fn=None, inputs=
|
| 285 |
|
| 286 |
flyout_sheet.change(
|
| 287 |
fn=on_flyout_change,
|
|
@@ -292,16 +232,24 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 292 |
close_btn.click(
|
| 293 |
fn=close_the_flyout,
|
| 294 |
inputs=None,
|
| 295 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 296 |
-
)
|
| 297 |
|
| 298 |
def initial_flyout_setup(sampler_val, model_val):
|
| 299 |
return {
|
| 300 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 301 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 302 |
-
}
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
if __name__ == "__main__":
|
| 307 |
demo.launch()
|
|
|
|
| 36 |
- **Theme-Aware**: Designed to look and feel native in all Gradio themes.
|
| 37 |
- **Dynamic Updates**: Supports advanced patterns where changing one field (e.g., a model selector) can dynamically update the options of another field (e.g., a sampler dropdown).
|
| 38 |
|
|
|
|
| 39 |
## Installation
|
| 40 |
|
| 41 |
+
|
| 42 |
```bash
|
| 43 |
pip install gradio_propertysheet
|
| 44 |
```
|
|
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import os
|
| 50 |
+
import json
|
| 51 |
import gradio as gr
|
| 52 |
from dataclasses import dataclass, field, asdict
|
| 53 |
from typing import Literal
|
| 54 |
from gradio_propertysheet import PropertySheet
|
| 55 |
+
from gradio_htmlinjector import HTMLInjector
|
|
|
|
| 56 |
|
| 57 |
+
# --- 1. Dataclass Definitions (unchanged) ---
|
|
|
|
|
|
|
| 58 |
@dataclass
|
| 59 |
class ModelSettings:
|
|
|
|
| 60 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 61 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 62 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
| 63 |
@dataclass
|
| 64 |
class SamplingSettings:
|
|
|
|
| 65 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 66 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 67 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
|
|
|
| 68 |
@dataclass
|
| 69 |
class RenderConfig:
|
|
|
|
| 70 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 71 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 72 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class Lighting:
|
|
|
|
| 75 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 76 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
|
|
|
| 77 |
@dataclass
|
| 78 |
class EnvironmentConfig:
|
|
|
|
| 79 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 80 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 81 |
@dataclass
|
| 82 |
class EulerSettings:
|
|
|
|
| 83 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
|
|
|
| 84 |
@dataclass
|
| 85 |
class DPM_Settings:
|
|
|
|
| 86 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 87 |
|
| 88 |
+
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
|
|
|
|
|
|
| 89 |
initial_render_config = RenderConfig()
|
| 90 |
initial_env_config = EnvironmentConfig()
|
|
|
|
|
|
|
| 91 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 92 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 93 |
|
| 94 |
+
# --- 3. CSS & JS Injection function (unchanged) ---
|
|
|
|
| 95 |
def inject_assets():
|
| 96 |
"""
|
| 97 |
+
This function prepares the payload of CSS, JS, and Body HTML for injection.
|
|
|
|
| 98 |
"""
|
| 99 |
+
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 100 |
css_code = ""
|
| 101 |
js_code = ""
|
| 102 |
+
|
|
|
|
| 103 |
try:
|
| 104 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 105 |
css_code += f.read() + "\n"
|
| 106 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 107 |
js_code += f.read() + "\n"
|
| 108 |
except FileNotFoundError as e:
|
| 109 |
print(f"Warning: Could not read asset file: {e}")
|
| 110 |
+
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# --- 4. Gradio App Build ---
|
| 113 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 114 |
+
html_injector = HTMLInjector()
|
| 115 |
gr.Markdown("# PropertySheet Component Demos")
|
| 116 |
|
| 117 |
+
with gr.Row():
|
| 118 |
+
# --- Flyout popup ---
|
| 119 |
+
with gr.Column(elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]) as flyout_panel_source:
|
| 120 |
+
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 121 |
+
flyout_sheet = PropertySheet(visible=True, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 122 |
+
|
| 123 |
with gr.Tabs():
|
| 124 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 125 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
|
|
|
| 126 |
render_state = gr.State(value=initial_render_config)
|
| 127 |
env_state = gr.State(value=initial_env_config)
|
| 128 |
sidebar_visible = gr.State(False)
|
|
|
|
| 129 |
with gr.Row():
|
| 130 |
with gr.Column(scale=3):
|
| 131 |
generate = gr.Button("Show Settings", variant="primary")
|
| 132 |
with gr.Row():
|
| 133 |
output_render_json = gr.JSON(label="Live Render State")
|
| 134 |
output_env_json = gr.JSON(label="Live Environment State")
|
|
|
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
+
render_sheet = PropertySheet(value=initial_render_config, label="Render Settings", width=400, height=550, visible=False, root_label="Generator")
|
| 137 |
+
environment_sheet = PropertySheet(value=initial_env_config, label="Environment Settings", width=400, open=False, visible=False, root_label="General")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 139 |
new_visibility = not is_visible
|
| 140 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 141 |
+
return (new_visibility, gr.update(visible=new_visibility, value=render_cfg), gr.update(visible=new_visibility, value=env_cfg), gr.update(value=button_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 143 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
| 144 |
+
if updated_config.model.model_type != "Custom": updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
|
|
|
|
|
|
| 145 |
return updated_config, asdict(updated_config), updated_config
|
|
|
|
| 146 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 147 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
|
|
|
| 148 |
return updated_config, asdict(updated_config), current_state
|
| 149 |
+
generate.click(fn=change_visibility, inputs=[sidebar_visible, render_state, env_state], outputs=[sidebar_visible, render_sheet, environment_sheet, generate])
|
| 150 |
+
render_sheet.change(fn=handle_render_change, inputs=[render_sheet, render_state], outputs=[render_sheet, output_render_json, render_state])
|
| 151 |
+
environment_sheet.change(fn=handle_env_change, inputs=[environment_sheet, env_state], outputs=[environment_sheet, output_env_json, env_state])
|
| 152 |
+
demo.load(fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)), inputs=[render_state, env_state], outputs=[output_render_json, output_env_json])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
with gr.TabItem("Flyout Popup Demo"):
|
| 155 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 156 |
|
| 157 |
+
# --- State Management ---
|
| 158 |
flyout_visible = gr.State(False)
|
| 159 |
active_anchor_id = gr.State(None)
|
| 160 |
+
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 161 |
|
| 162 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 163 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
|
|
| 168 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 169 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 170 |
|
| 171 |
+
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
# --- Event Logic ---
|
| 174 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 175 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 176 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 177 |
+
return False, None, gr.update(), gr.update(value=js_data)
|
| 178 |
else:
|
| 179 |
+
js_data = json.dumps({"isVisible": True, "anchorId": clicked_dropdown_id})
|
| 180 |
+
return True, clicked_dropdown_id, gr.update(value=settings_obj), gr.update(value=js_data)
|
| 181 |
|
| 182 |
def update_ear_visibility(selection, settings_map):
|
| 183 |
has_settings = settings_map.get(selection) is not None
|
| 184 |
return gr.update(visible=has_settings)
|
| 185 |
|
| 186 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 187 |
+
if updated_settings is None or active_id is None:
|
| 188 |
+
return
|
| 189 |
if active_id == sampler_dd.elem_id:
|
| 190 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 191 |
elif active_id == model_dd.elem_id:
|
| 192 |
model_settings_map_py[model_val] = updated_settings
|
| 193 |
|
| 194 |
def close_the_flyout():
|
| 195 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 196 |
+
return False, None, gr.update(value=js_data)
|
| 197 |
+
|
| 198 |
+
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 199 |
|
| 200 |
sampler_dd.change(
|
| 201 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 202 |
inputs=[sampler_dd],
|
| 203 |
outputs=[sampler_ear_btn]
|
| 204 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 205 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 206 |
|
| 207 |
sampler_ear_btn.click(
|
| 208 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 209 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 210 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 211 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 212 |
|
| 213 |
model_dd.change(
|
| 214 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 215 |
inputs=[model_dd],
|
| 216 |
outputs=[model_ear_btn]
|
| 217 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 218 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 219 |
|
| 220 |
model_ear_btn.click(
|
| 221 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 222 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 223 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 224 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 225 |
|
| 226 |
flyout_sheet.change(
|
| 227 |
fn=on_flyout_change,
|
|
|
|
| 232 |
close_btn.click(
|
| 233 |
fn=close_the_flyout,
|
| 234 |
inputs=None,
|
| 235 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 236 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 237 |
|
| 238 |
def initial_flyout_setup(sampler_val, model_val):
|
| 239 |
return {
|
| 240 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 241 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
# --- App Load ---
|
| 245 |
+
demo.load(
|
| 246 |
+
fn=inject_assets, inputs=None, outputs=[html_injector]
|
| 247 |
+
).then(
|
| 248 |
+
fn=initial_flyout_setup, inputs=[sampler_dd, model_dd], outputs=[sampler_ear_btn, model_ear_btn]
|
| 249 |
+
).then(
|
| 250 |
+
fn=None, inputs=None, outputs=None,
|
| 251 |
+
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 252 |
+
)
|
| 253 |
|
| 254 |
if __name__ == "__main__":
|
| 255 |
demo.launch()
|
src/custom.css
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
-
.flyout-context-area {
|
| 2 |
-
position: relative !important;
|
| 3 |
-
overflow: visible !important;
|
| 4 |
}
|
| 5 |
|
| 6 |
-
.flyout-sheet {
|
| 7 |
-
position:
|
| 8 |
flex-grow: 0 !important;
|
| 9 |
-
min-width: unset !important;
|
| 10 |
width: 350px !important;
|
| 11 |
align-self: center !important;
|
| 12 |
z-index: 1000;
|
|
@@ -20,7 +20,7 @@
|
|
| 20 |
.fake-input-container {
|
| 21 |
border: var(--input-border-width) solid var(--border-color-primary);
|
| 22 |
border-radius: var(--input-radius);
|
| 23 |
-
background: var(--
|
| 24 |
padding: 0 !important;
|
| 25 |
align-items: stretch !important;
|
| 26 |
box-shadow: var(--input-shadow);
|
|
@@ -77,4 +77,8 @@
|
|
| 77 |
|
| 78 |
.flyout-close-btn button:hover {
|
| 79 |
color: var(--body-text-color) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
|
|
|
| 1 |
+
.flyout-context-area {
|
| 2 |
+
position: relative !important;
|
| 3 |
+
overflow: visible !important;
|
| 4 |
}
|
| 5 |
|
| 6 |
+
.flyout-sheet {
|
| 7 |
+
position: fixed !important;
|
| 8 |
flex-grow: 0 !important;
|
| 9 |
+
min-width: unset !important;
|
| 10 |
width: 350px !important;
|
| 11 |
align-self: center !important;
|
| 12 |
z-index: 1000;
|
|
|
|
| 20 |
.fake-input-container {
|
| 21 |
border: var(--input-border-width) solid var(--border-color-primary);
|
| 22 |
border-radius: var(--input-radius);
|
| 23 |
+
background: var(--block-background-fill) !important;
|
| 24 |
padding: 0 !important;
|
| 25 |
align-items: stretch !important;
|
| 26 |
box-shadow: var(--input-shadow);
|
|
|
|
| 77 |
|
| 78 |
.flyout-close-btn button:hover {
|
| 79 |
color: var(--body-text-color) !important;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.flyout-source-hidden {
|
| 83 |
+
display: none !important;
|
| 84 |
}
|
src/custom.js
CHANGED
|
@@ -1,20 +1,69 @@
|
|
| 1 |
-
function
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
const leftPosition = (anchorRect.left - containerRect.left) + (anchorRect.width / 2) - (flyoutWidth / 2);
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
}
|
| 20 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
function reparent_flyout() {
|
| 2 |
+
//console.log("Attempting to reparent flyout...");
|
| 3 |
+
const source = document.getElementById('flyout_panel_source');
|
| 4 |
+
const target = document.getElementById('injected_flyout_container');
|
| 5 |
+
|
| 6 |
+
if (source && target) {
|
| 7 |
+
while (source.firstChild) {
|
| 8 |
+
target.appendChild(source.firstChild);
|
| 9 |
+
}
|
| 10 |
+
source.remove();
|
| 11 |
+
//console.log("SUCCESS: Flyout panel has been reparented.");
|
| 12 |
+
} else {
|
| 13 |
+
console.error("ERROR: Reparenting failed. Source or target element not found.");
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function position_flyout(anchorId) {
|
| 18 |
+
if (!anchorId) {
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const anchorElem = document.getElementById(anchorId);
|
| 23 |
+
const flyoutElem = document.getElementById('injected_flyout_container');
|
| 24 |
+
|
| 25 |
+
if (anchorElem && flyoutElem) {
|
| 26 |
+
//console.log("JS: Positioning flyout relative to:", anchorId);
|
| 27 |
+
const anchorRect = anchorElem.getBoundingClientRect();
|
| 28 |
+
const flyoutWidth = flyoutElem.offsetWidth;
|
| 29 |
+
const flyoutHeight = flyoutElem.offsetHeight;
|
| 30 |
+
|
| 31 |
+
let topPosition = anchorRect.top + (anchorRect.height / 2) - (flyoutHeight / 2);
|
| 32 |
+
let leftPosition = anchorRect.left + (anchorRect.width / 2) - (flyoutWidth / 2);
|
| 33 |
|
| 34 |
+
const windowWidth = window.innerWidth;
|
| 35 |
+
const windowHeight = window.innerHeight;
|
| 36 |
+
if (leftPosition < 8) leftPosition = 8;
|
| 37 |
+
if (topPosition < 8) topPosition = 8;
|
| 38 |
+
if (leftPosition + flyoutWidth > windowWidth) leftPosition = windowWidth - flyoutWidth - 8;
|
| 39 |
+
if (topPosition + flyoutHeight > windowHeight) topPosition = windowHeight - flyoutHeight - 8;
|
| 40 |
+
|
| 41 |
+
flyoutElem.style.top = `${topPosition}px`;
|
| 42 |
+
flyoutElem.style.left = `${leftPosition}px`;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// This is the new main function called by Gradio's .then() event
|
| 47 |
+
function update_flyout_from_state(jsonData) {
|
| 48 |
+
//console.log("JS: update_flyout_from_state() called with data:", jsonData);
|
| 49 |
+
|
| 50 |
+
if (!jsonData) return;
|
| 51 |
+
|
| 52 |
+
const state = JSON.parse(jsonData);
|
| 53 |
+
const { isVisible, anchorId } = state;
|
| 54 |
+
const flyout = document.getElementById('injected_flyout_container');
|
| 55 |
+
|
| 56 |
+
if (!flyout) {
|
| 57 |
+
console.error("ERROR: Cannot update UI. Flyout container not found.");
|
| 58 |
+
return;
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
//console.log("JS: Parsed state:", { isVisible, anchorId });
|
|
|
|
| 62 |
|
| 63 |
+
if (isVisible) {
|
| 64 |
+
flyout.style.display = 'flex';
|
| 65 |
+
position_flyout(anchorId);
|
| 66 |
+
} else {
|
| 67 |
+
flyout.style.display = 'none';
|
| 68 |
+
}
|
| 69 |
+
}
|
src/demo/app.py
CHANGED
|
@@ -1,181 +1,115 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
from dataclasses import dataclass, field, asdict
|
| 5 |
from typing import Literal
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
-
from
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# --- 1. Dataclass Definitions ---
|
| 11 |
-
|
| 12 |
-
# Dataclasses for the Original Sidebar Demo
|
| 13 |
@dataclass
|
| 14 |
class ModelSettings:
|
| 15 |
-
"""Settings for loading models, VAEs, etc."""
|
| 16 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 17 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 18 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 19 |
-
|
| 20 |
@dataclass
|
| 21 |
class SamplingSettings:
|
| 22 |
-
"""Settings for the image sampling process."""
|
| 23 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 24 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 25 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
| 26 |
-
|
| 27 |
@dataclass
|
| 28 |
class RenderConfig:
|
| 29 |
-
"""Main configuration object for rendering, grouping all settings."""
|
| 30 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 31 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 32 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 33 |
-
|
| 34 |
@dataclass
|
| 35 |
class Lighting:
|
| 36 |
-
"""Lighting settings for the environment."""
|
| 37 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 38 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
| 39 |
-
|
| 40 |
@dataclass
|
| 41 |
class EnvironmentConfig:
|
| 42 |
-
"""Main configuration for the environment."""
|
| 43 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 44 |
lighting: Lighting = field(default_factory=Lighting)
|
| 45 |
-
|
| 46 |
-
# Dataclasses for the Flyout Demo
|
| 47 |
@dataclass
|
| 48 |
class EulerSettings:
|
| 49 |
-
"""Settings specific to the Euler sampler."""
|
| 50 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
| 51 |
-
|
| 52 |
@dataclass
|
| 53 |
class DPM_Settings:
|
| 54 |
-
"""Settings specific to DPM samplers."""
|
| 55 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 56 |
|
| 57 |
-
# --- 2. Data Mappings and Initial Instances ---
|
| 58 |
-
|
| 59 |
-
# Data for Original Sidebar Demo
|
| 60 |
initial_render_config = RenderConfig()
|
| 61 |
initial_env_config = EnvironmentConfig()
|
| 62 |
-
|
| 63 |
-
# Data for Flyout Demo
|
| 64 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 65 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 66 |
|
| 67 |
-
# --- 3. CSS & JS Injection function ---
|
| 68 |
-
|
| 69 |
def inject_assets():
|
| 70 |
"""
|
| 71 |
-
This function prepares the payload of CSS
|
| 72 |
-
demo.load() event listener when the Gradio app starts.
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
css_code = ""
|
| 76 |
js_code = ""
|
| 77 |
-
|
| 78 |
-
# Read from files
|
| 79 |
try:
|
| 80 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 81 |
css_code += f.read() + "\n"
|
| 82 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 83 |
js_code += f.read() + "\n"
|
| 84 |
except FileNotFoundError as e:
|
| 85 |
print(f"Warning: Could not read asset file: {e}")
|
| 86 |
-
|
| 87 |
-
return {"js": js_code, "css": css_code}
|
| 88 |
-
|
| 89 |
|
| 90 |
# --- 4. Gradio App Build ---
|
| 91 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 92 |
-
|
| 93 |
gr.Markdown("# PropertySheet Component Demos")
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
with gr.Tabs():
|
| 96 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 97 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
| 98 |
-
|
| 99 |
render_state = gr.State(value=initial_render_config)
|
| 100 |
env_state = gr.State(value=initial_env_config)
|
| 101 |
sidebar_visible = gr.State(False)
|
| 102 |
-
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column(scale=3):
|
| 105 |
generate = gr.Button("Show Settings", variant="primary")
|
| 106 |
with gr.Row():
|
| 107 |
output_render_json = gr.JSON(label="Live Render State")
|
| 108 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 109 |
-
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
-
render_sheet = PropertySheet(
|
| 112 |
-
|
| 113 |
-
label="Render Settings",
|
| 114 |
-
width=400,
|
| 115 |
-
height=550,
|
| 116 |
-
visible=False,
|
| 117 |
-
root_label="Generator"
|
| 118 |
-
)
|
| 119 |
-
environment_sheet = PropertySheet(
|
| 120 |
-
value=initial_env_config,
|
| 121 |
-
label="Environment Settings",
|
| 122 |
-
width=400,
|
| 123 |
-
open=False,
|
| 124 |
-
visible=False,
|
| 125 |
-
root_label="General"
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 129 |
new_visibility = not is_visible
|
| 130 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 131 |
-
return (
|
| 132 |
-
new_visibility,
|
| 133 |
-
gr.update(visible=new_visibility, value=render_cfg),
|
| 134 |
-
gr.update(visible=new_visibility, value=env_cfg),
|
| 135 |
-
gr.update(value=button_text)
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 139 |
-
if updated_config is None:
|
| 140 |
-
|
| 141 |
-
if updated_config.model.model_type != "Custom":
|
| 142 |
-
updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
| 143 |
return updated_config, asdict(updated_config), updated_config
|
| 144 |
-
|
| 145 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 146 |
-
if updated_config is None:
|
| 147 |
-
return current_state, asdict(current_state), current_state
|
| 148 |
return updated_config, asdict(updated_config), current_state
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
outputs=[sidebar_visible, render_sheet, environment_sheet, generate]
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
render_sheet.change(
|
| 157 |
-
fn=handle_render_change,
|
| 158 |
-
inputs=[render_sheet, render_state],
|
| 159 |
-
outputs=[render_sheet, output_render_json, render_state]
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
environment_sheet.change(
|
| 163 |
-
fn=handle_env_change,
|
| 164 |
-
inputs=[environment_sheet, env_state],
|
| 165 |
-
outputs=[environment_sheet, output_env_json, env_state]
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
demo.load(
|
| 169 |
-
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 170 |
-
inputs=[render_state, env_state],
|
| 171 |
-
outputs=[output_render_json, output_env_json]
|
| 172 |
-
)
|
| 173 |
|
| 174 |
with gr.TabItem("Flyout Popup Demo"):
|
| 175 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 176 |
|
|
|
|
| 177 |
flyout_visible = gr.State(False)
|
| 178 |
active_anchor_id = gr.State(None)
|
|
|
|
| 179 |
|
| 180 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 181 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
@@ -186,54 +120,60 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 186 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 187 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 188 |
|
| 189 |
-
|
| 190 |
-
with gr.Row(elem_classes=["close-btn-row"]):
|
| 191 |
-
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 192 |
-
flyout_sheet = PropertySheet(visible=False, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 193 |
|
|
|
|
| 194 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 195 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 196 |
-
|
|
|
|
| 197 |
else:
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
def update_ear_visibility(selection, settings_map):
|
| 201 |
has_settings = settings_map.get(selection) is not None
|
| 202 |
return gr.update(visible=has_settings)
|
| 203 |
|
| 204 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 205 |
-
if updated_settings is None or active_id is None:
|
|
|
|
| 206 |
if active_id == sampler_dd.elem_id:
|
| 207 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 208 |
elif active_id == model_dd.elem_id:
|
| 209 |
model_settings_map_py[model_val] = updated_settings
|
| 210 |
|
| 211 |
def close_the_flyout():
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
sampler_dd.change(
|
| 215 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 216 |
inputs=[sampler_dd],
|
| 217 |
outputs=[sampler_ear_btn]
|
| 218 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 219 |
|
| 220 |
sampler_ear_btn.click(
|
| 221 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 222 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 223 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 224 |
-
).then(fn=None, inputs=
|
| 225 |
|
| 226 |
model_dd.change(
|
| 227 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 228 |
inputs=[model_dd],
|
| 229 |
outputs=[model_ear_btn]
|
| 230 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 231 |
|
| 232 |
model_ear_btn.click(
|
| 233 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 234 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 235 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 236 |
-
).then(fn=None, inputs=
|
| 237 |
|
| 238 |
flyout_sheet.change(
|
| 239 |
fn=on_flyout_change,
|
|
@@ -244,16 +184,24 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 244 |
close_btn.click(
|
| 245 |
fn=close_the_flyout,
|
| 246 |
inputs=None,
|
| 247 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 248 |
-
)
|
| 249 |
|
| 250 |
def initial_flyout_setup(sampler_val, model_val):
|
| 251 |
return {
|
| 252 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 253 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 254 |
-
}
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
| 259 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import gradio as gr
|
| 4 |
from dataclasses import dataclass, field, asdict
|
| 5 |
from typing import Literal
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
+
from gradio_htmlinjector import HTMLInjector
|
| 8 |
|
| 9 |
+
# --- 1. Dataclass Definitions (unchanged) ---
|
|
|
|
|
|
|
|
|
|
| 10 |
@dataclass
|
| 11 |
class ModelSettings:
|
|
|
|
| 12 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 13 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 14 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
| 15 |
@dataclass
|
| 16 |
class SamplingSettings:
|
|
|
|
| 17 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 18 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 19 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
|
|
|
| 20 |
@dataclass
|
| 21 |
class RenderConfig:
|
|
|
|
| 22 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 23 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 24 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
| 25 |
@dataclass
|
| 26 |
class Lighting:
|
|
|
|
| 27 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 28 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
|
|
|
| 29 |
@dataclass
|
| 30 |
class EnvironmentConfig:
|
|
|
|
| 31 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 32 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 33 |
@dataclass
|
| 34 |
class EulerSettings:
|
|
|
|
| 35 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
|
|
|
| 36 |
@dataclass
|
| 37 |
class DPM_Settings:
|
|
|
|
| 38 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 39 |
|
| 40 |
+
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
|
|
|
|
|
|
| 41 |
initial_render_config = RenderConfig()
|
| 42 |
initial_env_config = EnvironmentConfig()
|
|
|
|
|
|
|
| 43 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 44 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 45 |
|
| 46 |
+
# --- 3. CSS & JS Injection function (unchanged) ---
|
|
|
|
| 47 |
def inject_assets():
|
| 48 |
"""
|
| 49 |
+
This function prepares the payload of CSS, JS, and Body HTML for injection.
|
|
|
|
| 50 |
"""
|
| 51 |
+
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 52 |
css_code = ""
|
| 53 |
js_code = ""
|
| 54 |
+
|
|
|
|
| 55 |
try:
|
| 56 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 57 |
css_code += f.read() + "\n"
|
| 58 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 59 |
js_code += f.read() + "\n"
|
| 60 |
except FileNotFoundError as e:
|
| 61 |
print(f"Warning: Could not read asset file: {e}")
|
| 62 |
+
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# --- 4. Gradio App Build ---
|
| 65 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 66 |
+
html_injector = HTMLInjector()
|
| 67 |
gr.Markdown("# PropertySheet Component Demos")
|
| 68 |
|
| 69 |
+
with gr.Row():
|
| 70 |
+
# --- Flyout popup ---
|
| 71 |
+
with gr.Column(elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]) as flyout_panel_source:
|
| 72 |
+
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 73 |
+
flyout_sheet = PropertySheet(visible=True, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 74 |
+
|
| 75 |
with gr.Tabs():
|
| 76 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 77 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
|
|
|
| 78 |
render_state = gr.State(value=initial_render_config)
|
| 79 |
env_state = gr.State(value=initial_env_config)
|
| 80 |
sidebar_visible = gr.State(False)
|
|
|
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column(scale=3):
|
| 83 |
generate = gr.Button("Show Settings", variant="primary")
|
| 84 |
with gr.Row():
|
| 85 |
output_render_json = gr.JSON(label="Live Render State")
|
| 86 |
output_env_json = gr.JSON(label="Live Environment State")
|
|
|
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
+
render_sheet = PropertySheet(value=initial_render_config, label="Render Settings", width=400, height=550, visible=False, root_label="Generator")
|
| 89 |
+
environment_sheet = PropertySheet(value=initial_env_config, label="Environment Settings", width=400, open=False, visible=False, root_label="General")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 91 |
new_visibility = not is_visible
|
| 92 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 93 |
+
return (new_visibility, gr.update(visible=new_visibility, value=render_cfg), gr.update(visible=new_visibility, value=env_cfg), gr.update(value=button_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 95 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
| 96 |
+
if updated_config.model.model_type != "Custom": updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
|
|
|
|
|
|
| 97 |
return updated_config, asdict(updated_config), updated_config
|
|
|
|
| 98 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 99 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
|
|
|
| 100 |
return updated_config, asdict(updated_config), current_state
|
| 101 |
+
generate.click(fn=change_visibility, inputs=[sidebar_visible, render_state, env_state], outputs=[sidebar_visible, render_sheet, environment_sheet, generate])
|
| 102 |
+
render_sheet.change(fn=handle_render_change, inputs=[render_sheet, render_state], outputs=[render_sheet, output_render_json, render_state])
|
| 103 |
+
environment_sheet.change(fn=handle_env_change, inputs=[environment_sheet, env_state], outputs=[environment_sheet, output_env_json, env_state])
|
| 104 |
+
demo.load(fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)), inputs=[render_state, env_state], outputs=[output_render_json, output_env_json])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
with gr.TabItem("Flyout Popup Demo"):
|
| 107 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 108 |
|
| 109 |
+
# --- State Management ---
|
| 110 |
flyout_visible = gr.State(False)
|
| 111 |
active_anchor_id = gr.State(None)
|
| 112 |
+
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 113 |
|
| 114 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 115 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
|
|
| 120 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 121 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 122 |
|
| 123 |
+
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# --- Event Logic ---
|
| 126 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 127 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 128 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 129 |
+
return False, None, gr.update(), gr.update(value=js_data)
|
| 130 |
else:
|
| 131 |
+
js_data = json.dumps({"isVisible": True, "anchorId": clicked_dropdown_id})
|
| 132 |
+
return True, clicked_dropdown_id, gr.update(value=settings_obj), gr.update(value=js_data)
|
| 133 |
|
| 134 |
def update_ear_visibility(selection, settings_map):
|
| 135 |
has_settings = settings_map.get(selection) is not None
|
| 136 |
return gr.update(visible=has_settings)
|
| 137 |
|
| 138 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 139 |
+
if updated_settings is None or active_id is None:
|
| 140 |
+
return
|
| 141 |
if active_id == sampler_dd.elem_id:
|
| 142 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 143 |
elif active_id == model_dd.elem_id:
|
| 144 |
model_settings_map_py[model_val] = updated_settings
|
| 145 |
|
| 146 |
def close_the_flyout():
|
| 147 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 148 |
+
return False, None, gr.update(value=js_data)
|
| 149 |
+
|
| 150 |
+
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 151 |
|
| 152 |
sampler_dd.change(
|
| 153 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 154 |
inputs=[sampler_dd],
|
| 155 |
outputs=[sampler_ear_btn]
|
| 156 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 157 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 158 |
|
| 159 |
sampler_ear_btn.click(
|
| 160 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 161 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 162 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 163 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 164 |
|
| 165 |
model_dd.change(
|
| 166 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 167 |
inputs=[model_dd],
|
| 168 |
outputs=[model_ear_btn]
|
| 169 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 170 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 171 |
|
| 172 |
model_ear_btn.click(
|
| 173 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 174 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 175 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 176 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 177 |
|
| 178 |
flyout_sheet.change(
|
| 179 |
fn=on_flyout_change,
|
|
|
|
| 184 |
close_btn.click(
|
| 185 |
fn=close_the_flyout,
|
| 186 |
inputs=None,
|
| 187 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 188 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 189 |
|
| 190 |
def initial_flyout_setup(sampler_val, model_val):
|
| 191 |
return {
|
| 192 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 193 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
# --- App Load ---
|
| 197 |
+
demo.load(
|
| 198 |
+
fn=inject_assets, inputs=None, outputs=[html_injector]
|
| 199 |
+
).then(
|
| 200 |
+
fn=initial_flyout_setup, inputs=[sampler_dd, model_dd], outputs=[sampler_ear_btn, model_ear_btn]
|
| 201 |
+
).then(
|
| 202 |
+
fn=None, inputs=None, outputs=None,
|
| 203 |
+
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 204 |
+
)
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch()
|
src/demo/custom.css
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
-
.flyout-context-area {
|
| 2 |
-
position: relative !important;
|
| 3 |
-
overflow: visible !important;
|
| 4 |
}
|
| 5 |
|
| 6 |
-
.flyout-sheet {
|
| 7 |
-
position:
|
| 8 |
flex-grow: 0 !important;
|
| 9 |
-
min-width: unset !important;
|
| 10 |
width: 350px !important;
|
| 11 |
align-self: center !important;
|
| 12 |
z-index: 1000;
|
|
@@ -20,7 +20,7 @@
|
|
| 20 |
.fake-input-container {
|
| 21 |
border: var(--input-border-width) solid var(--border-color-primary);
|
| 22 |
border-radius: var(--input-radius);
|
| 23 |
-
background: var(--
|
| 24 |
padding: 0 !important;
|
| 25 |
align-items: stretch !important;
|
| 26 |
box-shadow: var(--input-shadow);
|
|
@@ -77,4 +77,8 @@
|
|
| 77 |
|
| 78 |
.flyout-close-btn button:hover {
|
| 79 |
color: var(--body-text-color) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
|
|
|
| 1 |
+
.flyout-context-area {
|
| 2 |
+
position: relative !important;
|
| 3 |
+
overflow: visible !important;
|
| 4 |
}
|
| 5 |
|
| 6 |
+
.flyout-sheet {
|
| 7 |
+
position: fixed !important;
|
| 8 |
flex-grow: 0 !important;
|
| 9 |
+
min-width: unset !important;
|
| 10 |
width: 350px !important;
|
| 11 |
align-self: center !important;
|
| 12 |
z-index: 1000;
|
|
|
|
| 20 |
.fake-input-container {
|
| 21 |
border: var(--input-border-width) solid var(--border-color-primary);
|
| 22 |
border-radius: var(--input-radius);
|
| 23 |
+
background: var(--block-background-fill) !important;
|
| 24 |
padding: 0 !important;
|
| 25 |
align-items: stretch !important;
|
| 26 |
box-shadow: var(--input-shadow);
|
|
|
|
| 77 |
|
| 78 |
.flyout-close-btn button:hover {
|
| 79 |
color: var(--body-text-color) !important;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.flyout-source-hidden {
|
| 83 |
+
display: none !important;
|
| 84 |
}
|
src/demo/custom.js
CHANGED
|
@@ -1,20 +1,69 @@
|
|
| 1 |
-
function
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
const leftPosition = (anchorRect.left - containerRect.left) + (anchorRect.width / 2) - (flyoutWidth / 2);
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
}
|
| 20 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
function reparent_flyout() {
|
| 2 |
+
//console.log("Attempting to reparent flyout...");
|
| 3 |
+
const source = document.getElementById('flyout_panel_source');
|
| 4 |
+
const target = document.getElementById('injected_flyout_container');
|
| 5 |
+
|
| 6 |
+
if (source && target) {
|
| 7 |
+
while (source.firstChild) {
|
| 8 |
+
target.appendChild(source.firstChild);
|
| 9 |
+
}
|
| 10 |
+
source.remove();
|
| 11 |
+
//console.log("SUCCESS: Flyout panel has been reparented.");
|
| 12 |
+
} else {
|
| 13 |
+
console.error("ERROR: Reparenting failed. Source or target element not found.");
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function position_flyout(anchorId) {
|
| 18 |
+
if (!anchorId) {
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const anchorElem = document.getElementById(anchorId);
|
| 23 |
+
const flyoutElem = document.getElementById('injected_flyout_container');
|
| 24 |
+
|
| 25 |
+
if (anchorElem && flyoutElem) {
|
| 26 |
+
//console.log("JS: Positioning flyout relative to:", anchorId);
|
| 27 |
+
const anchorRect = anchorElem.getBoundingClientRect();
|
| 28 |
+
const flyoutWidth = flyoutElem.offsetWidth;
|
| 29 |
+
const flyoutHeight = flyoutElem.offsetHeight;
|
| 30 |
+
|
| 31 |
+
let topPosition = anchorRect.top + (anchorRect.height / 2) - (flyoutHeight / 2);
|
| 32 |
+
let leftPosition = anchorRect.left + (anchorRect.width / 2) - (flyoutWidth / 2);
|
| 33 |
|
| 34 |
+
const windowWidth = window.innerWidth;
|
| 35 |
+
const windowHeight = window.innerHeight;
|
| 36 |
+
if (leftPosition < 8) leftPosition = 8;
|
| 37 |
+
if (topPosition < 8) topPosition = 8;
|
| 38 |
+
if (leftPosition + flyoutWidth > windowWidth) leftPosition = windowWidth - flyoutWidth - 8;
|
| 39 |
+
if (topPosition + flyoutHeight > windowHeight) topPosition = windowHeight - flyoutHeight - 8;
|
| 40 |
+
|
| 41 |
+
flyoutElem.style.top = `${topPosition}px`;
|
| 42 |
+
flyoutElem.style.left = `${leftPosition}px`;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// This is the new main function called by Gradio's .then() event
|
| 47 |
+
function update_flyout_from_state(jsonData) {
|
| 48 |
+
//console.log("JS: update_flyout_from_state() called with data:", jsonData);
|
| 49 |
+
|
| 50 |
+
if (!jsonData) return;
|
| 51 |
+
|
| 52 |
+
const state = JSON.parse(jsonData);
|
| 53 |
+
const { isVisible, anchorId } = state;
|
| 54 |
+
const flyout = document.getElementById('injected_flyout_container');
|
| 55 |
+
|
| 56 |
+
if (!flyout) {
|
| 57 |
+
console.error("ERROR: Cannot update UI. Flyout container not found.");
|
| 58 |
+
return;
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
//console.log("JS: Parsed state:", { isVisible, anchorId });
|
|
|
|
| 62 |
|
| 63 |
+
if (isVisible) {
|
| 64 |
+
flyout.style.display = 'flex';
|
| 65 |
+
position_flyout(anchorId);
|
| 66 |
+
} else {
|
| 67 |
+
flyout.style.display = 'none';
|
| 68 |
+
}
|
| 69 |
+
}
|
src/demo/requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
gradio_propertysheet
|
| 2 |
-
|
|
|
|
| 1 |
gradio_propertysheet
|
| 2 |
+
gradio_htmlinjector
|
src/demo/space.py
CHANGED
|
@@ -39,183 +39,117 @@ pip install gradio_propertysheet
|
|
| 39 |
|
| 40 |
```python
|
| 41 |
import os
|
| 42 |
-
|
| 43 |
import gradio as gr
|
| 44 |
from dataclasses import dataclass, field, asdict
|
| 45 |
from typing import Literal
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
-
from
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# --- 1. Dataclass Definitions ---
|
| 51 |
-
|
| 52 |
-
# Dataclasses for the Original Sidebar Demo
|
| 53 |
@dataclass
|
| 54 |
class ModelSettings:
|
| 55 |
-
\"\"\"Settings for loading models, VAEs, etc.\"\"\"
|
| 56 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 57 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 58 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 59 |
-
|
| 60 |
@dataclass
|
| 61 |
class SamplingSettings:
|
| 62 |
-
\"\"\"Settings for the image sampling process.\"\"\"
|
| 63 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 64 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 65 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
| 66 |
-
|
| 67 |
@dataclass
|
| 68 |
class RenderConfig:
|
| 69 |
-
\"\"\"Main configuration object for rendering, grouping all settings.\"\"\"
|
| 70 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 71 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 72 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 73 |
-
|
| 74 |
@dataclass
|
| 75 |
class Lighting:
|
| 76 |
-
\"\"\"Lighting settings for the environment.\"\"\"
|
| 77 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 78 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
| 79 |
-
|
| 80 |
@dataclass
|
| 81 |
class EnvironmentConfig:
|
| 82 |
-
\"\"\"Main configuration for the environment.\"\"\"
|
| 83 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 84 |
lighting: Lighting = field(default_factory=Lighting)
|
| 85 |
-
|
| 86 |
-
# Dataclasses for the Flyout Demo
|
| 87 |
@dataclass
|
| 88 |
class EulerSettings:
|
| 89 |
-
\"\"\"Settings specific to the Euler sampler.\"\"\"
|
| 90 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
| 91 |
-
|
| 92 |
@dataclass
|
| 93 |
class DPM_Settings:
|
| 94 |
-
\"\"\"Settings specific to DPM samplers.\"\"\"
|
| 95 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 96 |
|
| 97 |
-
# --- 2. Data Mappings and Initial Instances ---
|
| 98 |
-
|
| 99 |
-
# Data for Original Sidebar Demo
|
| 100 |
initial_render_config = RenderConfig()
|
| 101 |
initial_env_config = EnvironmentConfig()
|
| 102 |
-
|
| 103 |
-
# Data for Flyout Demo
|
| 104 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 105 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 106 |
|
| 107 |
-
# --- 3. CSS & JS Injection function ---
|
| 108 |
-
|
| 109 |
def inject_assets():
|
| 110 |
\"\"\"
|
| 111 |
-
This function prepares the payload of CSS
|
| 112 |
-
demo.load() event listener when the Gradio app starts.
|
| 113 |
\"\"\"
|
| 114 |
-
|
| 115 |
css_code = ""
|
| 116 |
js_code = ""
|
| 117 |
-
|
| 118 |
-
# Read from files
|
| 119 |
try:
|
| 120 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 121 |
css_code += f.read() + "\n"
|
| 122 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 123 |
js_code += f.read() + "\n"
|
| 124 |
except FileNotFoundError as e:
|
| 125 |
print(f"Warning: Could not read asset file: {e}")
|
| 126 |
-
|
| 127 |
-
return {"js": js_code, "css": css_code}
|
| 128 |
-
|
| 129 |
|
| 130 |
# --- 4. Gradio App Build ---
|
| 131 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 132 |
-
|
| 133 |
gr.Markdown("# PropertySheet Component Demos")
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
with gr.Tabs():
|
| 136 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 137 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
| 138 |
-
|
| 139 |
render_state = gr.State(value=initial_render_config)
|
| 140 |
env_state = gr.State(value=initial_env_config)
|
| 141 |
sidebar_visible = gr.State(False)
|
| 142 |
-
|
| 143 |
with gr.Row():
|
| 144 |
with gr.Column(scale=3):
|
| 145 |
generate = gr.Button("Show Settings", variant="primary")
|
| 146 |
with gr.Row():
|
| 147 |
output_render_json = gr.JSON(label="Live Render State")
|
| 148 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 149 |
-
|
| 150 |
with gr.Column(scale=1):
|
| 151 |
-
render_sheet = PropertySheet(
|
| 152 |
-
|
| 153 |
-
label="Render Settings",
|
| 154 |
-
width=400,
|
| 155 |
-
height=550,
|
| 156 |
-
visible=False,
|
| 157 |
-
root_label="Generator"
|
| 158 |
-
)
|
| 159 |
-
environment_sheet = PropertySheet(
|
| 160 |
-
value=initial_env_config,
|
| 161 |
-
label="Environment Settings",
|
| 162 |
-
width=400,
|
| 163 |
-
open=False,
|
| 164 |
-
visible=False,
|
| 165 |
-
root_label="General"
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 169 |
new_visibility = not is_visible
|
| 170 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 171 |
-
return (
|
| 172 |
-
new_visibility,
|
| 173 |
-
gr.update(visible=new_visibility, value=render_cfg),
|
| 174 |
-
gr.update(visible=new_visibility, value=env_cfg),
|
| 175 |
-
gr.update(value=button_text)
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 179 |
-
if updated_config is None:
|
| 180 |
-
|
| 181 |
-
if updated_config.model.model_type != "Custom":
|
| 182 |
-
updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
| 183 |
return updated_config, asdict(updated_config), updated_config
|
| 184 |
-
|
| 185 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 186 |
-
if updated_config is None:
|
| 187 |
-
return current_state, asdict(current_state), current_state
|
| 188 |
return updated_config, asdict(updated_config), current_state
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
outputs=[sidebar_visible, render_sheet, environment_sheet, generate]
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
render_sheet.change(
|
| 197 |
-
fn=handle_render_change,
|
| 198 |
-
inputs=[render_sheet, render_state],
|
| 199 |
-
outputs=[render_sheet, output_render_json, render_state]
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
environment_sheet.change(
|
| 203 |
-
fn=handle_env_change,
|
| 204 |
-
inputs=[environment_sheet, env_state],
|
| 205 |
-
outputs=[environment_sheet, output_env_json, env_state]
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
demo.load(
|
| 209 |
-
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 210 |
-
inputs=[render_state, env_state],
|
| 211 |
-
outputs=[output_render_json, output_env_json]
|
| 212 |
-
)
|
| 213 |
|
| 214 |
with gr.TabItem("Flyout Popup Demo"):
|
| 215 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 216 |
|
|
|
|
| 217 |
flyout_visible = gr.State(False)
|
| 218 |
active_anchor_id = gr.State(None)
|
|
|
|
| 219 |
|
| 220 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 221 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
@@ -226,54 +160,60 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 226 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 227 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 228 |
|
| 229 |
-
|
| 230 |
-
with gr.Row(elem_classes=["close-btn-row"]):
|
| 231 |
-
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 232 |
-
flyout_sheet = PropertySheet(visible=False, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 233 |
|
|
|
|
| 234 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 235 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 236 |
-
|
|
|
|
| 237 |
else:
|
| 238 |
-
|
|
|
|
| 239 |
|
| 240 |
def update_ear_visibility(selection, settings_map):
|
| 241 |
has_settings = settings_map.get(selection) is not None
|
| 242 |
return gr.update(visible=has_settings)
|
| 243 |
|
| 244 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 245 |
-
if updated_settings is None or active_id is None:
|
|
|
|
| 246 |
if active_id == sampler_dd.elem_id:
|
| 247 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 248 |
elif active_id == model_dd.elem_id:
|
| 249 |
model_settings_map_py[model_val] = updated_settings
|
| 250 |
|
| 251 |
def close_the_flyout():
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
sampler_dd.change(
|
| 255 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 256 |
inputs=[sampler_dd],
|
| 257 |
outputs=[sampler_ear_btn]
|
| 258 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 259 |
|
| 260 |
sampler_ear_btn.click(
|
| 261 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 262 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 263 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 264 |
-
).then(fn=None, inputs=
|
| 265 |
|
| 266 |
model_dd.change(
|
| 267 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 268 |
inputs=[model_dd],
|
| 269 |
outputs=[model_ear_btn]
|
| 270 |
-
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id,
|
|
|
|
| 271 |
|
| 272 |
model_ear_btn.click(
|
| 273 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 274 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 275 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 276 |
-
).then(fn=None, inputs=
|
| 277 |
|
| 278 |
flyout_sheet.change(
|
| 279 |
fn=on_flyout_change,
|
|
@@ -284,16 +224,24 @@ with gr.Blocks(title="PropertySheet Demos") as demo:
|
|
| 284 |
close_btn.click(
|
| 285 |
fn=close_the_flyout,
|
| 286 |
inputs=None,
|
| 287 |
-
outputs=[flyout_visible, active_anchor_id,
|
| 288 |
-
)
|
| 289 |
|
| 290 |
def initial_flyout_setup(sampler_val, model_val):
|
| 291 |
return {
|
| 292 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 293 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
if __name__ == "__main__":
|
| 299 |
demo.launch()
|
|
|
|
| 39 |
|
| 40 |
```python
|
| 41 |
import os
|
| 42 |
+
import json
|
| 43 |
import gradio as gr
|
| 44 |
from dataclasses import dataclass, field, asdict
|
| 45 |
from typing import Literal
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
+
from gradio_htmlinjector import HTMLInjector
|
| 48 |
|
| 49 |
+
# --- 1. Dataclass Definitions (unchanged) ---
|
|
|
|
|
|
|
|
|
|
| 50 |
@dataclass
|
| 51 |
class ModelSettings:
|
|
|
|
| 52 |
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(default="SDXL", metadata={"component": "dropdown", "label": "Base Model"})
|
| 53 |
custom_model_path: str = field(default="/path/to/default.safetensors", metadata={"label": "Custom Model Path", "interactive_if": {"field": "model_type", "value": "Custom"}})
|
| 54 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
| 55 |
@dataclass
|
| 56 |
class SamplingSettings:
|
|
|
|
| 57 |
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(default="DPM++ 2M Karras", metadata={"component": "dropdown", "label": "Sampler"})
|
| 58 |
steps: int = field(default=25, metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1})
|
| 59 |
cfg_scale: float = field(default=7.0, metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5})
|
|
|
|
| 60 |
@dataclass
|
| 61 |
class RenderConfig:
|
|
|
|
| 62 |
seed: int = field(default=-1, metadata={"component": "number_integer", "label": "Seed (-1 for random)"})
|
| 63 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 64 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
| 65 |
@dataclass
|
| 66 |
class Lighting:
|
|
|
|
| 67 |
sun_intensity: float = field(default=1.0, metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1})
|
| 68 |
color: str = field(default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"})
|
|
|
|
| 69 |
@dataclass
|
| 70 |
class EnvironmentConfig:
|
|
|
|
| 71 |
background: Literal["Sky", "Color", "Image"] = field(default="Sky", metadata={"component": "dropdown"})
|
| 72 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class EulerSettings:
|
|
|
|
| 75 |
s_churn: float = field(default=0.0, metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01})
|
|
|
|
| 76 |
@dataclass
|
| 77 |
class DPM_Settings:
|
|
|
|
| 78 |
karras_style: bool = field(default=True, metadata={"label": "Use Karras Sigma Schedule"})
|
| 79 |
|
| 80 |
+
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
|
|
|
|
|
|
| 81 |
initial_render_config = RenderConfig()
|
| 82 |
initial_env_config = EnvironmentConfig()
|
|
|
|
|
|
|
| 83 |
sampler_settings_map_py = {"Euler": EulerSettings(), "DPM++ 2M Karras": DPM_Settings(), "UniPC": None}
|
| 84 |
model_settings_map_py = {"SDXL 1.0": DPM_Settings(), "Stable Diffusion 1.5": EulerSettings(), "Pony": None}
|
| 85 |
|
| 86 |
+
# --- 3. CSS & JS Injection function (unchanged) ---
|
|
|
|
| 87 |
def inject_assets():
|
| 88 |
\"\"\"
|
| 89 |
+
This function prepares the payload of CSS, JS, and Body HTML for injection.
|
|
|
|
| 90 |
\"\"\"
|
| 91 |
+
popup_html = \"\"\"<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>\"\"\"
|
| 92 |
css_code = ""
|
| 93 |
js_code = ""
|
| 94 |
+
|
|
|
|
| 95 |
try:
|
| 96 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 97 |
css_code += f.read() + "\n"
|
| 98 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 99 |
js_code += f.read() + "\n"
|
| 100 |
except FileNotFoundError as e:
|
| 101 |
print(f"Warning: Could not read asset file: {e}")
|
| 102 |
+
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# --- 4. Gradio App Build ---
|
| 105 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 106 |
+
html_injector = HTMLInjector()
|
| 107 |
gr.Markdown("# PropertySheet Component Demos")
|
| 108 |
|
| 109 |
+
with gr.Row():
|
| 110 |
+
# --- Flyout popup ---
|
| 111 |
+
with gr.Column(elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]) as flyout_panel_source:
|
| 112 |
+
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 113 |
+
flyout_sheet = PropertySheet(visible=True, container=False, label="Settings", show_group_name_only_one=False, disable_accordion=True)
|
| 114 |
+
|
| 115 |
with gr.Tabs():
|
| 116 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 117 |
gr.Markdown("An example of using the `PropertySheet` component as a traditional sidebar for settings.")
|
|
|
|
| 118 |
render_state = gr.State(value=initial_render_config)
|
| 119 |
env_state = gr.State(value=initial_env_config)
|
| 120 |
sidebar_visible = gr.State(False)
|
|
|
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column(scale=3):
|
| 123 |
generate = gr.Button("Show Settings", variant="primary")
|
| 124 |
with gr.Row():
|
| 125 |
output_render_json = gr.JSON(label="Live Render State")
|
| 126 |
output_env_json = gr.JSON(label="Live Environment State")
|
|
|
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
+
render_sheet = PropertySheet(value=initial_render_config, label="Render Settings", width=400, height=550, visible=False, root_label="Generator")
|
| 129 |
+
environment_sheet = PropertySheet(value=initial_env_config, label="Environment Settings", width=400, open=False, visible=False, root_label="General")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 131 |
new_visibility = not is_visible
|
| 132 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 133 |
+
return (new_visibility, gr.update(visible=new_visibility, value=render_cfg), gr.update(visible=new_visibility, value=env_cfg), gr.update(value=button_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
def handle_render_change(updated_config: RenderConfig, current_state: RenderConfig):
|
| 135 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
| 136 |
+
if updated_config.model.model_type != "Custom": updated_config.model.custom_model_path = "/path/to/default.safetensors"
|
|
|
|
|
|
|
| 137 |
return updated_config, asdict(updated_config), updated_config
|
|
|
|
| 138 |
def handle_env_change(updated_config: EnvironmentConfig, current_state: EnvironmentConfig):
|
| 139 |
+
if updated_config is None: return current_state, asdict(current_state), current_state
|
|
|
|
| 140 |
return updated_config, asdict(updated_config), current_state
|
| 141 |
+
generate.click(fn=change_visibility, inputs=[sidebar_visible, render_state, env_state], outputs=[sidebar_visible, render_sheet, environment_sheet, generate])
|
| 142 |
+
render_sheet.change(fn=handle_render_change, inputs=[render_sheet, render_state], outputs=[render_sheet, output_render_json, render_state])
|
| 143 |
+
environment_sheet.change(fn=handle_env_change, inputs=[environment_sheet, env_state], outputs=[environment_sheet, output_env_json, env_state])
|
| 144 |
+
demo.load(fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)), inputs=[render_state, env_state], outputs=[output_render_json, output_env_json])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
with gr.TabItem("Flyout Popup Demo"):
|
| 147 |
gr.Markdown("An example of attaching a `PropertySheet` as a flyout panel to other components.")
|
| 148 |
|
| 149 |
+
# --- State Management ---
|
| 150 |
flyout_visible = gr.State(False)
|
| 151 |
active_anchor_id = gr.State(None)
|
| 152 |
+
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 153 |
|
| 154 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 155 |
with gr.Row(elem_classes=["fake-input-container", "no-border-dropdown"]):
|
|
|
|
| 160 |
model_dd = gr.Dropdown(choices=list(model_settings_map_py.keys()), label="Model", value="SDXL 1.0", elem_id="model_dd", scale=10)
|
| 161 |
model_ear_btn = gr.Button("⚙️", elem_id="model_ear_btn", scale=1, elem_classes=["integrated-ear-btn"])
|
| 162 |
|
| 163 |
+
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
# --- Event Logic ---
|
| 166 |
def handle_flyout_toggle(is_vis, current_anchor, clicked_dropdown_id, settings_obj):
|
| 167 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 168 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 169 |
+
return False, None, gr.update(), gr.update(value=js_data)
|
| 170 |
else:
|
| 171 |
+
js_data = json.dumps({"isVisible": True, "anchorId": clicked_dropdown_id})
|
| 172 |
+
return True, clicked_dropdown_id, gr.update(value=settings_obj), gr.update(value=js_data)
|
| 173 |
|
| 174 |
def update_ear_visibility(selection, settings_map):
|
| 175 |
has_settings = settings_map.get(selection) is not None
|
| 176 |
return gr.update(visible=has_settings)
|
| 177 |
|
| 178 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 179 |
+
if updated_settings is None or active_id is None:
|
| 180 |
+
return
|
| 181 |
if active_id == sampler_dd.elem_id:
|
| 182 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 183 |
elif active_id == model_dd.elem_id:
|
| 184 |
model_settings_map_py[model_val] = updated_settings
|
| 185 |
|
| 186 |
def close_the_flyout():
|
| 187 |
+
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 188 |
+
return False, None, gr.update(value=js_data)
|
| 189 |
+
|
| 190 |
+
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 191 |
|
| 192 |
sampler_dd.change(
|
| 193 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 194 |
inputs=[sampler_dd],
|
| 195 |
outputs=[sampler_ear_btn]
|
| 196 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 197 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 198 |
|
| 199 |
sampler_ear_btn.click(
|
| 200 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)),
|
| 201 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 202 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 203 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 204 |
|
| 205 |
model_dd.change(
|
| 206 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 207 |
inputs=[model_dd],
|
| 208 |
outputs=[model_ear_btn]
|
| 209 |
+
).then(fn=close_the_flyout, outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 210 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 211 |
|
| 212 |
model_ear_btn.click(
|
| 213 |
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)),
|
| 214 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 215 |
+
outputs=[flyout_visible, active_anchor_id, flyout_sheet, js_data_bridge]
|
| 216 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 217 |
|
| 218 |
flyout_sheet.change(
|
| 219 |
fn=on_flyout_change,
|
|
|
|
| 224 |
close_btn.click(
|
| 225 |
fn=close_the_flyout,
|
| 226 |
inputs=None,
|
| 227 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 228 |
+
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 229 |
|
| 230 |
def initial_flyout_setup(sampler_val, model_val):
|
| 231 |
return {
|
| 232 |
sampler_ear_btn: update_ear_visibility(sampler_val, sampler_settings_map_py),
|
| 233 |
model_ear_btn: update_ear_visibility(model_val, model_settings_map_py)
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
# --- App Load ---
|
| 237 |
+
demo.load(
|
| 238 |
+
fn=inject_assets, inputs=None, outputs=[html_injector]
|
| 239 |
+
).then(
|
| 240 |
+
fn=initial_flyout_setup, inputs=[sampler_dd, model_dd], outputs=[sampler_ear_btn, model_ear_btn]
|
| 241 |
+
).then(
|
| 242 |
+
fn=None, inputs=None, outputs=None,
|
| 243 |
+
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 244 |
+
)
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.launch()
|
src/pyproject.toml
CHANGED
|
@@ -16,7 +16,7 @@ requires-python = ">=3.10"
|
|
| 16 |
authors = [{ name = "Eliseu Silva", email = "[email protected]" }]
|
| 17 |
keywords = ["gradio-custom-component", "gradio-template-Fallback"]
|
| 18 |
# Add dependencies here
|
| 19 |
-
dependencies = ["gradio>=4.0,<6.0"
|
| 20 |
classifiers = [
|
| 21 |
'Development Status :: 3 - Alpha',
|
| 22 |
'Operating System :: OS Independent',
|
|
@@ -48,4 +48,4 @@ dev = ["build", "twine"]
|
|
| 48 |
artifacts = ["/backend/gradio_propertysheet/templates", "*.pyi", "/\\backend\\gradio_propertysheet\\templates"]
|
| 49 |
|
| 50 |
[tool.hatch.build.targets.wheel]
|
| 51 |
-
packages = ["/backend/gradio_propertysheet"]
|
|
|
|
| 16 |
authors = [{ name = "Eliseu Silva", email = "[email protected]" }]
|
| 17 |
keywords = ["gradio-custom-component", "gradio-template-Fallback"]
|
| 18 |
# Add dependencies here
|
| 19 |
+
dependencies = ["gradio>=4.0,<6.0"]
|
| 20 |
classifiers = [
|
| 21 |
'Development Status :: 3 - Alpha',
|
| 22 |
'Operating System :: OS Independent',
|
|
|
|
| 48 |
artifacts = ["/backend/gradio_propertysheet/templates", "*.pyi", "/\\backend\\gradio_propertysheet\\templates"]
|
| 49 |
|
| 50 |
[tool.hatch.build.targets.wheel]
|
| 51 |
+
packages = ["/backend/gradio_propertysheet"]
|