Skip to content

Commit becda03

Browse files
committed
Cleanup macroelements
Earlier I noted many macroelements do not fully use the facilities of Figure, but instead in render manipulate the parent Figure. Step 1: remove GlobalSwitches as Element
1 parent f9b00cd commit becda03

File tree

9 files changed

+199
-277
lines changed

9 files changed

+199
-277
lines changed

folium/elements.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class JSCSSMixin(MacroElement):
1818
default_js: List[Tuple[str, str]] = []
1919
default_css: List[Tuple[str, str]] = []
2020

21+
# Since this is typically used as a mixin, we cannot
22+
# override the _template member variable here. It would
23+
# be overwritten by any subclassing class that also has
24+
# a _template variable.
2125
def render(self, **kwargs):
2226
figure = self.get_root()
2327
assert isinstance(

folium/folium.py

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import webbrowser
88
from typing import Any, List, Optional, Sequence, Union
99

10-
from branca.element import Element, Figure
10+
from branca.element import Figure
1111

1212
from folium.elements import JSCSSMixin
1313
from folium.map import Evented, FitBounds, Layer
@@ -62,23 +62,6 @@
6262
]
6363

6464

65-
class GlobalSwitches(Element):
66-
_template = Template(
67-
"""
68-
<script>
69-
L_NO_TOUCH = {{ this.no_touch |tojson}};
70-
L_DISABLE_3D = {{ this.disable_3d|tojson }};
71-
</script>
72-
"""
73-
)
74-
75-
def __init__(self, no_touch=False, disable_3d=False):
76-
super().__init__()
77-
self._name = "GlobalSwitches"
78-
self.no_touch = no_touch
79-
self.disable_3d = disable_3d
80-
81-
8265
class Map(JSCSSMixin, Evented):
8366
"""Create a Map with Folium and Leaflet.js
8467
@@ -192,6 +175,30 @@ class Map(JSCSSMixin, Evented):
192175
}
193176
.leaflet-container { font-size: {{this.font_size}}; }
194177
</style>
178+
179+
<style>html, body {
180+
width: 100%;
181+
height: 100%;
182+
margin: 0;
183+
padding: 0;
184+
}
185+
</style>
186+
187+
<style>#map {
188+
position:absolute;
189+
top:0;
190+
bottom:0;
191+
right:0;
192+
left:0;
193+
}
194+
</style>
195+
196+
197+
<script>
198+
L_NO_TOUCH = {{ this.no_touch |tojson}};
199+
L_DISABLE_3D = {{ this.disable_3d|tojson }};
200+
</script>
201+
195202
{% endmacro %}
196203
197204
{% macro html(this, kwargs) %}
@@ -304,6 +311,9 @@ def __init__(
304311
else:
305312
self.zoom_control_position = False
306313

314+
self.no_touch = no_touch
315+
self.disable_3d = disable_3d
316+
307317
self.options = remove_empty(
308318
max_bounds=max_bounds_array,
309319
zoom=zoom_start,
@@ -312,8 +322,6 @@ def __init__(
312322
**kwargs,
313323
)
314324

315-
self.global_switches = GlobalSwitches(no_touch, disable_3d)
316-
317325
self.objects_to_stay_in_front: List[Layer] = []
318326

319327
if isinstance(tiles, TileLayer):
@@ -377,45 +385,6 @@ def _repr_png_(self) -> Optional[bytes]:
377385
return None
378386
return self._to_png()
379387

380-
def render(self, **kwargs):
381-
"""Renders the HTML representation of the element."""
382-
figure = self.get_root()
383-
assert isinstance(
384-
figure, Figure
385-
), "You cannot render this Element if it is not in a Figure."
386-
387-
# Set global switches
388-
figure.header.add_child(self.global_switches, name="global_switches")
389-
390-
figure.header.add_child(
391-
Element(
392-
"<style>html, body {"
393-
"width: 100%;"
394-
"height: 100%;"
395-
"margin: 0;"
396-
"padding: 0;"
397-
"}"
398-
"</style>"
399-
),
400-
name="css_style",
401-
)
402-
403-
figure.header.add_child(
404-
Element(
405-
"<style>#map {"
406-
"position:absolute;"
407-
"top:0;"
408-
"bottom:0;"
409-
"right:0;"
410-
"left:0;"
411-
"}"
412-
"</style>"
413-
),
414-
name="map_style",
415-
)
416-
417-
super().render(**kwargs)
418-
419388
def show_in_browser(self) -> None:
420389
"""Display the Map in the default web browser."""
421390
with temp_html_filepath(self.get_root().render()) as fname:

folium/map.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections import OrderedDict
88
from typing import TYPE_CHECKING, Optional, Sequence, Union, cast
99

10-
from branca.element import Element, Figure, Html, MacroElement
10+
from branca.element import Element, Html, MacroElement
1111

1212
from folium.elements import ElementAddToElement, EventHandler
1313
from folium.template import Template
@@ -454,25 +454,27 @@ class Popup(MacroElement):
454454

455455
_template = Template(
456456
"""
457-
var {{this.get_name()}} = L.popup({{ this.options|tojavascript }});
458-
459-
{% for name, element in this.html._children.items() %}
460-
{% if this.lazy %}
461-
{{ this._parent.get_name() }}.once('click', function() {
462-
{{ this.get_name() }}.setContent($(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]);
463-
});
464-
{% else %}
465-
var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0];
466-
{{ this.get_name() }}.setContent({{ name }});
467-
{% endif %}
468-
{% endfor %}
469-
470-
{{ this._parent.get_name() }}.bindPopup({{ this.get_name() }})
471-
{% if this.show %}.openPopup(){% endif %};
472-
473-
{% for name, element in this.script._children.items() %}
474-
{{element.render()}}
475-
{% endfor %}
457+
{% macro script(this, kwargs) %}
458+
var {{this.get_name()}} = L.popup({{ this.options|tojavascript }});
459+
460+
{% for name, element in this.html._children.items() %}
461+
{% if this.lazy %}
462+
{{ this._parent.get_name() }}.once('click', function() {
463+
{{ this.get_name() }}.setContent($(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]);
464+
});
465+
{% else %}
466+
var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0];
467+
{{ this.get_name() }}.setContent({{ name }});
468+
{% endif %}
469+
{% endfor %}
470+
471+
{{ this._parent.get_name() }}.bindPopup({{ this.get_name() }})
472+
{% if this.show %}.openPopup(){% endif %};
473+
474+
{% for name, element in this.script._children.items() %}
475+
{{element.render()}}
476+
{% endfor %}
477+
{% endmacro %}
476478
"""
477479
) # noqa
478480

@@ -513,21 +515,6 @@ def __init__(
513515
**kwargs,
514516
)
515517

516-
def render(self, **kwargs):
517-
"""Renders the HTML representation of the element."""
518-
for name, child in self._children.items():
519-
child.render(**kwargs)
520-
521-
figure = self.get_root()
522-
assert isinstance(
523-
figure, Figure
524-
), "You cannot render this Element if it is not in a Figure."
525-
526-
figure.script.add_child(
527-
Element(self._template.render(this=self, kwargs=kwargs)),
528-
name=self.get_name(),
529-
)
530-
531518

532519
class Tooltip(MacroElement):
533520
"""

folium/plugins/draw.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from branca.element import Element, Figure, MacroElement
1+
from branca.element import MacroElement
22

33
from folium.elements import JSCSSMixin
44
from folium.template import Template
@@ -60,6 +60,29 @@ class Draw(JSCSSMixin, MacroElement):
6060

6161
_template = Template(
6262
"""
63+
{% macro html(this, kwargs) %}
64+
{% if this.export %}
65+
<style>
66+
#export {
67+
position: absolute;
68+
top: 5px;
69+
right: 10px;
70+
z-index: 999;
71+
background: white;
72+
color: black;
73+
padding: 6px;
74+
border-radius: 4px;
75+
font-family: 'Helvetica Neue';
76+
cursor: pointer;
77+
font-size: 12px;
78+
text-decoration: none;
79+
top: 90px;
80+
}
81+
</style>
82+
<a href='#' id='export'>Export</a>
83+
{% endif %}
84+
{% endmacro %}
85+
6386
{% macro script(this, kwargs) %}
6487
var options = {
6588
position: {{ this.position|tojson }},
@@ -155,35 +178,3 @@ def __init__(
155178
self.draw_options = draw_options or {}
156179
self.edit_options = edit_options or {}
157180
self.on = on or {}
158-
159-
def render(self, **kwargs):
160-
super().render(**kwargs)
161-
162-
figure = self.get_root()
163-
assert isinstance(
164-
figure, Figure
165-
), "You cannot render this Element if it is not in a Figure."
166-
167-
export_style = """
168-
<style>
169-
#export {
170-
position: absolute;
171-
top: 5px;
172-
right: 10px;
173-
z-index: 999;
174-
background: white;
175-
color: black;
176-
padding: 6px;
177-
border-radius: 4px;
178-
font-family: 'Helvetica Neue';
179-
cursor: pointer;
180-
font-size: 12px;
181-
text-decoration: none;
182-
top: 90px;
183-
}
184-
</style>
185-
"""
186-
export_button = """<a href='#' id='export'>Export</a>"""
187-
if self.export:
188-
figure.header.add_child(Element(export_style), name="export")
189-
figure.html.add_child(Element(export_button), name="export_button")

0 commit comments

Comments
 (0)