@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”
propTypethat denotes that this component supportsdata-*properties. This could either be a special name, likedata-*
Div.propTypes = {
'data-*': PropTypes.string,
...
}
or perhaps a special propType that signifies that this is a wildcard attribute, like
Div.propTypes = {
'data-*': CustomPropTypes.wildcard
...
}
- Update the HTML component generator with this property: https://github.com/plotly/dash-html-components/blob/6d04a70f098bb8640545467255ae382e8e17cffb/scripts/generate-components.js#L43-L55
- Regenerate the HTML components: with
$ npm run prepublishhttps://github.com/plotly/dash-html-components/blob/6d04a70f098bb8640545467255ae382e8e17cffb/package.json#L22 - That propType will then be encoded in the
metadata.jsonfile that describes all of the properties of all of the components. For example, see the properties ofDivhere: https://github.com/plotly/dash-html-components/blob/master/lib/metadata.json#L5465-L5607 - 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 thisself._prop_nameslist (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)) - Then, we’ll need to make sure that
_valid_wildcard_attribute_namesis encoded in the dynamic component generation code (https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L205), something like
whereself._valid_wildcard_attribute_names = {list_of_valid_wildcard_attribute_prefixes}list_of_valid_wildcard_attribute_prefixesis created by filtering themetadata.jsonfile and looking for either the"wildcard"type that was defined above or the special"data-*"property name.
- In the base component which does the validation and serialization: https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L15-L24.
- Add some tests to https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/tests/development/test_base_component.py and https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/tests/development/test_component_loader.py
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:
html.Div(data-label='')
however, I believe we could do
html.Div(**{'data-label': ''})
we could do some _ to - replacement too, like:
html.Div(data_label='')
but would introduce some complexity into the BaseComponent. If we do html.Div(**{'data-label': ''}), then we should probably patch the __repr__ in the BaseComponent: https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L216-L229