#!/usr/bin/env python """02 -- gr.Blocks: explicit layout, multiple events, shared components. Blocks is what you want for anything beyond a single function: it gives you rows, columns, tabs, and full control over which event writes which output. """ import gradio as gr from _common import launch def to_upper(s): return (s or "").upper() def word_stats(s): words = (s or "").split() return {"characters": len(s or ""), "words": len(words), "unique words": len(set(w.lower() for w in words))} with gr.Blocks(title="Blocks layout") as demo: gr.Markdown("## Blocks layout\nTwo tabs, sharing one input box.") src = gr.Textbox(label="Input text", lines=3, value="The quick brown fox jumps over the lazy dog") with gr.Tabs(): with gr.Tab("Transform"): with gr.Row(): up_btn = gr.Button("Upper-case", variant="primary") clr_btn = gr.Button("Clear") out = gr.Textbox(label="Result", lines=3) up_btn.click(to_upper, src, out, api_name="to_upper") clr_btn.click(lambda: ("", ""), None, [src, out]) with gr.Tab("Statistics"): stat_btn = gr.Button("Count") stats = gr.JSON(label="Counts") stat_btn.click(word_stats, src, stats, api_name="word_stats") # change events fire as the user types src.change(to_upper, src, out) if __name__ == "__main__": launch(demo)