+{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: textbox_custom_buttons"]}, {"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", "def export_data(text):\n", " print(\"Exporting data:\", text)\n", " return \"Data exported to server!\"\n", "\n", "def refresh_data():\n", " import random\n", " return f\"Refreshed content: {random.randint(1000, 9999)}\"\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"\"\"\n", " # Textbox with Custom Buttons Demo\n", " \n", " This demo showcases custom buttons in a Textbox component that can trigger either (or both):\n", " - **Python functions** \n", " - **JS functions** (with and without input parameters)\n", " \n", " You can use emojis, text, or icons for the buttons.\n", " \"\"\")\n", " \n", " gr.Markdown(\"### Textbox with Custom Buttons\")\n", " refresh_btn = gr.Button(\"Refresh\")\n", " alert_btn = gr.Button(\"\u26a0\ufe0f Alert\")\n", " clear_btn = gr.Button(\"\ud83d\uddd1\ufe0f\")\n", " \n", " textbox = gr.Textbox(\n", " value=\"Sample text content that can be exported, refreshed, or transformed.\",\n", " buttons=[\"copy\", refresh_btn, alert_btn, clear_btn],\n", " label=\"Sample Text\",\n", " lines=5\n", " )\n", " \n", " output = gr.Textbox(label=\"Output (Python Function Result)\")\n", " \n", " \n", " refresh_btn.click(refresh_data, outputs=textbox)\n", " \n", " alert_btn.click(\n", " None,\n", " inputs=textbox,\n", " outputs=[],\n", " js=\"(text) => { alert('This is a JavaScript alert!\\\\n\\\\nTextbox content: ' + text); return []; }\"\n", " )\n", " \n", " \n", " clear_btn.click(\n", " None,\n", " inputs=[],\n", " outputs=textbox,\n", " js=\"() => ''\"\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n", "\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
0 commit comments