Skip to content

Commit e6d3421

Browse files
committed
2 parents 302a797 + 4ed49ec commit e6d3421

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

README.md

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ The order in which `html2dash` searches for tags is:
107107
1. `dash.html`
108108
2. `dash.dcc`
109109

110-
You can add additional component libraries to the module list as follows.
110+
You can change the list of modules that `html2dash` searches for tags by
111+
passing in `module_list` argument.
111112

112113
```python
113-
from html2dash import html2dash, settings
114+
from dash import Dash, html, dcc
115+
from html2dash import html2dash
114116
import dash_mantine_components as dmc
115117

116-
# settings["modules"] is a list of modules to search for tags.
117-
# Default value is [html, dcc]
118-
settings["modules"].append(dmc)
118+
modules = [html, dcc, dmc]
119119

120120
layout = html2dash("""
121121
<div>
@@ -126,27 +126,58 @@ layout = html2dash("""
126126
<Badge variant="outline">Outline</Badge>
127127
</div>
128128
</div>
129-
""")
129+
""", module_list=modules)
130130
```
131131

132132
You can also map html tags to dash components. For example, if you dont want to
133133
use `<icon>` tag, you can map it to `DashIconify` as follows.
134134

135135
```python
136-
from html2dash import html2dash, settings
136+
from html2dash import html2dash
137137
from dash_iconify import DashIconify
138138

139-
settings["element-map"]["icon"] = DashIconify
139+
element_map = {"icon": DashIconify}
140140

141141
layout = html2dash("""
142142
<div>
143143
<h1>Icon example</h1>
144144
<icon icon="mdi:home"/>
145145
</div>
146-
""")
146+
""", element_map=element_map)
147147
```
148148

149-
## Display a pandas dataframe in dash
149+
The `element_map` is a dictionary that maps html tags to dash components.
150+
The `element_map` will be searched first before searching in the `module_list`.
151+
152+
The mapped component does not have to be a dash component. It can be any
153+
function that takes `children` and `**kwargs` as arguments and returns a dash
154+
component.
155+
156+
```python
157+
from html2dash import html2dash
158+
159+
def my_component(children, **kwargs):
160+
return html.Div(children=children, **kwargs)
161+
162+
element_map = {"my-component": my_component}
163+
164+
layout = html2dash("""
165+
<div>
166+
<h1>My component</h1>
167+
<my-component id="my-component">
168+
<h2>My component</h2>
169+
<p>This is my component</p>
170+
</my-component>
171+
172+
<my-component id="my-component-2">
173+
<h2>My component 2</h2>
174+
<p>This is my component 2</p>
175+
</my-component>
176+
</div>
177+
""", element_map=element_map)
178+
```
179+
180+
## Example usecase: Display a pandas dataframe in dash
150181

151182
Since pandas dataframes come with a `to_html` method, you can easily display
152183
them in dash using `html2dash`.
@@ -164,15 +195,15 @@ do the following.
164195

165196
```python
166197
import pandas as pd
167-
from html2dash import html2dash, settings
198+
from html2dash import html2dash
168199
import dash_mantine_components as dmc
169200

170201
# <table> would have been mapped to dash.html.Table
171202
# But, we want to use dmc.Table instead.
172-
settings["element-map"]["table"] = dmc.Table
203+
element_map = {"table": dmc.Table}
173204

174205
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
175-
layout = html2dash(df.to_html())
206+
layout = html2dash(df.to_html(), element_map=element_map)
176207
```
177208

178209
`html2dash` can handle multi-index dataframes as well.
@@ -182,16 +213,17 @@ import pandas as pd
182213
from html2dash import html2dash, settings
183214
import dash_mantine_components as dmc
184215

185-
settings["element-map"]["table"] = dmc.Table
186-
187216
df = pd.DataFrame(
188217
{
189218
("a", "b"): [1, 2, 3],
190219
("a", "c"): [4, 5, 6],
191220
("d", "e"): [7, 8, 9],
192221
}
193222
)
194-
layout = html2dash(df.to_html())
223+
224+
element_map = {"table": dmc.Table}
225+
226+
layout = html2dash(df.to_html(), element_map=element_map)
195227
```
196228

197229
## Case sensitivity of html tags

0 commit comments

Comments
 (0)