File size: 2,084 Bytes
c3f69c9
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_js_methods"]}, {"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", "\n", "blocks = gr.Blocks()\n", "\n", "with blocks as demo:\n", "    subject = gr.Textbox(placeholder=\"subject\")\n", "    verb = gr.Radio([\"ate\", \"loved\", \"hated\"])\n", "    object = gr.Textbox(placeholder=\"object\")\n", "\n", "    with gr.Row():\n", "        btn = gr.Button(\"Create sentence.\")\n", "        reverse_btn = gr.Button(\"Reverse sentence.\")\n", "        foo_bar_btn = gr.Button(\"Append foo\")\n", "        reverse_then_to_the_server_btn = gr.Button(\n", "            \"Reverse sentence and send to server.\"\n", "        )\n", "\n", "    def sentence_maker(w1, w2, w3):\n", "        return f\"{w1} {w2} {w3}\"\n", "\n", "    output1 = gr.Textbox(label=\"output 1\")\n", "    output2 = gr.Textbox(label=\"verb\")\n", "    output3 = gr.Textbox(label=\"verb reversed\")\n", "    output4 = gr.Textbox(label=\"front end process and then send to backend\")\n", "\n", "    btn.click(sentence_maker, [subject, verb, object], output1)\n", "    reverse_btn.click(\n", "        None, [subject, verb, object], output2, js=\"(s, v, o) => o + ' ' + v + ' ' + s\"\n", "    )\n", "    verb.change(lambda x: x, verb, output3, js=\"(x) => [...x].reverse().join('')\")\n", "    foo_bar_btn.click(None, [], subject, js=\"(x) => x + ' foo'\")\n", "\n", "    reverse_then_to_the_server_btn.click(\n", "        sentence_maker,\n", "        [subject, verb, object],\n", "        output4,\n", "        js=\"(s, v, o) => [s, v, o].map(x => [...x].reverse().join(''))\",\n", "    )\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}