freddyaboulton HF Staff commited on
Commit
95fbef7
·
verified ·
1 Parent(s): d441238

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +8 -8
  2. run.ipynb +1 -0
  3. run.py +56 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Textbox Custom Buttons
3
- emoji: 🐨
4
- colorFrom: gray
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 6.1.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: textbox_custom_buttons
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 6.2.0
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"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}
run.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def export_data(text):
4
+ print("Exporting data:", text)
5
+ return "Data exported to server!"
6
+
7
+ def refresh_data():
8
+ import random
9
+ return f"Refreshed content: {random.randint(1000, 9999)}"
10
+
11
+ with gr.Blocks() as demo:
12
+ gr.Markdown("""
13
+ # Textbox with Custom Buttons Demo
14
+
15
+ This demo showcases custom buttons in a Textbox component that can trigger either (or both):
16
+ - **Python functions**
17
+ - **JS functions** (with and without input parameters)
18
+
19
+ You can use emojis, text, or icons for the buttons.
20
+ """)
21
+
22
+ gr.Markdown("### Textbox with Custom Buttons")
23
+ refresh_btn = gr.Button("Refresh")
24
+ alert_btn = gr.Button("⚠️ Alert")
25
+ clear_btn = gr.Button("🗑️")
26
+
27
+ textbox = gr.Textbox(
28
+ value="Sample text content that can be exported, refreshed, or transformed.",
29
+ buttons=["copy", refresh_btn, alert_btn, clear_btn],
30
+ label="Sample Text",
31
+ lines=5
32
+ )
33
+
34
+ output = gr.Textbox(label="Output (Python Function Result)")
35
+
36
+
37
+ refresh_btn.click(refresh_data, outputs=textbox)
38
+
39
+ alert_btn.click(
40
+ None,
41
+ inputs=textbox,
42
+ outputs=[],
43
+ js="(text) => { alert('This is a JavaScript alert!\\n\\nTextbox content: ' + text); return []; }"
44
+ )
45
+
46
+
47
+ clear_btn.click(
48
+ None,
49
+ inputs=[],
50
+ outputs=textbox,
51
+ js="() => ''"
52
+ )
53
+
54
+ if __name__ == "__main__":
55
+ demo.launch()
56
+