Greetings.
In order to convert html to dash format more conveniently, I developed a Python library html-to-dash
. You can use the command pip install html-to-dash
to use it.
Here are some examples:
Basic usage
from html_to_dash import parse_html
element_str = """
<div>
<div class='bg-gray-800' style='color:red;margin:10px'>
<svg aria-label="Ripples. Logo" role="img" xmlns="http://www.w3.org/2000/svg"</svg>
<a href="#" id="link1">A</a>
</div>
<div>text</div>
<div><a href="#" id="link2">B</a></div>
</div>
"""
parse_html(element_str)
Print:
# Tags : Unsupported [svg] removed.
Result:
html.Div(
children=[
html.Div(
className="bg-gray-800",
style={"color": "red", "margin": "10px"},
children=[html.A(href="#", id="link1", children=["A"])],
),
html.Div(children=["text"]),
html.Div(children=[html.A(href="#", id="link2", children=["B"])]),
]
)
Enable dash_svg
Use dash-svg module to render SVG tags.
from html_to_dash import parse_html
element_str = """
<svg xmlns=" http://www.w3.org/2000/svg " version="1.1" width="300" height="300">
<rect x="100" y="100" width="100" height="100" fill="#e74c3c"></rect>
<polygon points="100,100 200,100 150,50" fill="#c0392b"></polygon>
<polygon points="200,100 200,200 250,150" fill="#f39c12"></polygon>
<polygon points="100,100 150,50 150,150 100,200" fill="#f1c40f"></polygon>
<polygon points="150,50 200,100 250,50 200,0" fill="#2ecc71"></polygon>
<polygon points="100,200 150,150 200,200 150,250" fill="#3498db"></polygon>
</svg>
"""
parse_html(element_str, enable_dash_svg=True)
Print:
Result:
dash_svg.Svg(
xmlns=" http://www.w3.org/2000/svg ",
version="1.1",
width="300",
height="300",
children=[
dash_svg.Rect(x="100", y="100", width="100", height="100", fill="#e74c3c"),
dash_svg.Polygon(points="100,100 200,100 150,50", fill="#c0392b"),
dash_svg.Polygon(points="200,100 200,200 250,150", fill="#f39c12"),
dash_svg.Polygon(points="100,100 150,50 150,150 100,200", fill="#f1c40f"),
dash_svg.Polygon(points="150,50 200,100 250,50 200,0", fill="#2ecc71"),
dash_svg.Polygon(points="100,200 150,150 200,200 150,250", fill="#3498db"),
],
)
In addition, it also supports advanced functions such as expanding modules and removing tags. More content can be found in the github repository:GitHub - PierXuY/html-to-dash: Convert HTML to dash format.
I hope it can be helpful to you.