gradio: [Dynamic components] - Problem with update values on client after component is removed and added again.

Describe the bug

Hi guys, i again.

I’m experiencing a new bug. When I add a component dynamically and change its value everything is fine. So if I remove the component and then add it again, on the server side it assumes the default value, but on the client it shows the values ​​from the first time I added it (as if it got it from some cache).

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr

selected_loras=[]
loras_scales=[]
LORA_FILES = ["A.safetensors", "B.safetensors", "C.safetensors", "D.safetensors"]

def click_add_lora(lora):    
    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 submit_button(progress=gr.Progress(track_tqdm=True)):
    print('Testing')
    for i in loras_scales:
        print(f"key: {i.key[8:]}, value:{i.value}")

    return "See the result in the terminal!"

with gr.Blocks(analytics_enabled=False) as app:                                    
    with gr.Row():
        with gr.Column(scale=0.4, min_width=100):
            lora_files = gr.Dropdown(label="LoRA model", choices=LORA_FILES, value=LORA_FILES[0])
        with gr.Column(scale=0, min_width=50):
            refresh_lora = gr.Button(value="Refresh", elem_id="lora_refresh_button")
        with gr.Column(scale=0, min_width=50):                                                
            add_lora = gr.Button(value="+", elem_id="add_lora_button")
        with gr.Column(scale=0, min_width=50):                                                
            submit_test = gr.Button(value="Submit", elem_id="submit_button" )
    with gr.Row():
        status = gr.Textbox(label="Status", value="", show_label=False)
    with gr.Row():                                              
        with gr.Column():                                                                                                        
            selected_loras_state= gr.State(value=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): 
                global loras_scales                                                 
                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):                                            
                                    remove_lora = gr.Button(value="X", 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]}")                                                                
                            
                            def click_remove_lora(value, lora=selected[i]):                                                                    
                                for l in range(len(loras_scales)):
                                    if loras_scales[l].key[8:] == lora:
                                        loras_scales.pop(l)
                                        break
                                selected_loras.pop(selected_loras.index(value))
                                return selected_loras
                            
                            remove_lora.click(fn=click_remove_lora, inputs=lora, outputs=selected_loras_state)
                            
                            def change_lora_scale(value, lora=selected[i]):
                                for l in range(len(loras_scales)):
                                    if loras_scales[l].key[8:] == lora:
                                        loras_scales[l].value=value                                                                    
                                                                
                            lora_scale.release(fn=change_lora_scale, inputs=lora_scale)                                                               

                            hasLora = False
                            for l in range(len(loras_scales)):
                                if loras_scales[l].key[8:] == selected[i]:
                                    hasLora = True
                                    break
                            if not hasLora:
                                    loras_scales.append(lora_scale)    

    submit_test.click(           
        fn=submit_button,        
        outputs=status)

app.launch(inbrowser=True)

Screenshot

Reproduction on screenshots:

1º I added 2 combo items, and changed the default value of the first one to 1.7. image

2º Now i removed the first element card with click on “X”. Only the second element remains. image

3º Now i add the first element again, that with name “Beyond_Reality_v2.safetensors”, and it will be the second card now. Note: put a breakpoint on the last lines of code, and see the value property of lora_scale, It has the default value which is the desired one.:

 if not hasLora:
    loras_scales.append(lora_scale) 

But on the page the element returns with the value it had in step 1. image

Logs

N/A

System Info

Gradio Environment Information:
------------------------------
Operating System: Windows
gradio version: 4.42.0
gradio_client version: 1.3.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
anyio: 4.4.0
fastapi: 0.112.0
ffmpy: 0.4.0
gradio-client==1.3.0 is not installed.
httpx: 0.27.0
huggingface-hub: 0.24.5
importlib-resources: 6.4.0
jinja2: 3.1.3
markupsafe: 2.1.5
matplotlib: 3.9.0
numpy: 1.26.3
orjson: 3.10.6
packaging: 24.1
pandas: 2.2.2
pillow: 10.2.0
pydantic: 2.8.2
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.5.6
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.5
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.


gradio_client dependencies in your environment:

fsspec: 2024.2.0
httpx: 0.27.0
huggingface-hub: 0.24.5
packaging: 24.1
typing-extensions: 4.12.2
websockets: 12.0

Severity

Blocking usage of gradio

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (4 by maintainers)

Most upvoted comments

I’ll go ahead and close since it seems like you were able to find a workaround @elismasilva

Sorry we’ve been working on tackling some gr.render() bugs and we haven’t gotten to this one yet. Its on our radar though and we’ll fix as soon as we can!