# Convert html to dash\_html\_components

**URL:** <https://community.plotly.com/t/convert-html-to-dash-html-components/47328>\
**Category:** Dash Python\
**Tags:** show-and-tell\
**Created:** [November 19, 2020, 2:24pm UTC](https://community.plotly.com/t/convert-html-to-dash-html-components/47328 "2020-11-19T14:24:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdementen](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@sdementen](https://community.plotly.com/u/sdementen)\
**Post date:** [November 19, 2020, 2:24pm UTC](https://community.plotly.com/t/convert-html-to-dash-html-components/47328/1 "2020-11-19T14:24:18Z")

</div>

Here is a simple function to convert an HTML string to Dash dash\_html\_components components.  
Can be useful to convert existing pages/layout to Dash.

```auto
def convert_html_to_dash(html_code):
    """Convert standard html to Dash components"""
    from xml.etree import ElementTree
    import dash_html_components as html

    def parse_css(css):
        """Convert a style in ccs format to dictionary accepted by Dash"""
        return {k: v for style in css.strip(";").split(";") for k, v in [style.split(":")]}

    def _convert(elem):
        comp = getattr(html, elem.tag.capitalize())
        children = [_convert(child) for child in elem]
        if not children:
            children = elem.text
        attribs = elem.attrib.copy()
        if "class" in attribs:
            attribs["className"] = attribs.pop("class")
        attribs = {k: (parse_css(v) if k == "style" else v) for k, v in attribs.items()}

        return comp(children=children, **attribs)

    et = ElementTree.fromstring(html_code)

    return _convert(et)

```

---

<div class="post-metadata">

**Author:** ![luquezsantiago](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/luquezsantiago/32/13100_2.png) [@luquezsantiago](https://community.plotly.com/u/luquezsantiago)\
**Post date:** [November 19, 2020, 6:35pm UTC](https://community.plotly.com/t/convert-html-to-dash-html-components/47328/2 "2020-11-19T18:35:32Z")

</div>

looks good, I will give it a try! thanks

---

<div class="post-metadata">

**Author:** ![atharvakatre](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/atharvakatre/32/14713_2.png) [@atharvakatre](https://community.plotly.com/u/atharvakatre)\
**Post date:** [January 12, 2021, 7:16pm UTC](https://community.plotly.com/t/convert-html-to-dash-html-components/47328/3 "2021-01-12T19:16:05Z")

</div>

Thanks for this, it would surely come in handy.

---

<div class="post-metadata">

**Author:** ![sdementen](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@sdementen](https://community.plotly.com/u/sdementen)\
**Post date:** [January 12, 2021, 7:54pm UTC](https://community.plotly.com/t/convert-html-to-dash-html-components/47328/4 "2021-01-12T19:54:08Z")

</div>

An update supporting:

- components from dash\_bootstrap\_components
- attributes that are python literals (int, float, dicts, lists, …)

The latest fiture allows you do the following to define a slider with marks (notice the marks attribute that is a string version of python dict):

```python
my_value = 3
my_marks = {0: "min", 3:"3", 5:"max"}
html = f'<slider min="0" max="5" step="1" value="{my_value}" marks="{my_marks}" class="col-span-4"></slider>'
slider = convert_html_to_dash(html)

```

Updated version:

```python
def convert_html_to_dash(html_code, dash_modules=None):
    """Convert standard html (as string) to Dash components.

    Looks into the list of dash_modules to find the right component (default to [html, dcc, dbc])."""
    from xml.etree import ElementTree

    if dash_modules is None:
        import dash_html_components as html
        import dash_core_components as dcc

        dash_modules = [html, dcc]
        try:
            import dash_bootstrap_components as dbc

            dash_modules.append(dbc)
        except ImportError:
            pass

    def find_component(name):
        for module in dash_modules:
            try:
                return getattr(module, name)
            except AttributeError:
                pass
        raise AttributeError(f"Could not find a dash widget for '{name}'")

    def parse_css(css):
        """Convert a style in ccs format to dictionary accepted by Dash"""
        return {k: v for style in css.strip(";").split(";") for k, v in [style.split(":")]}

    def parse_value(v):
        try:
            return ast.literal_eval(v)
        except (SyntaxError, ValueError):
            return v

    parsers = {"style": parse_css, "id": lambda x: x}

    def _convert(elem):
        comp = find_component(elem.tag.capitalize())
        children = [_convert(child) for child in elem]
        if not children:
            children = elem.text
        attribs = elem.attrib.copy()
        if "class" in attribs:
            attribs["className"] = attribs.pop("class")
        attribs = {k: parsers.get(k, parse_value)(v) for k, v in attribs.items()}

        return comp(children=children, **attribs)

    et = ElementTree.fromstring(html_code)

    return _convert(et)

```
