Spaces:
Running on Zero
Running on Zero
Fix canvas not rendering: keep bridge components mounted (CSS-hidden) instead of visible=False; robust gradio id selectors
Browse files
app.py
CHANGED
|
@@ -355,9 +355,17 @@ SCRIPT = r"""
|
|
| 355 |
let id=base, n=1; const ids=new Set(S.elements.map(e=>e.id));
|
| 356 |
while(ids.has(id)){ id=base+"_"+(++n);} return id; }
|
| 357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
function setHidden(id,val){
|
| 359 |
-
const
|
| 360 |
-
const el=w.querySelector('textarea,input'); if(!el) return;
|
| 361 |
const proto = el.tagName==='TEXTAREA'?window.HTMLTextAreaElement.prototype:window.HTMLInputElement.prototype;
|
| 362 |
const setter=Object.getOwnPropertyDescriptor(proto,'value').set;
|
| 363 |
setter.call(el,val); el.dispatchEvent(new Event('input',{bubbles:true}));
|
|
@@ -523,7 +531,7 @@ SCRIPT = r"""
|
|
| 523 |
S.seed=-1; S.step=4; renderSteps(); syncState();
|
| 524 |
NS.el.board.classList.add('busy');
|
| 525 |
NS.el.genbtn.setAttribute('disabled','');
|
| 526 |
-
setTimeout(()=>{ const b=
|
| 527 |
clearTimeout(NS._to); NS._to=setTimeout(()=>{ NS.el.board.classList.remove('busy');
|
| 528 |
NS.el.genbtn.removeAttribute('disabled'); }, 300000);
|
| 529 |
}
|
|
@@ -607,7 +615,7 @@ SCRIPT = r"""
|
|
| 607 |
|
| 608 |
function init(){
|
| 609 |
const root=document.getElementById('f3-root');
|
| 610 |
-
if(!root || !
|
| 611 |
return false;
|
| 612 |
}
|
| 613 |
if(root.dataset.ready) return true;
|
|
@@ -621,10 +629,15 @@ SCRIPT = r"""
|
|
| 621 |
HEAD = STYLE + STYLE2 + CONFIG_JS + SCRIPT
|
| 622 |
|
| 623 |
with gr.Blocks(head=HEAD, title="FLUX.3 layout canvas",
|
| 624 |
-
css="footer{display:none!important} .gradio-container{max-width:1240px!important}"
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
gr.HTML('<div id="f3-root"></div>')
|
| 629 |
|
| 630 |
ev = gen_btn.click(generate, [state_box], [result_box], api_name="generate",
|
|
|
|
| 355 |
let id=base, n=1; const ids=new Set(S.elements.map(e=>e.id));
|
| 356 |
while(ids.has(id)){ id=base+"_"+(++n);} return id; }
|
| 357 |
|
| 358 |
+
// gradio may put elem_id on the field/button itself OR on a wrapper that contains it.
|
| 359 |
+
function fieldEl(id){
|
| 360 |
+
const w=document.getElementById(id); if(!w) return null;
|
| 361 |
+
return w.matches('textarea,input') ? w : w.querySelector('textarea,input');
|
| 362 |
+
}
|
| 363 |
+
function genButton(){
|
| 364 |
+
const g=document.getElementById('f3-gen'); if(!g) return null;
|
| 365 |
+
return g.tagName==='BUTTON' ? g : g.querySelector('button');
|
| 366 |
+
}
|
| 367 |
function setHidden(id,val){
|
| 368 |
+
const el=fieldEl(id); if(!el) return;
|
|
|
|
| 369 |
const proto = el.tagName==='TEXTAREA'?window.HTMLTextAreaElement.prototype:window.HTMLInputElement.prototype;
|
| 370 |
const setter=Object.getOwnPropertyDescriptor(proto,'value').set;
|
| 371 |
setter.call(el,val); el.dispatchEvent(new Event('input',{bubbles:true}));
|
|
|
|
| 531 |
S.seed=-1; S.step=4; renderSteps(); syncState();
|
| 532 |
NS.el.board.classList.add('busy');
|
| 533 |
NS.el.genbtn.setAttribute('disabled','');
|
| 534 |
+
setTimeout(()=>{ const b=genButton(); if(b)b.click(); },70);
|
| 535 |
clearTimeout(NS._to); NS._to=setTimeout(()=>{ NS.el.board.classList.remove('busy');
|
| 536 |
NS.el.genbtn.removeAttribute('disabled'); }, 300000);
|
| 537 |
}
|
|
|
|
| 615 |
|
| 616 |
function init(){
|
| 617 |
const root=document.getElementById('f3-root');
|
| 618 |
+
if(!root || !fieldEl('f3-state') || !genButton()){
|
| 619 |
return false;
|
| 620 |
}
|
| 621 |
if(root.dataset.ready) return true;
|
|
|
|
| 629 |
HEAD = STYLE + STYLE2 + CONFIG_JS + SCRIPT
|
| 630 |
|
| 631 |
with gr.Blocks(head=HEAD, title="FLUX.3 layout canvas",
|
| 632 |
+
css="footer{display:none!important} .gradio-container{max-width:1240px!important}"
|
| 633 |
+
" .f3-bridge{position:absolute!important;left:-9999px!important;top:0;"
|
| 634 |
+
"width:1px!important;height:1px!important;overflow:hidden!important;"
|
| 635 |
+
"opacity:0!important;pointer-events:none!important;}") as demo:
|
| 636 |
+
# Bridge components: kept MOUNTED (not visible=False, which unmounts them from the
|
| 637 |
+
# DOM in gradio 5.x) but hidden off-screen via CSS, so the canvas JS can find them.
|
| 638 |
+
state_box = gr.Textbox(elem_id="f3-state", elem_classes=["f3-bridge"])
|
| 639 |
+
result_box = gr.Textbox(elem_id="f3-result", elem_classes=["f3-bridge"])
|
| 640 |
+
gen_btn = gr.Button("gen", elem_id="f3-gen", elem_classes=["f3-bridge"])
|
| 641 |
gr.HTML('<div id="f3-root"></div>')
|
| 642 |
|
| 643 |
ev = gen_btn.click(generate, [state_box], [result_box], api_name="generate",
|