diff --git a/Taskfile.yml b/Taskfile.yml index 632af2e..6137d58 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,7 +4,7 @@ tasks: install-pre-commit: desc: Install pre-commit hooks cmds: - - pip install pre-commit + - uv pip install pre-commit - pre-commit install sources: - .pre-commit-config.yaml @@ -12,7 +12,7 @@ tasks: install-package: desc: Install the package cmds: - - pip install -e . + - uv pip install -e . sources: - requirements.txt - setup.py @@ -20,7 +20,8 @@ tasks: install-test-deps: desc: Install the dependencies for testing cmds: - - pip install -r tests/requirements.txt + - uv pip install -r tests/requirements.txt + - playwright install --with-deps sources: - tests/requirements.txt diff --git a/pyproject.toml b/pyproject.toml index 8e46680..6683e74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,24 +4,24 @@ line-length = 88 [tool.ruff.lint] ignore = ["B008", "ISC001", "E501", "W191", "B018"] select = [ - "B", - "E", - "F", - "W", - "I", - "N", - "C4", - "EXE", - "ISC", - "ICN", - "PIE", - "PT", - "RET", - "SIM", - "ERA", - "PLC", - "RUF", - "ARG", + "B", + "E", + "F", + "W", + "I", + "N", + "C4", + "EXE", + "ISC", + "ICN", + "PIE", + "PT", + "RET", + "SIM", + "ERA", + "PLC", + "RUF", + "ARG", ] diff --git a/requirements.txt b/requirements.txt index 2b78e61..a530b86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -streamlit>=1.13.0 +streamlit>=1.35.0 folium>=0.13,!=0.15.0 jinja2 branca diff --git a/setup.py b/setup.py index c10973f..f4313d1 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setuptools.setup( name="streamlit_folium", - version="0.23.2", + version="0.24.0", author="Randy Zwitch", author_email="rzwitch@gmail.com", description="Render Folium objects in Streamlit", @@ -13,5 +13,5 @@ include_package_data=True, classifiers=[], python_requires=">=3.9", - install_requires=["streamlit>=1.13.0", "folium>=0.13,!=0.15.0", "jinja2", "branca"], + install_requires=["streamlit>=1.35.0", "folium>=0.13,!=0.15.0", "jinja2", "branca"], ) diff --git a/streamlit_folium/__init__.py b/streamlit_folium/__init__.py index 5ceab9b..7892492 100644 --- a/streamlit_folium/__init__.py +++ b/streamlit_folium/__init__.py @@ -6,7 +6,7 @@ import re import warnings from textwrap import dedent -from typing import Iterable +from typing import Callable, Iterable import branca import folium @@ -212,6 +212,7 @@ def st_folium( pixelated: bool = False, debug: bool = False, render: bool = True, + on_change: Callable | None = None, ): """Display a Folium object in Streamlit, returning data as user interacts with app. @@ -398,11 +399,19 @@ def walk(fig): css_links.extend([href for _, href in getattr(elem, "default_css", [])]) js_links.extend([src for _, src in getattr(elem, "default_js", [])]) + hash_key = generate_js_hash(leaflet, key, return_on_hover) + + def _on_change(): + if key is not None: + st.session_state[key] = st.session_state.get(hash_key, {}) + if on_change is not None: + on_change() + return _component_func( script=leaflet, html=html, id=m_id, - key=generate_js_hash(leaflet, key, return_on_hover), + key=hash_key, height=height, width=width, returned_objects=returned_objects, @@ -415,6 +424,7 @@ def walk(fig): pixelated=pixelated, css_links=css_links, js_links=js_links, + on_change=_on_change, ) diff --git a/tests/test_package.py b/tests/test_package.py index b196529..1c95e4f 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -1,3 +1,6 @@ +from textwrap import dedent + + def test_map(): import folium @@ -8,16 +11,13 @@ def test_map(): leaflet = _get_map_string(map) assert ( - """var map_div = L.map( - "map_div", - { - center: [0.0, 0.0], - crs: L.CRS.EPSG3857, - zoom: 1, - zoomControl: true, - preferCanvas: false, - } -);""" + dedent( + """var map_div = L.map( + "map_div", + { + center: [0.0, 0.0], + crs: L.CRS.EPSG3857,""" + ) in leaflet ) @@ -54,15 +54,15 @@ def test_draw_support(): assert ( """map_div.on('draw:created', function(e) { - drawnItems.addLayer(e.layer); -});""" + drawnItems.addLayer(e.layer); + });""" in leaflet ) assert ( """var draw_control_div_1 = new L.Control.Draw( - options -).addTo( map_div );""" + options + ).addTo( map_div );""" in leaflet )