Skip to content

Commit fed24a4

Browse files
author
Martin Journois
committed
Add popup option for PolyLine and MultiPolyLine
1 parent 50ac6c9 commit fed24a4

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

folium/features.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import json
1010

1111
from .utilities import (color_brewer, _parse_size, legend_scaler,
12-
_locations_mirror, _locations_tolist, image_to_url)
12+
_locations_mirror, _locations_tolist, image_to_url,
13+
text_type, binary_type)
1314

1415
from .element import Element, Figure, JavascriptLink, CssLink, MacroElement
15-
from .map import TileLayer, Icon, Marker
16+
from .map import TileLayer, Icon, Marker, Popup
1617

1718

1819
class WmsTileLayer(TileLayer):
@@ -502,7 +503,7 @@ def __init__(self, popup=None):
502503

503504
class PolyLine(MacroElement):
504505
def __init__(self, locations, color=None, weight=None,
505-
opacity=None, latlon=True):
506+
opacity=None, latlon=True, popup=None):
506507
"""
507508
Creates a PolyLine object to append into a map with
508509
Map.add_children.
@@ -519,6 +520,8 @@ def __init__(self, locations, color=None, weight=None,
519520
or not ([[lon, lat]] if False).
520521
Note that the default GeoJson format is latlon=False,
521522
while Leaflet polyline's default is latlon=True.
523+
popup: string or folium.Popup, default None
524+
Input text or visualization for object.
522525
"""
523526
super(PolyLine, self).__init__()
524527
self._name = 'PolyLine'
@@ -527,6 +530,10 @@ def __init__(self, locations, color=None, weight=None,
527530
self.color = color
528531
self.weight = weight
529532
self.opacity = opacity
533+
if isinstance(popup, text_type) or isinstance(popup, binary_type):
534+
self.add_children(Popup(popup))
535+
elif popup is not None:
536+
self.add_children(popup)
530537

531538
self._template = Template(u"""
532539
{% macro script(this, kwargs) %}
@@ -544,7 +551,7 @@ def __init__(self, locations, color=None, weight=None,
544551

545552
class MultiPolyLine(MacroElement):
546553
def __init__(self, locations, color=None, weight=None,
547-
opacity=None, latlon=True):
554+
opacity=None, latlon=True, popup=None):
548555
"""
549556
Creates a MultiPolyLine object to append into a map with
550557
Map.add_children.
@@ -561,6 +568,8 @@ def __init__(self, locations, color=None, weight=None,
561568
or not ([[lon, lat]] if False).
562569
Note that the default GeoJson format is latlon=False,
563570
while Leaflet polyline's default is latlon=True.
571+
popup: string or folium.Popup, default None
572+
Input text or visualization for object.
564573
"""
565574
super(MultiPolyLine, self).__init__()
566575
self._name = 'MultiPolyLine'
@@ -569,6 +578,10 @@ def __init__(self, locations, color=None, weight=None,
569578
self.color = color
570579
self.weight = weight
571580
self.opacity = opacity
581+
if isinstance(popup, text_type) or isinstance(popup, binary_type):
582+
self.add_children(Popup(popup))
583+
elif popup is not None:
584+
self.add_children(popup)
572585

573586
self._template = Template(u"""
574587
{% macro script(this, kwargs) %}

folium/map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ def __init__(self, location, popup=None, icon=None):
374374
375375
Example
376376
-------
377-
>>>map.simple_marker(location=[45.5, -122.3], popup='Portland, OR')
378-
>>>map.simple_marker(location=[45.5, -122.3], popup=folium.Popup('Portland, OR'))
377+
>>> Marker(location=[45.5, -122.3], popup='Portland, OR')
378+
>>> Marker(location=[45.5, -122.3], popup=folium.Popup('Portland, OR'))
379379
"""
380380
super(Marker, self).__init__()
381381
self._name = 'Marker'

tests/test_features.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ def test_marker_popups():
8989
features.CircleMarker([45,120],popup=Popup('120')).add_to(m)
9090
m._repr_html_()
9191

92+
def test_polyline_popups():
93+
m = folium.Map([43,-100], zoom_start=4)
94+
features.PolyLine([[40,-80],[45,-80]], popup="PolyLine").add_to(m)
95+
features.PolyLine([[40,-90],[45,-90]], popup=Popup("PolyLine")).add_to(m)
96+
features.MultiPolyLine([[[40,-110],[45,-110]]], popup="MultiPolyLine").add_to(m)
97+
features.MultiPolyLine([[[40,-120],[45,-120]]], popup=Popup("MultiPolyLine")).add_to(m)
98+
m._repr_html_()
99+
92100
# DivIcon.
93101
def test_divicon():
94102
html = """<svg height="100" width="100">

0 commit comments

Comments
 (0)