I would like to try out and test if I can create custom layouts for the Fast Templates of Panel. The Fast Templates already includes the js
and css
dependencies required for using the Fast Components. This includes the Fast Accordion.
I’m able to create the below but have several problems
- How do I use the
name
of theobj
in the loop as the heading of each accordion item? - How to I get the
component1
andvgl_pane
to show it self correctly? I.e. show the background colors and anything at all for the vgl_pane. - What would a reference implementation look like? Probably the FastAccordion should also inherit from
pn.pane.ListLike
similarly topn.Column
.
import panel as pn
import param
pn.extension("vega", sizing_mode="stretch_width")
SVG_OPEN = """<svg style="stroke: #e62f63;" width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg" slot="collapsed-icon">
<path d="M15.2222 1H2.77778C1.79594 1 1 1.79594 1 2.77778V15.2222C1 16.2041 1.79594 17 2.77778 17H15.2222C16.2041 17 17 16.2041 17 15.2222V2.77778C17 1.79594 16.2041 1 15.2222 1Z" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M9 5.44446V12.5556" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M5.44446 9H12.5556" stroke-linecap="round" stroke-linejoin="round"></path></svg>"""
SVG_CLOSED = """<svg style="stroke: #e62f63;" width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg" slot="expanded-icon">
<path d="M15.2222 1H2.77778C1.79594 1 1 1.79594 1 2.77778V15.2222C1 16.2041 1.79594 17 2.77778 17H15.2222C16.2041 17 17 16.2041 17 15.2222V2.77778C17 1.79594 16.2041 1 15.2222 1Z" stroke-linecap="round" stroke-linejoin="round"></path>
<path d="M5.44446 9H12.5556" stroke-linecap="round" stroke-linejoin="round"></path></svg>"""
# Should probably inherit from pn.pane.ListLike also similarly to pn.Column
class FastAccordion(pn.reactive.ReactiveHTML):
icon_open = param.String(SVG_OPEN)
icon_closed = param.String(SVG_CLOSED)
objects = param.List()
_template = html ="""
<fast-accordion>
{% for obj in objects %}
<fast-accordion-item slot="item">
<option id="option">${obj}</option>
<div slot="heading">obj.name</div>
{{icon_open}}{{icon_closed}}</fast-accordion-item>
{% endfor %}
</fast-accordion>
"""
def __init__(self, *objects, **params):
params["objects"]=[pn.panel(obj) for obj in objects]
super().__init__(**params)
component1 = pn.Column("a", pn.Spacer(background="blue"),
background="yellow", name="Component 1")
vegalite = {
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {"url": "https://raw.githubusercontent.com/vega/vega/master/docs/data/barley.json"},
"mark": "bar",
"encoding": {
"x": {"aggregate": "sum", "field": "yield", "type": "quantitative"},
"y": {"field": "variety", "type": "nominal"},
"color": {"field": "site", "type": "nominal"},
},
"width": 'container',
}
vgl_pane = pn.panel(vegalite, height=240)
accordion = FastAccordion(component1, vgl_pane, "d")
pn.template.FastListTemplate(
title="Awesome Panel",
main = [accordion, vgl_pane],
main_layout="",
).servable()