-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use gr.Accordion
as output for gr.Checkbox.change
#10208
Comments
Hi @acylam I can reproduce the issue, but I can't reproduce the error not happening when I set an accordion as an output for other event listeners like import gradio as gr
def toggle_accordion(at: bool):
return gr.Accordion(visible=False)
with gr.Blocks() as demo:
b = gr.Button()
with gr.Accordion(
label="this is an accordion", open=True, visible=True
) as accordion:
gr.Markdown("some text")
b.click(
fn=toggle_accordion,
inputs=[accordion_toggle],
outputs=[accordion],
)
if __name__ == "__main__":
demo.launch() Can you share an example where an accordion is working as an output component? |
Hi @abidlabs , thanks for swift response. Your example gives an error because import gradio as gr
def toggle_accordion():
return gr.Accordion(visible=False)
with gr.Blocks() as demo:
b = gr.Button()
with gr.Accordion(
label="this is an accordion", open=True, visible=True
) as accordion:
gr.Markdown("some text")
b.click(
fn=toggle_accordion,
inputs=None,
outputs=[accordion],
)
if __name__ == "__main__":
demo.launch() |
The current work around is to use a button to simulate a checkbox and use a import gradio as gr
def toggle_accordion(accordion_visibility: bool):
new_accordion = gr.Accordion(visible=not accordion_visibility)
return [not accordion_visibility, new_accordion]
with gr.Blocks() as demo:
b = gr.Button("Toggle Accordion Visibility")
with gr.Accordion(
label="this is an accordion", open=True, visible=True
) as accordion:
gr.Markdown("some text")
accordion_visibility = gr.State(accordion.visible)
b.click(
fn=toggle_accordion,
inputs=[accordion_visibility],
outputs=[accordion_visibility, accordion],
)
if __name__ == "__main__":
demo.launch() |
Hi @abidlabs, I've been testing some more cases and have narrowed down the issue further. TLDR; Error occurs when there is only 1 output component returned in a list. Works as expected otherwise. See example below: import gradio as gr
def toggle_accordion_2_comps(
accordion_toggle: bool,
):
output_accordion = gr.Accordion(visible=accordion_toggle)
return [True, output_accordion]
def toggle_accordion_1_comp_no_list(
accordion_toggle: bool,
):
output_accordion = gr.Accordion(visible=accordion_toggle)
return output_accordion
def toggle_accordion_1_comp_list(
accordion_toggle: bool,
):
output_accordion = gr.Accordion(visible=accordion_toggle)
return [output_accordion]
with gr.Blocks() as demo:
accordion_toggle1 = gr.Checkbox(
value=True,
label="Accordion toggle (2 output components)",
show_label=True,
interactive=True,
visible=True,
)
accordion_toggle2 = gr.Checkbox(
value=True,
label="Accordion toggle (1 output component no list)",
show_label=True,
interactive=True,
visible=True,
)
accordion_toggle3 = gr.Checkbox(
value=True,
label="Accordion toggle (1 output component with list)",
show_label=True,
interactive=True,
visible=True,
)
with gr.Accordion(
label="this is an accordion", open=True, visible=True
) as accordion:
gr.Markdown("some text")
dummy_state = gr.State(True)
accordion_toggle1.change(
fn=toggle_accordion_2_comps,
inputs=[accordion_toggle1],
outputs=[dummy_state, accordion],
)
accordion_toggle2.change(
fn=toggle_accordion_1_comp_no_list,
inputs=[accordion_toggle2],
outputs=[accordion],
)
accordion_toggle3.change(
fn=toggle_accordion_1_comp_list,
inputs=[accordion_toggle3],
outputs=[accordion],
)
if __name__ == "__main__":
demo.launch() It seems that the crux of the problem is with how you're returning the output components from the event function. As I observed, the error only occurs when the event function returns a list containing a single component ( Not sure if this is expected behavior, but IMO, all three cases should work, or at least |
Describe the bug
I'm trying to toggle a
gr.Accordion
's visibility based on agr.Checkbox
selection, but run into this error:This doesn't seem to happen when I set an accordion as an output for other event listeners like
gr.Button.click
. Doesgr.Checkbox.change
currently not supportgr.Accordion
as output?Have you searched existing issues? 🔎
Reproduction
Screenshot
Logs
System Info
Severity
I can work around it
The text was updated successfully, but these errors were encountered: