Skip to content

Commit 527acd7

Browse files
authored
Support on_change callback functions and if custom key is passed use it in session state (#241)
* Support on_change callback functions and if custom key is passed use it in session state * Use hash key * Update tests for indentation * Dedent
1 parent 6309d61 commit 527acd7

File tree

6 files changed

+51
-40
lines changed

6 files changed

+51
-40
lines changed

Taskfile.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ tasks:
44
install-pre-commit:
55
desc: Install pre-commit hooks
66
cmds:
7-
- pip install pre-commit
7+
- uv pip install pre-commit
88
- pre-commit install
99
sources:
1010
- .pre-commit-config.yaml
1111

1212
install-package:
1313
desc: Install the package
1414
cmds:
15-
- pip install -e .
15+
- uv pip install -e .
1616
sources:
1717
- requirements.txt
1818
- setup.py
1919

2020
install-test-deps:
2121
desc: Install the dependencies for testing
2222
cmds:
23-
- pip install -r tests/requirements.txt
23+
- uv pip install -r tests/requirements.txt
24+
- playwright install --with-deps
2425
sources:
2526
- tests/requirements.txt
2627

pyproject.toml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ line-length = 88
44
[tool.ruff.lint]
55
ignore = ["B008", "ISC001", "E501", "W191", "B018"]
66
select = [
7-
"B",
8-
"E",
9-
"F",
10-
"W",
11-
"I",
12-
"N",
13-
"C4",
14-
"EXE",
15-
"ISC",
16-
"ICN",
17-
"PIE",
18-
"PT",
19-
"RET",
20-
"SIM",
21-
"ERA",
22-
"PLC",
23-
"RUF",
24-
"ARG",
7+
"B",
8+
"E",
9+
"F",
10+
"W",
11+
"I",
12+
"N",
13+
"C4",
14+
"EXE",
15+
"ISC",
16+
"ICN",
17+
"PIE",
18+
"PT",
19+
"RET",
20+
"SIM",
21+
"ERA",
22+
"PLC",
23+
"RUF",
24+
"ARG",
2525
]
2626

2727

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
streamlit>=1.13.0
1+
streamlit>=1.35.0
22
folium>=0.13,!=0.15.0
33
jinja2
44
branca

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setuptools.setup(
44
name="streamlit_folium",
5-
version="0.23.2",
5+
version="0.24.0",
66
author="Randy Zwitch",
77
author_email="[email protected]",
88
description="Render Folium objects in Streamlit",
@@ -13,5 +13,5 @@
1313
include_package_data=True,
1414
classifiers=[],
1515
python_requires=">=3.9",
16-
install_requires=["streamlit>=1.13.0", "folium>=0.13,!=0.15.0", "jinja2", "branca"],
16+
install_requires=["streamlit>=1.35.0", "folium>=0.13,!=0.15.0", "jinja2", "branca"],
1717
)

streamlit_folium/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import warnings
88
from textwrap import dedent
9-
from typing import Iterable
9+
from typing import Callable, Iterable
1010

1111
import branca
1212
import folium
@@ -212,6 +212,7 @@ def st_folium(
212212
pixelated: bool = False,
213213
debug: bool = False,
214214
render: bool = True,
215+
on_change: Callable | None = None,
215216
):
216217
"""Display a Folium object in Streamlit, returning data as user interacts
217218
with app.
@@ -398,11 +399,19 @@ def walk(fig):
398399
css_links.extend([href for _, href in getattr(elem, "default_css", [])])
399400
js_links.extend([src for _, src in getattr(elem, "default_js", [])])
400401

402+
hash_key = generate_js_hash(leaflet, key, return_on_hover)
403+
404+
def _on_change():
405+
if key is not None:
406+
st.session_state[key] = st.session_state.get(hash_key, {})
407+
if on_change is not None:
408+
on_change()
409+
401410
return _component_func(
402411
script=leaflet,
403412
html=html,
404413
id=m_id,
405-
key=generate_js_hash(leaflet, key, return_on_hover),
414+
key=hash_key,
406415
height=height,
407416
width=width,
408417
returned_objects=returned_objects,
@@ -415,6 +424,7 @@ def walk(fig):
415424
pixelated=pixelated,
416425
css_links=css_links,
417426
js_links=js_links,
427+
on_change=_on_change,
418428
)
419429

420430

tests/test_package.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from textwrap import dedent
2+
3+
14
def test_map():
25
import folium
36

@@ -8,16 +11,13 @@ def test_map():
811

912
leaflet = _get_map_string(map)
1013
assert (
11-
"""var map_div = L.map(
12-
"map_div",
13-
{
14-
center: [0.0, 0.0],
15-
crs: L.CRS.EPSG3857,
16-
zoom: 1,
17-
zoomControl: true,
18-
preferCanvas: false,
19-
}
20-
);"""
14+
dedent(
15+
"""var map_div = L.map(
16+
"map_div",
17+
{
18+
center: [0.0, 0.0],
19+
crs: L.CRS.EPSG3857,"""
20+
)
2121
in leaflet
2222
)
2323

@@ -54,15 +54,15 @@ def test_draw_support():
5454

5555
assert (
5656
"""map_div.on('draw:created', function(e) {
57-
drawnItems.addLayer(e.layer);
58-
});"""
57+
drawnItems.addLayer(e.layer);
58+
});"""
5959
in leaflet
6060
)
6161

6262
assert (
6363
"""var draw_control_div_1 = new L.Control.Draw(
64-
options
65-
).addTo( map_div );"""
64+
options
65+
).addTo( map_div );"""
6666
in leaflet
6767
)
6868

0 commit comments

Comments
 (0)