Skip to content

Commit aaf3bb6

Browse files
authored
FloatImage: use any kwargs as CSS (#1668)
1 parent 54a741a commit aaf3bb6

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

folium/plugins/float_image.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@
33

44

55
class FloatImage(MacroElement):
6-
"""Adds a floating image in HTML canvas on top of the map."""
6+
"""Adds a floating image in HTML canvas on top of the map.
7+
8+
Parameters
9+
----------
10+
image: str
11+
Url to image location. Can also be an inline image using a data URI
12+
or a local file using `file://`.
13+
bottom: int, default 75
14+
Vertical position from the bottom, as a percentage of screen height.
15+
left: int, default 75
16+
Horizontal position from the left, as a percentage of screen width.
17+
**kwargs
18+
Additional keyword arguments are applied as CSS properties.
19+
For example: `width='300px'`.
20+
21+
"""
722

823
_template = Template(
924
"""
1025
{% macro header(this,kwargs) %}
1126
<style>
1227
#{{this.get_name()}} {
13-
position:absolute;
14-
bottom:{{this.bottom}}%;
15-
left:{{this.left}}%;
16-
width:{{this.width}}%;
28+
position: absolute;
29+
bottom: {{this.bottom}}%;
30+
left: {{this.left}}%;
31+
{%- for property, value in this.css.items() %}
32+
{{ property }}: {{ value }};
33+
{%- endfor %}
1734
}
1835
</style>
1936
{% endmacro %}
@@ -27,10 +44,10 @@ class FloatImage(MacroElement):
2744
"""
2845
)
2946

30-
def __init__(self, image, bottom=75, left=75, width=100):
47+
def __init__(self, image, bottom=75, left=75, **kwargs):
3148
super().__init__()
3249
self._name = "FloatImage"
3350
self.image = image
3451
self.bottom = bottom
3552
self.left = left
36-
self.width = width
53+
self.css = kwargs

tests/plugins/test_float_image.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
def test_float_image():
1414
m = folium.Map([45.0, 3.0], zoom_start=4)
1515
url = "https://raw.githubusercontent.com/SECOORA/static_assets/master/maps/img/rose.png"
16-
szt = plugins.FloatImage(url, bottom=60, left=70, width=20)
16+
szt = plugins.FloatImage(url, bottom=60, left=70, width="20%")
1717
m.add_child(szt)
1818
m._repr_html_()
1919

@@ -35,10 +35,10 @@ def test_float_image():
3535
"""
3636
<style>
3737
#{{this.get_name()}} {
38-
position:absolute;
39-
bottom:60%;
40-
left:70%;
41-
width:20%;
38+
position: absolute;
39+
bottom: 60%;
40+
left: 70%;
41+
width: 20%;
4242
}
4343
</style>
4444
"""

0 commit comments

Comments
 (0)