Skip to content

Commit 204d722

Browse files
committed
Merge pull request #122 from themiurgo/lines_popup
Adding popup to lines, test_line
2 parents fff78a3 + 97c0a02 commit 204d722

File tree

8 files changed

+67
-7
lines changed

8 files changed

+67
-7
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.1.5
22
~~~~~
3+
- Popups on lines. (themiurgo)
34
- `fit_bounds` method. (themiurgo)
45
- GeoJSON popup. (ocefpaf 7aad5e0)
56
- Added cartodb positron and dark_matter tiles (ocefpaf d4daee7)

docs/index.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ The popup parameter in any marker can be passed a `Vincent <https://github.com/w
158158
vis.to_json('vis.json')
159159
map.polygon_marker(location=[45.5, -122.5], popup=(vis, 'vis.json'))
160160

161+
Other features
162+
--------------
163+
164+
Polyline
165+
~~~~~~~~
166+
167+
You can plot a line by simply passing an iterable of coordinates to the method `line`::
168+
169+
map.line(locations=[[45.3288, -121.6625],
170+
[45.324224, -121.657763]
171+
[45.318702, -121.652871]])
172+
173+
You can specify the following parameters:
174+
175+
- `line_color`: line color, either a simple color (blue, black, etc), or a hex string
176+
- `line_weight`: line weight, in pixels
177+
- `line_opacity`: fill opacity
178+
179+
Polylines also support popups, through the `popup` and `popup_on` parameters, similarly to markers.
180+
181+
161182
Data Mapping: GeoJSON and TopoJSON
162183
----------------------------------
163184

folium/folium.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True,
363363

364364
@iter_obj('line')
365365
def line(self, locations,
366-
line_color=None, line_opacity=None, line_weight=None):
366+
line_color=None, line_opacity=None, line_weight=None,
367+
popup='Pop Text', popup_on=True, popup_width=300):
367368
"""Add a line to the map with optional styles.
368369
369370
Parameters
@@ -373,6 +374,11 @@ def line(self, locations,
373374
line_color: string, default Leaflet's default ('#03f')
374375
line_opacity: float, default Leaflet's default (0.5)
375376
line_weight: float, default Leaflet's default (5)
377+
popup: string or tuple, default 'Pop Text'
378+
Input text or visualization for object. Can pass either text,
379+
or a tuple of the form (Vincent object, 'vis_path.json')
380+
popup_on: boolean, default True
381+
Pass false for no popup information on the marker
376382
377383
Note: If the optional styles are omitted, they will not be included
378384
in the HTML output and will obtain the Leaflet defaults listed above.
@@ -396,9 +402,13 @@ def line(self, locations,
396402
'locations': locations,
397403
'options': polyline_opts})
398404

405+
popup_out = self._popup_render(popup=popup, mk_name='line_',
406+
count=count, popup_on=popup_on,
407+
width=popup_width)
408+
399409
add_line = 'map.addLayer({});'.format(varname)
400-
self.template_vars.setdefault('lines', []).append((line_rendered,
401-
add_line))
410+
append = (line_rendered, popup_out, add_line)
411+
self.template_vars.setdefault('lines', []).append((append))
402412

403413
@iter_obj('multiline')
404414
def multiline(self, locations, line_color=None, line_opacity=None,

folium/templates/fol_template.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@
134134
{{ add_mark }}
135135
{% endfor %}
136136

137-
{% for line, add_line in lines %}
137+
{% for line, popup, add_line in lines %}
138138
{{ line }}
139+
{{ popup }}
139140
{{ add_line }}
140141
{% endfor %}
141142

folium/templates/geojson_template.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@
103103
{{ add_mark }}
104104
{% endfor %}
105105

106-
{% for line, add_line in lines %}
106+
{% for line, popup, add_line in lines %}
107107
{{ line }}
108+
{{ popup }}
108109
{{ add_line }}
109110
{% endfor %}
110111

folium/templates/ipynb_geojson_repr.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
{{ add_mark }}
5454
{% endfor %}
5555

56-
{% for line, add_line in lines %}
56+
{% for line, popup, add_line in lines %}
5757
{{ line }}
58+
{{ popup }}
5859
{{ add_line }}
5960
{% endfor %}
6061

folium/templates/ipynb_repr.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
{{ add_mark }}
2828
{% endfor %}
2929

30-
{% for line, add_line in lines %}
30+
{% for line, popup, add_line in lines %}
3131
{{ line }}
32+
{{ popup }}
3233
{{ add_line }}
3334
{% endfor %}
3435

tests/folium_tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,30 @@ def test_create_map(self):
417417
# Test write
418418
map.create_map()
419419

420+
def test_line(self):
421+
'''Test multi_polyline'''
422+
423+
line_temp = self.env.get_template('polyline.js')
424+
425+
line_opts = {
426+
'color': 'blue',
427+
'weight': 2,
428+
'opacity': 1
429+
}
430+
locations = [
431+
[[45.5236, -122.6750], [45.5236, -122.6751]],
432+
[[45.5237, -122.6750], [45.5237, -122.6751]],
433+
[[45.5238, -122.6750], [45.5238, -122.6751]]
434+
]
435+
line_rendered = line_temp.render({'line': 'line_1',
436+
'locations': locations, 'options': line_opts})
437+
438+
self.map.line(locations=locations,
439+
line_color=line_opts['color'],
440+
line_weight=line_opts['weight'],
441+
line_opacity=line_opts['opacity'])
442+
assert self.map.template_vars['lines'][0][0] == line_rendered
443+
420444
def test_multi_polyline(self):
421445
'''Test multi_polyline'''
422446

0 commit comments

Comments
 (0)