freddyaboulton commited on
Commit
93df9b0
·
verified ·
1 Parent(s): 3b6b8e3

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 +152 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Super Html
3
- emoji: 🐢
4
- colorFrom: blue
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.49.1
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: super_html
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 6.0.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: super_html"]}, {"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", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"\"\"\n", " # Simple HTML usecase\n", " This is the classic `gr.HTML` usecase where we just want to render some static HTML.\n", " \"\"\")\n", " simple_html = gr.HTML(\"<h1 style='color:purple;' id='simple'>Hello, World!</h1>\")\n", "\n", " gr.Markdown(\"\"\"\n", " # Templated HTML usecase\n", " 'value' can now be anything, and it can be used inside the `html_template` using `${value}` syntax.\n", " Note that when used as output or input, `value` is just this specific value rather than the entire HTML.\n", " \"\"\")\n", " with gr.Row():\n", " name1 = gr.Textbox(label=\"Name\")\n", " templated_html = gr.HTML(\"\", html_template=\"<h1>Hello, {{value}}! ${value.length} letters</h1>\", elem_id=\"templated\")\n", " name1.change(lambda x: x, inputs=name1, outputs=templated_html)\n", "\n", " gr.Markdown(\"\"\"\n", " # Additional Props\n", " You are not limited to using `${value}` in the templates, you can add any number of custom tags to the template, and pass them to the component as keyword arguments. These props can be updated via python event listeners as well.\n", " \"\"\")\n", " with gr.Row():\n", " templated_html_props = gr.HTML(\"John\", html_template=\"\"\"\n", " <h1 style=\"font-size: ${fontSize}px;\">Hello, ${value}!</h1>\n", " \"\"\", fontSize=30, elem_id=\"props\")\n", " slider = gr.Slider(10, 100, value=30, label=\"Font Size\")\n", " slider.change(lambda x: gr.HTML(fontSize=x), inputs=slider, outputs=templated_html_props)\n", "\n", " gr.Markdown(\"\"\"\n", " # CSS Templating\n", " We can also template CSS, which is automatically scoped to the component.\n", " \"\"\")\n", " with gr.Row():\n", " name2 = gr.Textbox(label=\"Person\")\n", " color = gr.ColorPicker(label=\"Text Color\", value=\"#00ff00\")\n", " bold = gr.Checkbox(label=\"Bold Text\", value=True)\n", " templated_html_css = gr.HTML([\"J\", \"o\", \"h\", \"n\"], html_template=\"\"\"\n", " <h1>Hello, ${value.join('')}!</h1>\n", " <ul>\n", " {{#each value}}\n", " <li>{{this}}</li>\n", " {{/each}}\n", " </ul>\n", " \"\"\", css_template=\"\"\"\n", " h1, li {\n", " color: ${color};\n", " font-weight: ${bold ? 'bold' : 'normal'};\n", " }\n", " \"\"\", color=\"green\", bold=True, elem_id=\"css\")\n", " with gr.Row():\n", " btn = gr.Button(\"Update HTML\")\n", " btn_blue = gr.Button(\"Make HTML Blue\")\n", " def update_templated_html_css(name, color, bold):\n", " return gr.HTML(value=list(name), color=color, bold=bold)\n", " btn.click(update_templated_html_css, inputs=[name2, color, bold], outputs=templated_html_css)\n", " btn_blue.click(lambda: gr.HTML(color=\"blue\"), outputs=templated_html_css)\n", "\n", " gr.Markdown(\"\"\"\n", " # JS Prop Updates\n", " We can now trigger events from gr.HTML using event listeners in `js_on_load`. This script has access to `element` which refers to the parent element, and `trigger(event_name)` or `trigger(event_name, event_data)`, which can be used to dispatch events.\n", " \"\"\")\n", "\n", " button_set = gr.HTML(\n", " html_template=\"\"\"\n", " <button id='A'>A</button>\n", " <button id='B'>B</button>\n", " <button id='C'>C</button>\n", " \"\"\",\n", " css_template=\"\"\"\n", " button {\n", " padding: 10px;\n", " background-color: red;\n", " }\n", " \"\"\",\n", " js_on_load=\"\"\"\n", " const buttons = element.querySelectorAll('button');\n", " buttons.forEach(button => {\n", " button.addEventListener('click', () => {\n", " trigger('click', {clicked: button.innerText});\n", " });\n", " });\n", " \"\"\",\n", " elem_id=\"button_set\"\n", " )\n", " clicked_box = gr.Textbox(label=\"Clicked\")\n", "\n", " def on_button_click(evt: gr.EventData):\n", " return evt.clicked\n", " \n", " button_set.click(on_button_click, outputs=clicked_box)\n", "\n", " gr.Markdown(\"\"\"\n", " # JS Prop Changes\n", " You can also update `value` or any other prop of the component from JS using `props`, e.g., `props.value = \"new value\"` will update the `value` prop and re-render the HTML template.\n", " \"\"\") \n", " form = gr.HTML(\n", " html_template=\"\"\"\n", " <input type=\"text\" value=\"${value}\" id=\"text-input\" />\n", " <p>${value.length} letters</p>\n", " <button class=\"submit\" style=\"display: ${valid ? 'block' : 'none'};\">submit</button>\n", " <button class=\"clear\">clear</button>\n", " \"\"\",\n", " js_on_load=\"\"\"\n", " const input = element.querySelector('input');\n", " const submit_button = element.querySelector('button.submit');\n", " const clear_button = element.querySelector('button.clear');\n", " input.addEventListener('input', () => {\n", " props.valid = input.value.length > 5;\n", " props.value = input.value;\n", " });\n", " submit_button.addEventListener('click', () => {\n", " trigger('submit');\n", " });\n", " clear_button.addEventListener('click', () => {\n", " props.value = \"\";\n", " props.valid = false;\n", " trigger('clear');\n", " });\n", " \"\"\",\n", " valid=False, elem_id=\"form\")\n", " output_box = gr.Textbox(label=\"Output Box\")\n", " form.submit(lambda x: x, form, outputs=output_box)\n", " output_box.submit(lambda x: x, output_box, outputs=form)\n", "\n", " gr.Markdown(\"\"\"\n", " # Extending gr.HTML for new Components\n", " You can create your own Components by extending the gr.HTML class.\n", " \"\"\")\n", " class ListComponent(gr.HTML):\n", " def __init__(self, container=True, label=\"List\", **kwargs):\n", " super().__init__(\n", " html_template=\"\"\"\n", " <h2>${label}</h2>\n", " <ul>\n", " ${value.map(item => `<li>${item}</li>`).join('')}\n", " </ul>\n", " \"\"\",\n", " container=container,\n", " label=label,\n", " **kwargs\n", " )\n", "\n", " ListComponent(label=\"Fruits\", value=[\"Apple\", \"Banana\", \"Cherry\"], elem_id=\"fruits\")\n", " ListComponent(label=\"Vegetables\", value=[\"Carrot\", \"Broccoli\", \"Spinach\"], elem_id=\"vegetables\")\n", "\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ with gr.Blocks() as demo:
5
+ gr.Markdown("""
6
+ # Simple HTML usecase
7
+ This is the classic `gr.HTML` usecase where we just want to render some static HTML.
8
+ """)
9
+ simple_html = gr.HTML("<h1 style='color:purple;' id='simple'>Hello, World!</h1>")
10
+
11
+ gr.Markdown("""
12
+ # Templated HTML usecase
13
+ 'value' can now be anything, and it can be used inside the `html_template` using `${value}` syntax.
14
+ Note that when used as output or input, `value` is just this specific value rather than the entire HTML.
15
+ """)
16
+ with gr.Row():
17
+ name1 = gr.Textbox(label="Name")
18
+ templated_html = gr.HTML("", html_template="<h1>Hello, {{value}}! ${value.length} letters</h1>", elem_id="templated")
19
+ name1.change(lambda x: x, inputs=name1, outputs=templated_html)
20
+
21
+ gr.Markdown("""
22
+ # Additional Props
23
+ You are not limited to using `${value}` in the templates, you can add any number of custom tags to the template, and pass them to the component as keyword arguments. These props can be updated via python event listeners as well.
24
+ """)
25
+ with gr.Row():
26
+ templated_html_props = gr.HTML("John", html_template="""
27
+ <h1 style="font-size: ${fontSize}px;">Hello, ${value}!</h1>
28
+ """, fontSize=30, elem_id="props")
29
+ slider = gr.Slider(10, 100, value=30, label="Font Size")
30
+ slider.change(lambda x: gr.HTML(fontSize=x), inputs=slider, outputs=templated_html_props)
31
+
32
+ gr.Markdown("""
33
+ # CSS Templating
34
+ We can also template CSS, which is automatically scoped to the component.
35
+ """)
36
+ with gr.Row():
37
+ name2 = gr.Textbox(label="Person")
38
+ color = gr.ColorPicker(label="Text Color", value="#00ff00")
39
+ bold = gr.Checkbox(label="Bold Text", value=True)
40
+ templated_html_css = gr.HTML(["J", "o", "h", "n"], html_template="""
41
+ <h1>Hello, ${value.join('')}!</h1>
42
+ <ul>
43
+ {{#each value}}
44
+ <li>{{this}}</li>
45
+ {{/each}}
46
+ </ul>
47
+ """, css_template="""
48
+ h1, li {
49
+ color: ${color};
50
+ font-weight: ${bold ? 'bold' : 'normal'};
51
+ }
52
+ """, color="green", bold=True, elem_id="css")
53
+ with gr.Row():
54
+ btn = gr.Button("Update HTML")
55
+ btn_blue = gr.Button("Make HTML Blue")
56
+ def update_templated_html_css(name, color, bold):
57
+ return gr.HTML(value=list(name), color=color, bold=bold)
58
+ btn.click(update_templated_html_css, inputs=[name2, color, bold], outputs=templated_html_css)
59
+ btn_blue.click(lambda: gr.HTML(color="blue"), outputs=templated_html_css)
60
+
61
+ gr.Markdown("""
62
+ # JS Prop Updates
63
+ We can now trigger events from gr.HTML using event listeners in `js_on_load`. This script has access to `element` which refers to the parent element, and `trigger(event_name)` or `trigger(event_name, event_data)`, which can be used to dispatch events.
64
+ """)
65
+
66
+ button_set = gr.HTML(
67
+ html_template="""
68
+ <button id='A'>A</button>
69
+ <button id='B'>B</button>
70
+ <button id='C'>C</button>
71
+ """,
72
+ css_template="""
73
+ button {
74
+ padding: 10px;
75
+ background-color: red;
76
+ }
77
+ """,
78
+ js_on_load="""
79
+ const buttons = element.querySelectorAll('button');
80
+ buttons.forEach(button => {
81
+ button.addEventListener('click', () => {
82
+ trigger('click', {clicked: button.innerText});
83
+ });
84
+ });
85
+ """,
86
+ elem_id="button_set"
87
+ )
88
+ clicked_box = gr.Textbox(label="Clicked")
89
+
90
+ def on_button_click(evt: gr.EventData):
91
+ return evt.clicked
92
+
93
+ button_set.click(on_button_click, outputs=clicked_box)
94
+
95
+ gr.Markdown("""
96
+ # JS Prop Changes
97
+ You can also update `value` or any other prop of the component from JS using `props`, e.g., `props.value = "new value"` will update the `value` prop and re-render the HTML template.
98
+ """)
99
+ form = gr.HTML(
100
+ html_template="""
101
+ <input type="text" value="${value}" id="text-input" />
102
+ <p>${value.length} letters</p>
103
+ <button class="submit" style="display: ${valid ? 'block' : 'none'};">submit</button>
104
+ <button class="clear">clear</button>
105
+ """,
106
+ js_on_load="""
107
+ const input = element.querySelector('input');
108
+ const submit_button = element.querySelector('button.submit');
109
+ const clear_button = element.querySelector('button.clear');
110
+ input.addEventListener('input', () => {
111
+ props.valid = input.value.length > 5;
112
+ props.value = input.value;
113
+ });
114
+ submit_button.addEventListener('click', () => {
115
+ trigger('submit');
116
+ });
117
+ clear_button.addEventListener('click', () => {
118
+ props.value = "";
119
+ props.valid = false;
120
+ trigger('clear');
121
+ });
122
+ """,
123
+ valid=False, elem_id="form")
124
+ output_box = gr.Textbox(label="Output Box")
125
+ form.submit(lambda x: x, form, outputs=output_box)
126
+ output_box.submit(lambda x: x, output_box, outputs=form)
127
+
128
+ gr.Markdown("""
129
+ # Extending gr.HTML for new Components
130
+ You can create your own Components by extending the gr.HTML class.
131
+ """)
132
+ class ListComponent(gr.HTML):
133
+ def __init__(self, container=True, label="List", **kwargs):
134
+ super().__init__(
135
+ html_template="""
136
+ <h2>${label}</h2>
137
+ <ul>
138
+ ${value.map(item => `<li>${item}</li>`).join('')}
139
+ </ul>
140
+ """,
141
+ container=container,
142
+ label=label,
143
+ **kwargs
144
+ )
145
+
146
+ ListComponent(label="Fruits", value=["Apple", "Banana", "Cherry"], elem_id="fruits")
147
+ ListComponent(label="Vegetables", value=["Carrot", "Broccoli", "Spinach"], elem_id="vegetables")
148
+
149
+
150
+
151
+ if __name__ == "__main__":
152
+ demo.launch()