gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.97k stars 2.58k forks source link

Can the tab be redirected by clicking the button? #8156

Closed coolboyqu closed 6 months ago

coolboyqu commented 6 months ago

For example, if I choose Tab4 now, I would like to select Tab1 by clicking on a button or something else. Does Gradio currently have similar features?

mussonero commented 6 months ago

try this

import gradio as gr

def change_tab(id):
    return gr.Tabs(selected=id)

with gr.Blocks() as demo:
    with gr.Tabs() as tabs:
        with gr.TabItem("Train", id=0): #index is 0
            t = gr.Textbox()
        with gr.TabItem("Inference", id=1): #index is 1
            i = gr.Image()

    btn = gr.Button()
    # When the button is clicked, switch to Tab1 (index 1)
    btn.click(change_tab, gr.Number(1, visible=False), tabs)

demo.launch()
abidlabs commented 6 months ago

Thanks @mussonero! Yes that's the correct approach for selecting tabs programmatically