Open ahuang11 opened 1 year ago
As a work around, use param.depends
to rebuild the HTML:
from panel.reactive import ReactiveHTML
import param
import panel as pn
pn.extension()
class Test(ReactiveHTML):
value = param.List(doc="The selected reactions.")
options = param.Dict(
default={"favorite": "heart"},
doc="""
A key-value pair of reaction values and their corresponding tabler icon names
found on https://tabler-icons.io.""",
)
_icons_html = param.String()
_template = """
<div id="reactions" class="reactions">
${_icons_html}
</div>
"""
@param.depends("options", watch=True, on_init=True)
def _update_icons_html(self):
self._icons_html = "\n".join(
f"""
<img id="reaction-{reaction}"
src="https://tabler-icons.io/static/tabler-icons/icons/{icon_name}.svg"
alt="{reaction}">
</img>
"""
for reaction, icon_name in self.options.items()
)
test = Test()
test
However, the workaround cannot attach scripts~
Looping through a dict--this works with a div
<div id="v-{{ loop.index0 }}">${icon_name}</div>
:But not
<img id="reaction-{{ loop.index0 }}" src="https://tabler-icons.io/icons/{{ icon_name }}.svg" alt="${reaction}"></img>
Traceback: