File size: 2,069 Bytes
bf8d67d
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: function_values"]}, {"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", "\n", "countries = [\n", "    \"Algeria\",\n", "    \"Argentina\",\n", "    \"Australia\",\n", "    \"Brazil\",\n", "    \"Canada\",\n", "    \"China\",\n", "    \"Democratic Republic of the Congo\",\n", "    \"Greenland (Denmark)\",\n", "    \"India\",\n", "    \"Kazakhstan\",\n", "    \"Mexico\",\n", "    \"Mongolia\",\n", "    \"Peru\",\n", "    \"Russia\",\n", "    \"Saudi Arabia\",\n", "    \"Sudan\",\n", "    \"United States\",\n", "]\n", "\n", "with gr.Blocks() as demo:\n", "    with gr.Row():\n", "        count = gr.Slider(1, 10, step=1, label=\"Country Count\")\n", "        alpha_order = gr.Checkbox(True, label=\"Alphabetical Order\")\n", "\n", "    gr.JSON(\n", "        lambda count, alpha_order: countries[:count]\n", "        if alpha_order\n", "        else countries[-count:],\n", "        inputs=[count, alpha_order],\n", "    )\n", "    timer = gr.Timer(1)\n", "    with gr.Row():\n", "        gr.Textbox(\n", "            lambda: random.choice(countries), label=\"Random Country\", every=timer\n", "        )\n", "        gr.Textbox(\n", "            lambda count: \", \".join(random.sample(countries, count)),\n", "            inputs=count,\n", "            label=\"Random Countries\",\n", "            every=timer,\n", "        )\n", "    with gr.Row():\n", "        gr.Button(\"Start\").click(lambda: gr.Timer(active=True), None, timer)\n", "        gr.Button(\"Stop\").click(lambda: gr.Timer(active=False), None, timer)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}