#!/usr/bin/env python """06 -- gr.State: per-session state, isolated between browser tabs. gr.State lives for one browser session. A module-level variable would instead be shared by every concurrent user -- a real bug in multi-user deployments. """ import gradio as gr from _common import launch def add(item, history): history = list(history or []) if item: history.append(item) return history, "\n".join(f"{i+1}. {h}" for i, h in enumerate(history)), "" def reset(): return [], "", "" with gr.Blocks(title="Session state") as demo: gr.Markdown("## Per-session list\nOpen a second browser tab: the lists " "are independent.") state = gr.State([]) # not shared across sessions entry = gr.Textbox(label="Add an item") view = gr.Textbox(label="Your items", lines=6) with gr.Row(): gr.Button("Add", variant="primary").click( add, [entry, state], [state, view, entry], api_name="add") gr.Button("Reset").click(reset, None, [state, view, entry]) entry.submit(add, [entry, state], [state, view, entry]) if __name__ == "__main__": launch(demo)