Skip to content

Commit b94e61e

Browse files
committed
plugins: add fullscreen plugin
1 parent e59fa78 commit b94e61e

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

examples/fullscreen_plugin_example.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
import folium
3+
4+
from folium.plugins import Fullscreen
5+
6+
m = folium.Map(location=[41.9, -97.3], zoom_start=4)
7+
Fullscreen().add_to(m)
8+
9+
m.save(outfile='fullscreen.html')

folium/plugins/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
from .timestamped_geo_json import TimestampedGeoJson
1414
from .heat_map import HeatMap
1515
from .image_overlay import ImageOverlay
16+
from .fullscreen import Fullscreen
1617

1718
__all__ = ['MarkerCluster',
1819
'ScrollZoomToggler',
1920
'Terminator',
2021
'BoatMarker',
2122
'TimestampedGeoJson',
2223
'HeatMap',
23-
'ImageOverlay']
24+
'ImageOverlay',
25+
'Fullscreen']

folium/plugins/fullscreen.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Fullscreen
4+
--------------
5+
6+
https://github.com/brunob/leaflet.fullscreen
7+
8+
Adds fullscreen button to your maps.
9+
"""
10+
from jinja2 import Template
11+
12+
from branca.element import MacroElement, Figure, JavascriptLink, CssLink
13+
14+
15+
class Fullscreen(MacroElement):
16+
"""
17+
Adds a fullscreen button to your map.
18+
19+
Note: does not work from within an iframe, and because of this, does not
20+
work in jupyter notebook
21+
"""
22+
23+
def __init__(self):
24+
"""Add button to take your Folium map fullscreen"""
25+
super(Fullscreen, self).__init__()
26+
self._name = 'Fullscreen'
27+
28+
self._template = Template("""
29+
{% macro script(this, kwargs) %}
30+
L.control.fullscreen().addTo({{this._parent.get_name()}});
31+
{{this._parent.get_name()}}.on('enterFullscreen', function(){
32+
console.log('entered fullscreen');
33+
});
34+
35+
{% endmacro %}
36+
""") # noqa
37+
38+
def render(self, **kwargs):
39+
super(Fullscreen, self).render()
40+
41+
figure = self.get_root()
42+
assert isinstance(figure, Figure), ("You cannot render this Element "
43+
"if it's not in a Figure.")
44+
45+
figure.header.add_children(
46+
JavascriptLink(
47+
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.fullscreen/1.4.2/Control.FullScreen.min.js"), # noqa
48+
name='Control.Fullscreen.js'
49+
)
50+
51+
figure.header.add_children(
52+
CssLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.fullscreen/1.4.2/Control.FullScreen.min.css"), # noqa
53+
name='Control.FullScreen.css'
54+
)

tests/plugins/test_fullscreen.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test Fullscreen
4+
-----------------------
5+
"""
6+
from jinja2 import Template
7+
8+
import folium
9+
from folium import plugins
10+
11+
12+
def test_fullscreen():
13+
data = {
14+
"type": "FeatureCollection",
15+
"features": [
16+
{
17+
"type": "Feature",
18+
"geometry": {
19+
"type": "Point",
20+
"coordinates": [0, 0],
21+
},
22+
"properties": {}
23+
}
24+
],
25+
}
26+
27+
m = folium.Map([47, 3], zoom_start=1)
28+
fs = plugins.Fullscreen()
29+
m.add_children(fs)
30+
m._repr_html_()
31+
32+
out = m._parent.render()
33+
34+
# verify that the fullscreen control was rendered
35+
assert 'L.control.fullscreen().addTo' in out
36+

0 commit comments

Comments
 (0)