Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support on_change callback functions and if custom key is passed use it in session state #241

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Taskfile.yml
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, I'd say split this up...but you're basically doing this to future you 😏

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ 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

install-package:
desc: Install the package
cmds:
- pip install -e .
- uv pip install -e .
sources:
- requirements.txt
- setup.py

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

Expand Down
36 changes: 18 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
streamlit>=1.13.0
streamlit>=1.35.0
folium>=0.13,!=0.15.0
jinja2
branca
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="streamlit_folium",
version="0.23.2",
version="0.24.0",
author="Randy Zwitch",
author_email="[email protected]",
description="Render Folium objects in Streamlit",
Expand All @@ -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"],
)
14 changes: 12 additions & 2 deletions streamlit_folium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -415,6 +424,7 @@ def walk(fig):
pixelated=pixelated,
css_links=css_links,
js_links=js_links,
on_change=_on_change,
)


Expand Down
28 changes: 14 additions & 14 deletions tests/test_package.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from textwrap import dedent


def test_map():
import folium

Expand All @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down
Loading