Add component: Star Rating
#1
by
freddyaboulton HF Staff - opened
- components/star-rating.json +20 -0
components/star-rating.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"id": "star-rating",
|
| 3 |
+
"name": "Star Rating",
|
| 4 |
+
"description": "Click stars to rate from 1 to 5",
|
| 5 |
+
"author": "gradio",
|
| 6 |
+
"tags": [
|
| 7 |
+
"input",
|
| 8 |
+
"rating"
|
| 9 |
+
],
|
| 10 |
+
"category": "input",
|
| 11 |
+
"html_template": "\n <h2>${label} rating:</h2>\n ${Array.from({length: 5}, (_, i) => `<img class='${i < value ? '' : 'faded'}' src='https://upload.wikimedia.org/wikipedia/commons/d/df/Award-star-gold-3d.svg'>`).join('')}\n ",
|
| 12 |
+
"css_template": "\n img { height: 50px; display: inline-block; cursor: pointer; }\n .faded { filter: grayscale(100%); opacity: 0.3; }\n ",
|
| 13 |
+
"js_on_load": "\n const imgs = element.querySelectorAll('img');\n imgs.forEach((img, index) => {\n img.addEventListener('click', () => {\n props.value = index + 1;\n });\n });\n ",
|
| 14 |
+
"default_props": {
|
| 15 |
+
"value": 3,
|
| 16 |
+
"label": "Food"
|
| 17 |
+
},
|
| 18 |
+
"python_code": "class StarRating(gr.HTML):\n def __init__(self, label, value=0, **kwargs):\n html_template = \"\"\"\n <h2>${label} rating:</h2>\n ${Array.from({length: 5}, (_, i) => `<img class='${i < value ? '' : 'faded'}' src='https://upload.wikimedia.org/wikipedia/commons/d/df/Award-star-gold-3d.svg'>`).join('')}\n \"\"\"\n css_template = \"\"\"\n img { height: 50px; display: inline-block; cursor: pointer; }\n .faded { filter: grayscale(100%); opacity: 0.3; }\n \"\"\"\n js_on_load = \"\"\"\n const imgs = element.querySelectorAll('img');\n imgs.forEach((img, index) => {\n img.addEventListener('click', () => {\n props.value = index + 1;\n });\n });\n \"\"\"\n super().__init__(\n value=value, label=label,\n html_template=html_template,\n css_template=css_template,\n js_on_load=js_on_load, **kwargs\n )\n\n def api_info(self):\n return {\"type\": \"integer\", \"minimum\": 0, \"maximum\": 5}\n",
|
| 19 |
+
"head": ""
|
| 20 |
+
}
|