Skip to content

FloatImage: use any kwargs as CSS #1668

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

Merged
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
31 changes: 24 additions & 7 deletions folium/plugins/float_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@


class FloatImage(MacroElement):
"""Adds a floating image in HTML canvas on top of the map."""
"""Adds a floating image in HTML canvas on top of the map.

Parameters
----------
image: str
Url to image location. Can also be an inline image using a data URI
or a local file using `file://`.
bottom: int, default 75
Vertical position from the bottom, as a percentage of screen height.
left: int, default 75
Horizontal position from the left, as a percentage of screen width.
**kwargs
Additional keyword arguments are applied as CSS properties.
For example: `width='300px'`.

"""

_template = Template(
"""
{% macro header(this,kwargs) %}
<style>
#{{this.get_name()}} {
position:absolute;
bottom:{{this.bottom}}%;
left:{{this.left}}%;
width:{{this.width}}%;
position: absolute;
bottom: {{this.bottom}}%;
left: {{this.left}}%;
{%- for property, value in this.css.items() %}
{{ property }}: {{ value }};
{%- endfor %}
}
</style>
{% endmacro %}
Expand All @@ -27,10 +44,10 @@ class FloatImage(MacroElement):
"""
)

def __init__(self, image, bottom=75, left=75, width=100):
def __init__(self, image, bottom=75, left=75, **kwargs):
super().__init__()
self._name = "FloatImage"
self.image = image
self.bottom = bottom
self.left = left
self.width = width
self.css = kwargs
10 changes: 5 additions & 5 deletions tests/plugins/test_float_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def test_float_image():
m = folium.Map([45.0, 3.0], zoom_start=4)
url = "https://raw.githubusercontent.com/SECOORA/static_assets/master/maps/img/rose.png"
szt = plugins.FloatImage(url, bottom=60, left=70, width=20)
szt = plugins.FloatImage(url, bottom=60, left=70, width="20%")
m.add_child(szt)
m._repr_html_()

Expand All @@ -35,10 +35,10 @@ def test_float_image():
"""
<style>
#{{this.get_name()}} {
position:absolute;
bottom:60%;
left:70%;
width:20%;
position: absolute;
bottom: 60%;
left: 70%;
width: 20%;
}
</style>
"""
Expand Down