Closed beskrovnykh closed 1 year ago
I looked in the debugger for what is the reason, the check below does not pass
if not set(Context.root_block.blocks).isdisjoint(self.blocks):
raise DuplicateBlockError(
"At least one block in this Blocks has already been rendered."
)
The problem is that
set(Context.root_block.blocks).intersection(self.blocks)
returns a single element, 1, which is the id of the state block, but as far as I know the State component is not assumed to be rendered. Can we weaken this check to make the application run?
Workaround
Skip interfaces and use low-level blocks. On blocks, this code does what I need, but then I have to rewrite 3 tabs to blocks, which takes a long time
demo = gr.Blocks()
with demo:
gr.Markdown("Example of usage state variable in tabbed environment.")
with gr.Tabs() as tabs:
state = gr.State("")
asc_tab = gr.TabItem("Ask question")
with asc_tab:
with gr.Row():
ask_input = gr.Textbox()
answer_output = gr.Textbox()
clear_button = gr.Button("Clear")
submit_button = gr.Button("Submit")
with gr.TabItem("Show question") as tab2:
with gr.Row():
question = gr.Textbox(interactive=False)
tab2.select(lambda s: (s, f"User question: {s}"), [state], [state, question])
submit_button.click(lambda s, x: (x, f"I don't know"), inputs=[state, ask_input],
outputs=[state, answer_output])
demo.launch()
Very interesting issue, let me take a look @beskrovnykh!
How fast, I did not expect! Thank you
Describe the bug
I was attempting to use a State component in a TabbedInterface to share state between different tabs. However, when running the application, it crashed with the error "DuplicateBlockError: At least one block in this Blocks has already been rendered." Upon further investigation, I discovered that the State component was being treated as a rendered block, even though it was not supposed to be. I believe that this is a bug in the Gradio library. A workaround is to use low-level blocks instead of interfaces, but this requires a significant rewrite of the code.
Is there an existing issue for this?
Reproduction
Screenshot
No response
Logs
System Info
Severity
blocking all usage of gradio