| { | |
| "id": "star-rating", | |
| "name": "Star Rating", | |
| "description": "Click stars to set a rating from 1 to 5", | |
| "author": "gradio", | |
| "tags": [ | |
| "input", | |
| "rating", | |
| "stars" | |
| ], | |
| "category": "input", | |
| "html_template": "<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('')}", | |
| "css_template": "img { height: 50px; display: inline-block; cursor: pointer; }\n.faded { filter: grayscale(100%); opacity: 0.3; }", | |
| "js_on_load": "const imgs = element.querySelectorAll('img');\nimgs.forEach((img, index) => {\n img.addEventListener('click', () => {\n props.value = index + 1;\n });\n});", | |
| "default_props": { | |
| "value": 3, | |
| "label": "Food" | |
| }, | |
| "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}" | |
| } |