{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: timer"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "import time\n", "\n", "with gr.Blocks() as demo:\n", " timer = gr.Timer(1)\n", " timestamp = gr.Number(label=\"Time\")\n", " timer.tick(lambda: round(time.time()), outputs=timestamp)\n", " gr.Number(lambda: round(time.time()), label=\"Time 2\", every=1)\n", "\n", " with gr.Row():\n", " timestamp_3 = gr.Number()\n", " start_btn = gr.Button(\"Start\")\n", " stop_btn = gr.Button(\"Stop\")\n", "\n", " time_3 = start_btn.click(lambda: round(time.time()), None, timestamp_3, every=1)\n", " stop_btn.click(fn=None, cancels=time_3)\n", "\n", " with gr.Row():\n", " min = gr.Number(1, label=\"Min\")\n", " max = gr.Number(10, label=\"Max\")\n", " timer2 = gr.Timer(1)\n", " number = gr.Number(lambda a, b: random.randint(a, b), inputs=[min, max], every=timer2, label=\"Random Number\")\n", " with gr.Row():\n", " gr.Button(\"Start\").click(lambda: gr.Timer(active=True), None, timer2)\n", " gr.Button(\"Stop\").click(lambda: gr.Timer(active=False), None, timer2)\n", " gr.Button(\"Go Fast\").click(lambda: 0.2, None, timer2)\n", " gr.Button(\"Go Slow\").click(lambda: 2, None, timer2)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}