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

Leaflet control #1902

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rewrite mouse_position to demonstrate Control
Most of the code of mouse_position has been removed.
Instead it simply calls the constructor of Control with
the typed parameters.
  • Loading branch information
hansthen committed Jun 2, 2024
commit d020cc50387380ee0e96e772c216aa31876e7a64
4 changes: 2 additions & 2 deletions folium/plugins/beautify_icon.py
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ def __init__(
inner_icon_style="",
spin=False,
number=None,
**kwargs
**kwargs,
Copy link
Member

Choose a reason for hiding this comment

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

why are all these trailing commas in this PR? Nothing can follow a **kwargs entry so a trailing comma is not necessary. Black or ruff doesn't add these if I'm not mistaken, so why add them?

Copy link
Collaborator Author

@hansthen hansthen Jun 16, 2024

Choose a reason for hiding this comment

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

They were added by ruff format, so don't blame me :-). I agree they do not make sense in this case. As far as I know there is no option to remove these trailing commas. I can manually undo these kind of changes, but its a hassle. I'd prefer to just blindly follow ruff.

Copy link
Member

Choose a reason for hiding this comment

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

We don't run ruff format on this repo yet though. We use Black to autoformat and have Ruff as linter. So it's better not to run ruff format, since it will create a lot of changes.

Copy link
Member

Choose a reason for hiding this comment

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

We could switch over to ruff format though and ditch Black. Maybe that's good to do anyway, and if we merge that before this PR that also solves the issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My bad, I thought we used ruff as a formatter as well. I can create an extra PR to ditch black if you want, but I have no strong opinion either way. If not I will undo the ruff formatting changes.

):
super().__init__()
self._name = "BeautifyIcon"
@@ -111,5 +111,5 @@ def __init__(
spin=spin,
isAlphaNumericIcon=number is not None,
text=number,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions folium/plugins/fullscreen.py
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ def __init__(
title="Full Screen",
title_cancel="Exit Full Screen",
force_separate_button=False,
**kwargs
**kwargs,
):
super().__init__()
self._name = "Fullscreen"
@@ -65,5 +65,5 @@ def __init__(
title=title,
title_cancel=title_cancel,
force_separate_button=force_separate_button,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions folium/plugins/geocoder.py
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ def __init__(
zoom: Optional[int] = 11,
provider: str = "nominatim",
provider_options: dict = {},
**kwargs
**kwargs,
):
super().__init__()
self._name = "Geocoder"
@@ -89,5 +89,5 @@ def __init__(
zoom=zoom,
provider=provider,
provider_options=provider_options,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions folium/plugins/heat_map.py
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ def __init__(
overlay=True,
control=True,
show=True,
**kwargs
**kwargs,
):
super().__init__(name=name, overlay=overlay, control=control, show=show)
self._name = "HeatMap"
@@ -96,7 +96,7 @@ def __init__(
radius=radius,
blur=blur,
gradient=gradient,
**kwargs
**kwargs,
)

def _get_self_bounds(self):
2 changes: 1 addition & 1 deletion folium/plugins/marker_cluster.py
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ def __init__(
show=True,
icon_create_function=None,
options=None,
**kwargs
**kwargs,
):
if options is not None:
kwargs.update(options) # options argument is legacy
4 changes: 2 additions & 2 deletions folium/plugins/measure_control.py
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ def __init__(
secondary_length_unit="miles",
primary_area_unit="sqmeters",
secondary_area_unit="acres",
**kwargs
**kwargs,
):
super().__init__()
self._name = "MeasureControl"
@@ -79,5 +79,5 @@ def __init__(
secondary_length_unit=secondary_length_unit,
primary_area_unit=primary_area_unit,
secondary_area_unit=secondary_area_unit,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions folium/plugins/minimap.py
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ def __init__(
toggle_display=False,
auto_toggle_display=False,
minimized=False,
**kwargs
**kwargs,
):
super().__init__()
self._name = "MiniMap"
@@ -128,5 +128,5 @@ def __init__(
toggle_display=toggle_display,
auto_toggle_display=auto_toggle_display,
minimized=minimized,
**kwargs
**kwargs,
)
54 changes: 15 additions & 39 deletions folium/plugins/mouse_position.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.features import Control
from folium.utilities import parse_options
from folium.utilities import JsCode


class MousePosition(JSCSSMixin, Control):
@@ -45,34 +43,6 @@ class MousePosition(JSCSSMixin, Control):

"""

_template = Template(
"""
{% macro script(this, kwargs) %}
var {{ this.get_name() }} = new L.Control.MousePosition(
{{ this.options|tojson }}
);
{{ this.get_name() }}.options["latFormatter"] =
{{ this.lat_formatter }};
{{ this.get_name() }}.options["lngFormatter"] =
{{ this.lng_formatter }};
{{ this._parent.get_name() }}.addControl({{ this.get_name() }});
{% endmacro %}
"""
)

default_js = [
(
"Control_MousePosition_js",
"https://cdn.jsdelivr.net/gh/ardhi/Leaflet.MousePosition/src/L.Control.MousePosition.min.js",
)
]
default_css = [
(
"Control_MousePosition_css",
"https://cdn.jsdelivr.net/gh/ardhi/Leaflet.MousePosition/src/L.Control.MousePosition.min.css",
)
]

def __init__(
self,
position="bottomright",
@@ -83,19 +53,25 @@ def __init__(
prefix="",
lat_formatter=None,
lng_formatter=None,
**kwargs
**kwargs,
):
super().__init__()
self._name = "MousePosition"

self.options = parse_options(
super().__init__(
control="MousePosition",
position=position,
separator=separator,
empty_string=empty_string,
lng_first=lng_first,
num_digits=num_digits,
prefix=prefix,
**kwargs
lat_formatter=JsCode(lat_formatter) if lat_formatter else None,
lng_formatter=JsCode(lng_formatter) if lng_formatter else None,
**kwargs,
)
self.add_js_link(
"Control_MousePosition_js",
"https://cdn.jsdelivr.net/gh/ardhi/Leaflet.MousePosition/src/L.Control.MousePosition.min.js",
)
self.add_css_link(
"Control_MousePosition_css",
"https://cdn.jsdelivr.net/gh/ardhi/Leaflet.MousePosition/src/L.Control.MousePosition.min.css",
Comment on lines +70 to +76
Copy link
Member

Choose a reason for hiding this comment

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

please use the JSCSSMixin class attributes for these

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why? The method calls seem cleaner to me compared to the static attributes?

Copy link
Member

@Conengmo Conengmo Jun 16, 2024

Choose a reason for hiding this comment

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

the static attributes allow users that want to create offline maps to bundle these dependencies and then update the values in the static attributes. I know they might also use these new methods, but it's breaking behavior to define these in the init for this use case.

Also, I think it's better to have a consistent way of working. So not have two different ways of defining dependencies.

)
self.lat_formatter = lat_formatter or "undefined"
self.lng_formatter = lng_formatter or "undefined"
4 changes: 2 additions & 2 deletions folium/plugins/pattern.py
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ def __init__(
space_color="#ffffff",
opacity=0.75,
space_opacity=0.0,
**kwargs
**kwargs,
):
super().__init__()
self._name = "StripePattern"
@@ -67,7 +67,7 @@ def __init__(
space_color=space_color,
opacity=opacity,
space_opacity=space_opacity,
**kwargs
**kwargs,
)
self.parent_map = None

4 changes: 2 additions & 2 deletions folium/plugins/polyline_text_path.py
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ def __init__(
offset=0,
orientation=0,
attributes=None,
**kwargs
**kwargs,
):
super().__init__()
self._name = "PolyLineTextPath"
@@ -76,5 +76,5 @@ def __init__(
offset=offset,
orientation=orientation,
attributes=attributes,
**kwargs
**kwargs,
)
2 changes: 1 addition & 1 deletion folium/plugins/realtime.py
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ def __init__(
update_feature: Union[JsCode, str, None] = None,
remove_missing: bool = False,
container: Optional[Union[FeatureGroup, GeoJson]] = None,
**kwargs
**kwargs,
):
super().__init__()
self._name = "Realtime"
2 changes: 1 addition & 1 deletion folium/plugins/semicircle.py
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ def __init__(
stop_angle=None,
popup=None,
tooltip=None,
**kwargs
**kwargs,
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "SemiCircle"
4 changes: 2 additions & 2 deletions folium/plugins/tag_filter_button.py
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ def __init__(
clear_text="clear",
filter_on_every_click=True,
open_popup_on_hover=False,
**kwargs
**kwargs,
):
super().__init__()
self._name = "TagFilterButton"
@@ -92,5 +92,5 @@ def __init__(
clear_text=clear_text,
filter_on_every_click=filter_on_every_click,
open_popup_on_hover=open_popup_on_hover,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions folium/plugins/timeline.py
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@ def __init__(
self,
data: Union[dict, str, TextIO],
get_interval: Optional[JsCode] = None,
**kwargs
**kwargs,
):
super().__init__(data)
self._name = "Timeline"
@@ -232,7 +232,7 @@ def __init__(
show_ticks: bool = True,
steps: int = 1000,
playback_duration: int = 10000,
**kwargs
**kwargs,
):
super().__init__()
self._name = "TimelineSlider"
2 changes: 1 addition & 1 deletion folium/plugins/treelayercontrol.py
Original file line number Diff line number Diff line change
@@ -146,7 +146,7 @@ def __init__(
collapse_all: str = "",
expand_all: str = "",
label_is_selector: str = "both",
**kwargs
**kwargs,
):
super().__init__()
self._name = "TreeLayerControl"
8 changes: 4 additions & 4 deletions folium/vector_layers.py
Original file line number Diff line number Diff line change
@@ -230,7 +230,7 @@ def __init__(
locations: TypeMultiLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
**kwargs: TypePathOptions,
):
super().__init__(locations, popup=popup, tooltip=tooltip)
self._name = "Polygon"
@@ -272,7 +272,7 @@ def __init__(
bounds: TypeLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
**kwargs: TypePathOptions,
):
super().__init__()
self._name = "rectangle"
@@ -333,7 +333,7 @@ def __init__(
radius: float = 50,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
**kwargs: TypePathOptions,
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "circle"
@@ -379,7 +379,7 @@ def __init__(
radius: float = 10,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
**kwargs: TypePathOptions,
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "CircleMarker"