Search code examples
pythongradio

In a gradio tab GUI, a button calls the other tab


I have the following script

import gradio as gr

# Define the function for the first tab
def greet(text):
    return f"Hello {text}"

# Define the function for the second tab
def farewell(text):
    return f"Goodbye {text}"

# Create the interface for the first tab
with gr.Blocks() as greet_interface:
    input_text = gr.Textbox(label="Input Text")
    output_text = gr.Textbox(label="Output Text")
    button = gr.Button("Submit")
    button.click(greet, inputs=input_text, outputs=output_text)

# Create the interface for the second tab
with gr.Blocks() as farewell_interface:
    input_text = gr.Textbox(label="Input Text")
    output_text = gr.Textbox(label="Output Text")
    button = gr.Button("Submit")
    button.click(farewell, inputs=input_text, outputs=output_text)

# Combine the interfaces into tabs
with gr.Blocks() as demo:
    with gr.Tabs():
        with gr.TabItem("Greet"):
            greet_interface.render()
        with gr.TabItem("Farewell"):
            farewell_interface.render()

# Launch the interface
# demo.launch()
demo.launch(server_name="0.0.0.0", server_port=7861)

I am scratching my head because this works in one environment I have, and yet in the other it fails terribly.

How it fails:

In the second tab (farewell) when the button is pressed, it actually calls the greet function. farewell is never called.

I can see that some processing is being done in the output_text of the second tab but it never is completed. Instead the output text of the first tab is filled

I can not comprehend why this is happening.

The only difference I have is that of the environments

  1. The environment where it works:
  • Python 3.11.1
  • use venv
  • gradio 4.37.2
  1. The environment where it fails
  • Python 3.9.16
  • use poetry
  • gradio 4.32.2

Can someone help me with this strange occurrence? Is tabbed gradio buggy?

Btw, I already tried using completely different variables per tab but that does not work


Solution

  • Yes, tabbed gradio was buggy. It was fixed in version 4.36.1 See the changelog:

    Fixes TabbedInterface bug where only first interface events get triggered.

    This explains the differences you see between both versions (4.32.2 < 4.36.1 < 4.37.2)