Open z3lx opened 4 months ago
same bug. waiting to be solved
Also see #8973
Hi @freddyaboulton, any update on this? I'm currently blocked in the development of a demo because of this issue, so I'd like to know if it will be fixed in the near future. Thanks!
Hi @freddyaboulton, any update on this? I'm currently blocked in the development of a demo because of this issue, so I'd like to know if it will be fixed in the near future. Thanks!
hi @gsarti i got this problem too and i am using events without listening decorator, here is my fixed code snipet:
with gr.Row():
with gr.Column():
selected_loras=[]
selected_loras_state= gr.State(value=selected_loras)
use_this_loras=[]
loras = []
loras_scales = []
def click_add_lora(lora):
global selected_loras
if lora not in selected_loras:
selected_loras.append(lora)
return selected_loras, None
else:
return selected_loras, "This model has already been added!"
def click_remove_lora(value):
global selected_lora
selected_loras.pop(selected_loras.index(value))
return selected_loras
add_lora.click(fn=click_add_lora, inputs=lora_files, outputs=[selected_loras_state, status])
@gr.render(inputs=selected_loras_state)
def render_loras(selected):
with gr.Row(elem_id="lora_row"):
for i in range(len(selected)):
with gr.Column(variant="panel", min_width=300):
with gr.Row():
with gr.Column(min_width=300):
#use_this_lora = gr.Checkbox(label="Apply Lora", value=False)
remove_lora = gr.Button(value=remove_symbol, elem_id="remove_lora_button", key=f"remove-lora-{selected[i]}", elem_classes="remove-button vertical-center")
lora = gr.Textbox(label="File name", value=f"{selected[i]}", key=f"label-{selected[i]}", show_label=True)
lora_scale = gr.Slider(
interactive=True,
minimum=0.1,
maximum=20.0,
step=0.1,
value=0.1,
label="Lora scale",
key=f"scale-{selected[i]}")
remove_lora.click(fn=click_remove_lora, inputs=lora, outputs=selected_loras_state)
this code is plotting cards when i click on "+" button from item selected in dropdown, and remove item when i click on "X" icon on the card. I was getting same error from _id attribute for the event remove_lora. I notice that object "selected_loras_state" lost _id property for some reason i dont know, other problem that i got was if i remove card the card remains on page but object is updated, so i found is necessary you have a exclusive key on compomentes that will be updated, in my case the value i got in "selected" variable is my filename that is unique then it is replaced on re-render, and when i was using only index variable not work because new itens got same index and generate this problem. Well, my code is working know, i dont now if it will help you..
Sorry for the delay here @gsarti @z3lx ! I actually think this is the correct intended behavior (at least for @z3lx 's repro). There is a cycle in the @render
decorator. Changing the code component, changes the value of the state variable, so the whole block gets re-rendered. That's why it looks like it's flickering and laggy. When I break the cycle, the input and blur events of the code component are triggered correctly.
Code
import gradio as gr
def identity(v):
print(v)
return v
with gr.Blocks() as demo:
show_checkbox = gr.Checkbox(label="Show", value=True)
text_state = gr.Textbox("The Code")
@gr.render(inputs=[show_checkbox])
def render(show: bool) -> None:
if not show:
return
text_input = gr.Code(
value="",
)
text_input.input(
fn=identity,
inputs=[text_input],
outputs=[text_state],
)
text_input.blur(
lambda: "blurred",
inputs=None,
outputs=[text_state],
)
demo.launch()
The issue here, like @freddyaboulton described, is that when text_input changes, it changes text_state, which causes text_input to re-render. This causes a loop that, when typing fast, can causes an input event to trigger on a test_input that no longer exists because the the backend believes it has re-rendered. There should be a better error message to explain the issue, but you should be using key= to preserve the value of the textbox between re-renders instead. You can still store the value of the textbox in a state if you'd like. See below:
import gradio as gr
def identity(v):
print(v)
return v
with gr.Blocks() as demo:
show_checkbox = gr.Checkbox(label="Show", value=True)
text_state = gr.State("Some text")
@gr.render(inputs=[show_checkbox])
def render(show: bool) -> None:
if not show:
return
text_input = gr.Textbox(key="text_input", label="Enter text")
text_input.input(
fn=identity,
inputs=[text_input],
outputs=[text_state],
)
demo.launch()
Describe the bug
The event listeners for the
gr.Code
component does not seem to work properly when used in a function with thegr.render
decorator.Code.change
andCode.input
does not trigger properly, instead triggers only once per re-render.Code.change
,Code.input
,Code.focus
andCode.blur
does not return the entered code, instead returns the component's initial value.These issues are not observed when using a
gr.Textbox
or agr.TextArea
.Have you searched existing issues? 🔎
Reproduction
Screenshot
No response
Logs
No response
System Info
Severity
I can work around it