Skip to content

Commit 6f3a54a

Browse files
committed
WIP: move event handlers to client code
First step, move (some of) the event handlers to python code.
1 parent d56d9a5 commit 6f3a54a

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

streamlit_folium/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# Create a _RELEASE constant. We'll set this to False while we're developing
2020
# the component, and True when we're ready to package and distribute it.
21-
_RELEASE = True
21+
_RELEASE = False
2222

2323

2424
if not _RELEASE:
@@ -297,6 +297,21 @@ def st_folium(
297297

298298
folium_map.render()
299299

300+
folium_map.on(
301+
click=folium.JsCode("""
302+
function onMapClick(e) {
303+
const global_data = window.__GLOBAL_DATA__
304+
global_data.lat_lng_clicked = e.latlng
305+
debouncedUpdateComponentValue(window.map)
306+
}
307+
"""),
308+
move=folium.JsCode("""
309+
function onMapMove(e) {
310+
debouncedUpdateComponentValue(window.map)
311+
}
312+
"""),
313+
)
314+
300315
# we need to do this before _get_map_string, because
301316
# _get_map_string alters the folium structure
302317
html = _get_html(folium_map)

streamlit_folium/frontend/src/index.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,10 @@ declare global {
4040
feature_group: any
4141
layer_control: any
4242
Streamlit: any
43+
debouncedUpdateComponentValue: any
4344
}
4445
}
4546

46-
function onMapClick(e: any) {
47-
const global_data = window.__GLOBAL_DATA__
48-
global_data.lat_lng_clicked = e.latlng
49-
debouncedUpdateComponentValue(window.map)
50-
}
51-
5247
let debouncedUpdateComponentValue = debounce(updateComponentValue, 250)
5348

5449
function updateComponentValue(map: any) {
@@ -90,10 +85,6 @@ function updateComponentValue(map: any) {
9085
}
9186
}
9287

93-
function onMapMove(e: any) {
94-
debouncedUpdateComponentValue(window.map)
95-
}
96-
9788
function extractContent(s: string) {
9889
var span = document.createElement("span")
9990
span.innerHTML = s
@@ -215,11 +206,12 @@ function getPixelatedStyles(pixelated: boolean) {
215206
return styles
216207
}
217208
window.Streamlit = Streamlit;
209+
window.debouncedUpdateComponentValue = debouncedUpdateComponentValue;
218210

219211
window.initComponent = (map: any, return_on_hover: boolean) => {
220212
const global_data = window.__GLOBAL_DATA__
221-
map.on("click", onMapClick)
222-
map.on("moveend", onMapMove)
213+
// map.on("click", onMapClick)
214+
// map.on("moveend", onMapMove)
223215
for (let key in map._layers) {
224216
let layer = map._layers[key]
225217
if (layer && layer["_url"] && layer["wmsParams"] && layer["wmsParams"]["layers"]) {

0 commit comments

Comments
 (0)