Skip to content

Commit fe2ac44

Browse files
committed
Second iteration of the tests
1 parent 88cc26e commit fe2ac44

16 files changed

+272
-23
lines changed

folium/features.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
)
2424
from branca.utilities import color_brewer
2525

26+
import folium.map
2627
from folium.elements import JSCSSMixin
2728
from folium.folium import Map
28-
from folium.map import FeatureGroup, Icon, Layer, Marker, Popup, Tooltip
2929
from folium.template import Template
3030
from folium.utilities import (
3131
TypeBoundsReturn,
@@ -47,7 +47,7 @@
4747
from folium.vector_layers import Circle, CircleMarker, PolyLine, path_options
4848

4949

50-
class RegularPolygonMarker(JSCSSMixin, Marker):
50+
class RegularPolygonMarker(JSCSSMixin, folium.map.Marker):
5151
"""
5252
Custom markers using the Leaflet Data Vis Framework.
5353
@@ -96,8 +96,8 @@ def __init__(
9696
number_of_sides: int = 4,
9797
rotation: int = 0,
9898
radius: int = 15,
99-
popup: Union[Popup, str, None] = None,
100-
tooltip: Union[Tooltip, str, None] = None,
99+
popup: Union[folium.map.Popup, str, None] = None,
100+
tooltip: Union[folium.map.Tooltip, str, None] = None,
101101
**kwargs: TypePathOptions,
102102
):
103103
super().__init__(location, popup=popup, tooltip=tooltip)
@@ -297,7 +297,7 @@ def __init__(
297297
def render(self, **kwargs):
298298
"""Renders the HTML representation of the element."""
299299
parent = self._parent
300-
if not isinstance(parent, (Figure, Div, Popup)):
300+
if not isinstance(parent, (Figure, Div, folium.map.Popup)):
301301
raise TypeError(
302302
"VegaLite elements can only be added to a Figure, Div, or Popup"
303303
)
@@ -467,7 +467,7 @@ def _embed_vegalite_v1(self, figure: Figure, parent: TypeContainer) -> None:
467467
)
468468

469469

470-
class GeoJson(Layer):
470+
class GeoJson(folium.map.Layer):
471471
"""
472472
Creates a GeoJson object for plotting into a Map.
473473
@@ -675,11 +675,11 @@ def __init__(
675675
control: bool = True,
676676
show: bool = True,
677677
smooth_factor: Optional[float] = None,
678-
tooltip: Union[str, Tooltip, "GeoJsonTooltip", None] = None,
678+
tooltip: Union[str, folium.map.Tooltip, "GeoJsonTooltip", None] = None,
679679
embed: bool = True,
680680
popup: Optional["GeoJsonPopup"] = None,
681681
zoom_on_click: bool = False,
682-
marker: Union[Circle, CircleMarker, Marker, None] = None,
682+
marker: Union[Circle, CircleMarker, folium.map.Marker, None] = None,
683683
**kwargs: Any,
684684
):
685685
super().__init__(name=name, overlay=overlay, control=control, show=show)
@@ -693,7 +693,7 @@ def __init__(
693693
self.highlight = highlight_function is not None
694694
self.zoom_on_click = zoom_on_click
695695
if marker:
696-
if not isinstance(marker, (Circle, CircleMarker, Marker)):
696+
if not isinstance(marker, (Circle, CircleMarker, folium.map.Marker)):
697697
raise TypeError(
698698
"Only Marker, Circle, and CircleMarker are supported as GeoJson marker types."
699699
)
@@ -721,11 +721,11 @@ def __init__(
721721
self.highlight_map: dict = {}
722722
self.feature_identifier = self.find_identifier()
723723

724-
if isinstance(tooltip, (GeoJsonTooltip, Tooltip)):
724+
if isinstance(tooltip, (GeoJsonTooltip, folium.map.Tooltip)):
725725
self.add_child(tooltip)
726726
elif tooltip is not None:
727-
self.add_child(Tooltip(tooltip))
728-
if isinstance(popup, (GeoJsonPopup, Popup)):
727+
self.add_child(folium.map.Tooltip(tooltip))
728+
if isinstance(popup, (GeoJsonPopup, folium.map.Popup)):
729729
self.add_child(popup)
730730

731731
def process_data(self, data: Any) -> dict:
@@ -916,7 +916,7 @@ def _set_default_key(mapping: TypeStyleMapping) -> None:
916916
del mapping[key_longest]
917917

918918

919-
class TopoJson(JSCSSMixin, Layer):
919+
class TopoJson(JSCSSMixin, folium.map.Layer):
920920
"""
921921
Creates a TopoJson object for plotting into a Map.
922922
@@ -1011,7 +1011,7 @@ def __init__(
10111011
control: bool = True,
10121012
show: bool = True,
10131013
smooth_factor: Optional[float] = None,
1014-
tooltip: Union[str, Tooltip, None] = None,
1014+
tooltip: Union[str, folium.map.Tooltip, None] = None,
10151015
):
10161016
super().__init__(name=name, overlay=overlay, control=control, show=show)
10171017
self._name = "TopoJson"
@@ -1035,10 +1035,10 @@ def __init__(
10351035

10361036
self.smooth_factor = smooth_factor
10371037

1038-
if isinstance(tooltip, (GeoJsonTooltip, Tooltip)):
1038+
if isinstance(tooltip, (GeoJsonTooltip, folium.map.Tooltip)):
10391039
self.add_child(tooltip)
10401040
elif tooltip is not None:
1041-
self.add_child(Tooltip(tooltip))
1041+
self.add_child(folium.map.Tooltip(tooltip))
10421042

10431043
def style_data(self) -> None:
10441044
"""Applies self.style_function to each feature of self.data."""
@@ -1377,7 +1377,7 @@ def __init__(
13771377
self.popup_options = kwargs
13781378

13791379

1380-
class Choropleth(FeatureGroup):
1380+
class Choropleth(folium.map.FeatureGroup):
13811381
"""Apply a GeoJSON overlay to the map.
13821382
13831383
Plot a GeoJSON overlay on the base map. There is no requirement
@@ -1855,7 +1855,7 @@ def __init__(self, format_str: Optional[str] = None, alert: bool = True):
18551855
self.alert = alert
18561856

18571857

1858-
class CustomIcon(Icon):
1858+
class CustomIcon(folium.map.Icon):
18591859
"""
18601860
Create a custom icon, based on an image.
18611861
@@ -1908,7 +1908,7 @@ def __init__(
19081908
shadow_anchor: Optional[Tuple[int, int]] = None,
19091909
popup_anchor: Optional[Tuple[int, int]] = None,
19101910
):
1911-
super(Icon, self).__init__()
1911+
super(folium.map.Icon, self).__init__()
19121912
self._name = "CustomIcon"
19131913
self.options = remove_empty(
19141914
icon_url=image_to_url(icon_image),
@@ -1921,7 +1921,7 @@ def __init__(
19211921
)
19221922

19231923

1924-
class ColorLine(FeatureGroup):
1924+
class ColorLine(folium.map.FeatureGroup):
19251925
"""
19261926
Draw data on a map with specified colors.
19271927

folium/map.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from branca.element import Element, Figure, Html, MacroElement
1111

12+
import folium.features as features
1213
from folium.elements import ElementAddToElement, EventHandler
1314
from folium.template import Template
1415
from folium.utilities import (
@@ -408,8 +409,7 @@ def __init__(
408409
# this attribute is not used by Marker, but by GeoJson
409410
self.icon = None
410411
if icon is not None:
411-
self.add_child(icon)
412-
self.icon = icon
412+
self.set_icon(icon)
413413
if popup is not None:
414414
self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup)))
415415
if tooltip is not None:
@@ -434,6 +434,16 @@ def render(self):
434434
self.add_child(self.SetIcon(marker=self, icon=self.icon))
435435
super().render()
436436

437+
def set_icon(self, icon: Union[Icon, "CustomIcon", "DivIcon"]):
438+
super().add_child(icon)
439+
self.icon = icon
440+
441+
def add_child(self, child, name=None, index=None):
442+
if isinstance(child, (Icon, features.CustomIcon, features.DivIcon)):
443+
self.set_icon(child)
444+
else:
445+
super().add_child(child, name, index)
446+
437447

438448
class Popup(MacroElement):
439449
"""Create a Popup instance that can be linked to a Layer.

folium/plugins/search.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from branca.element import MacroElement
22

3+
from folium import FeatureGroup, GeoJson, TopoJson
34
from folium.elements import JSCSSMixin
4-
from folium.features import FeatureGroup, GeoJson, TopoJson
5+
6+
# from folium.map import FeatureGroup
7+
# from folium.features import GeoJson, TopoJson
58
from folium.folium import Map
69
from folium.plugins import MarkerCluster
710
from folium.template import Template

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ nbval
2525
owslib
2626
pandas >=2
2727
pillow
28+
pixelmatch
2829
pre-commit
2930
pycodestyle
3031
pydata-sphinx-theme

tests/playwright/regressions/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import streamlit as st
2+
3+
st.logo("https://python-visualization.github.io/folium/latest/_static/folium_logo.png")
4+
st.title("Python data, leaflet.js maps")
5+
6+
st.write(
7+
"""
8+
This app is meant to store regression tests. For issues, create a
9+
test pages under pages with the format issue_d+.
10+
"""
11+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from streamlit_folium import st_folium
2+
3+
import folium
4+
5+
# Library of Congress coordinates (latitude, longitude)
6+
loc_coordinates = (38.8886, -77.0047)
7+
8+
# Create a Folium map centered around the Library of Congress
9+
map_lc = folium.Map(location=loc_coordinates, zoom_start=15)
10+
11+
# Define the DivIcon with the custom icon. This variable can be used in one marker successfully, but will fail if we use it in two markers.
12+
13+
14+
icon = folium.DivIcon(
15+
icon_anchor=(15, 15),
16+
html="""<div><img src="/app/static/book-open-variant-outline.png" height="35" width="35"/></div>""",
17+
)
18+
19+
20+
folium.Marker(
21+
location=(38.886970844230866, -77.00471380332),
22+
popup="Library of Congress: James Madison Building",
23+
icon=icon,
24+
).add_to(map_lc)
25+
26+
folium.Marker(location=loc_coordinates, popup="Library of Congress", icon=icon).add_to(
27+
map_lc
28+
)
29+
# if we save here, everything will be fine.
30+
31+
st_folium(map_lc, width=600, height=500)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from streamlit_folium import st_folium
2+
3+
import folium
4+
5+
# Library of Congress coordinates (latitude, longitude)
6+
loc_coordinates = (38.8886, -77.0047)
7+
8+
# Create a Folium map centered around the Library of Congress
9+
map_lc = folium.Map(location=loc_coordinates, zoom_start=15)
10+
11+
# Define the DivIcon with the custom icon. This variable can be used in one marker successfully, but will fail if we use it in two markers.
12+
icon = folium.DivIcon(
13+
icon_anchor=(15, 15),
14+
html="""<div><img src="/app/static/book-open-variant-outline.png" height="35" width="35"/></div>""",
15+
)
16+
17+
18+
folium.Marker(
19+
location=(38.886970844230866, -77.00471380332),
20+
popup="Library of Congress: James Madison Building",
21+
icon=icon,
22+
).add_to(map_lc)
23+
24+
marker = folium.Marker(
25+
location=loc_coordinates,
26+
popup="Library of Congress",
27+
).add_to(map_lc)
28+
29+
marker.add_child(icon)
30+
# if we save here, everything will be fine.
31+
32+
st_folium(map_lc, width=600, height=500)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from streamlit_folium import st_folium
2+
3+
import folium
4+
5+
# Library of Congress coordinates (latitude, longitude)
6+
loc_coordinates = (38.8886, -77.0047)
7+
8+
# Create a Folium map centered around the Library of Congress
9+
map_lc = folium.Map(location=loc_coordinates, zoom_start=15)
10+
11+
# Define the DivIcon with the custom icon. This variable can be used in one marker successfully, but will fail if we use it in two markers.
12+
13+
14+
icon = folium.DivIcon(
15+
icon_anchor=(15, 15),
16+
html="""<div><img src="/app/static/book-open-variant-outline.png" height="35" width="35"/></div>""",
17+
)
18+
19+
20+
folium.Marker(
21+
location=(38.886970844230866, -77.00471380332),
22+
popup="Library of Congress: James Madison Building",
23+
icon=icon,
24+
).add_to(map_lc)
25+
26+
marker = folium.Marker(
27+
location=loc_coordinates,
28+
popup="Library of Congress",
29+
).add_to(map_lc)
30+
# if we save here, everything will be fine.
31+
32+
icon.add_to(marker)
33+
34+
st_folium(map_lc, width=600, height=500)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from streamlit_folium import st_folium
2+
3+
import folium
4+
5+
# Library of Congress coordinates (latitude, longitude)
6+
loc_coordinates = (38.8886, -77.0047)
7+
8+
# Create a Folium map centered around the Library of Congress
9+
map_lc = folium.Map(location=loc_coordinates, zoom_start=15)
10+
11+
# Define the DivIcon with the custom icon. This variable can be used in one marker successfully, but will fail if we use it in two markers.
12+
13+
14+
icon = folium.DivIcon(
15+
icon_anchor=(15, 15),
16+
html="""<div><img src="/app/static/book-open-variant-outline.png" height="35" width="35"/></div>""",
17+
)
18+
19+
20+
marker1 = folium.Marker(
21+
location=(38.886970844230866, -77.00471380332),
22+
popup="Library of Congress: James Madison Building",
23+
).add_to(map_lc)
24+
25+
marker2 = folium.Marker(
26+
location=loc_coordinates, popup="Library of Congress", icon=icon
27+
).add_to(map_lc)
28+
marker1.set_icon(icon)
29+
marker2.set_icon(icon)
30+
31+
st_folium(map_lc, width=600, height=500)
569 Bytes
Loading

0 commit comments

Comments
 (0)