Skip to content

Commit 68526ea

Browse files
authored
Support html siblings to the map (#83)
1 parent 390f8c4 commit 68526ea

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setuptools.setup(
44
name="streamlit_folium",
5-
version="0.6.14",
5+
version="0.6.15",
66
author="Randy Zwitch",
77
author_email="[email protected]",
88
description="Render Folium objects in Streamlit",

streamlit_folium/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ def st_folium(
114114

115115
leaflet = generate_leaflet_string(fig)
116116

117+
children = list(fig.get_root()._children.values())
118+
119+
html = ""
120+
if len(children) > 1:
121+
for child in children[1:]:
122+
try:
123+
html += child._template.module.html() + "\n"
124+
except Exception:
125+
pass
126+
117127
# Replace the folium generated map_{random characters} variables
118128
# with map_div and map_div2 (these end up being both the assumed)
119129
# div id where the maps are inserted into the DOM, and the names of
@@ -152,7 +162,8 @@ def bounds_to_dict(bounds_list: List[List[float]]) -> Dict[str, Dict[str, float]
152162
bounds = [[None, None], [None, None]]
153163

154164
component_value = _component_func(
155-
fig=leaflet,
165+
script=leaflet,
166+
html=html,
156167
id=m_id,
157168
key=generate_js_hash(leaflet, key),
158169
height=height,

streamlit_folium/frontend/src/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ function onRender(event: Event): void {
104104
// Get the RenderData from the event
105105
const data = (event as CustomEvent<RenderData>).detail
106106

107-
const fig: string = data.args["fig"]
107+
const script: string = data.args["script"]
108108
const height: number = data.args["height"]
109109
const width: number = data.args["width"]
110+
const html: string = data.args["html"]
110111

111112
if (!window.map) {
112113
// Only run this if the map hasn't already been created (and thus the global
@@ -121,7 +122,7 @@ function onRender(event: Event): void {
121122
div1.style.height = `${height}px`
122123
div1.style.width = `${width}px`
123124

124-
if (fig.indexOf("document.getElementById('export')") !== -1) {
125+
if (script.indexOf("document.getElementById('export')") !== -1) {
125126
let a = document.createElement("a")
126127
a.href = "#"
127128
a.id = "export"
@@ -146,8 +147,11 @@ function onRender(event: Event): void {
146147
// The folium-generated script creates a variable called "map_div", which
147148
// is the actual Leaflet map.
148149
render_script.innerHTML =
149-
fig + `window.map = map_div; window.initComponent(map_div);`
150+
script + `window.map = map_div; window.initComponent(map_div);`
150151
document.body.appendChild(render_script)
152+
const html_div = document.createElement("div")
153+
html_div.innerHTML = html
154+
document.body.appendChild(html_div)
151155
}
152156
}
153157
}

0 commit comments

Comments
 (0)