I would like to have data-* attributes on the elements I create in Dash. (I plan on using BootstrapJS along with Dash, and data-* attributes are preferred to writing more javascript… So I’m not blocked, but I’d like to be sure I’m doing things the best way.)
@mccalluc - This isn’t support right now, but we should support it.
The “issue” is that Dash components are currently generated from a fixed set of properties, the component generation code doesn’t have any support for arbitrary patterns of properties.
To support this, we need to:
Introduce a “special” propType that denotes that this component supports data-* properties. This could either be a special name, like data-*
Then, we’ll need to update the Dash component generation code to recognize the special attribute name / special attribute type. This will happen in two places:
In the base component which does the validation and serialization: https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L15-L24.
Validation happens by checking the supplied properties against the valid properties of that component. The list of valid properties are encoded in this self._prop_names list (for example, it might be like ['id', 'children', 'style', 'className', 'src', ...]). data-* properties don’t match that pattern, so we’ll have to have some exception to the logic that’s like:
if (k not in self._props_names and
all([k.startsWith(wildcard_attr)
for wildcard_attr in self._valid_wildcard_attribute_names])):
raise Exception('Unexpected attribute `{}`...'.format(k))
where list_of_valid_wildcard_attribute_prefixes is created by filtering the metadata.json file and looking for either the "wildcard" type that was defined above or the special "data-*" property name.
Another thing to consider is how we should deal with the fact that these properties have a hyphen in them. Since they are hyphenated, we can’t do assignments like:
Thanks! For now, I think I’ll inject the JS I need, but down the road I may need to create new components, and this step-by-step description will be useful.
Is there any conceptual reason why Dash html components should not take arbitrary attributes? Obviously there is the work involved in implementing it, but I’m wondering if there is any objection from a design point of view.
I’m imagining something along the lines of, for example,
This is a common pattern in a lot of libraries - a set of recognised arguments, but also the ability to pass on key:value pairs of arbitrary extra arguments, which provides flexibility and future-proofing. Conceptually speaking, HTML takes arbitrary attributes, so it seems to make sense to be able to pass these in. Having a general property dict doesn’t preclude handling specific property patterns like data-* explicitly as well, as and when these arguments are added.
Would this be harder to do than specific properties? Is there some conceptual objection to it?
There are no updates. I have been working on other features and no one from the community has taken a stab at what I outlined above. If you need this prioritized, then your company or organization can sponsor the feature: Consulting, Training & Open-Source Development
You can also solve this (in a slightly hacky way) using the dash_dangerously_set_inner_html package which lets you create components with arbitrary attributes (https://github.com/plotly/dash-dangerously-set-inner-html) an example below as a line in the layout to allow the answer to be a bootstrap collapsable
Looks like this feature is available. Can anyone point me to tutorial to use it. Note I have no experience in Javascript and UI development and hence I am not able to know what this feature is and how it is useful.
I am new to dash and have checked most of the dash forums for using the data-attributes. But could not find any suitable example which explains as to how it can be implemented. Could you please let know if there is any example which can guide me in implementing it.
Also tried using custom scripts but that also doesn’t work
Used this link for reference