Skip to content

Adding popup to lines, test_line #122

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 3 commits into from
May 13, 2015
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
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.1.5
~~~~~
- Popups on lines. (themiurgo)
- `fit_bounds` method. (themiurgo)
- GeoJSON popup. (ocefpaf 7aad5e0)
- Added cartodb positron and dark_matter tiles (ocefpaf d4daee7)
Expand Down
21 changes: 21 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ The popup parameter in any marker can be passed a `Vincent <https://github.com/w
vis.to_json('vis.json')
map.polygon_marker(location=[45.5, -122.5], popup=(vis, 'vis.json'))

Other features
--------------

Polyline
~~~~~~~~

You can plot a line by simply passing an iterable of coordinates to the method `line`::

map.line(locations=[[45.3288, -121.6625],
[45.324224, -121.657763]
[45.318702, -121.652871]])

You can specify the following parameters:

- `line_color`: line color, either a simple color (blue, black, etc), or a hex string
- `line_weight`: line weight, in pixels
- `line_opacity`: fill opacity

Polylines also support popups, through the `popup` and `popup_on` parameters, similarly to markers.


Data Mapping: GeoJSON and TopoJSON
----------------------------------

Expand Down
16 changes: 13 additions & 3 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True,

@iter_obj('line')
def line(self, locations,
line_color=None, line_opacity=None, line_weight=None):
line_color=None, line_opacity=None, line_weight=None,
popup='Pop Text', popup_on=True, popup_width=300):
"""Add a line to the map with optional styles.

Parameters
Expand All @@ -373,6 +374,11 @@ def line(self, locations,
line_color: string, default Leaflet's default ('#03f')
line_opacity: float, default Leaflet's default (0.5)
line_weight: float, default Leaflet's default (5)
popup: string or tuple, default 'Pop Text'
Input text or visualization for object. Can pass either text,
or a tuple of the form (Vincent object, 'vis_path.json')
popup_on: boolean, default True
Pass false for no popup information on the marker

Note: If the optional styles are omitted, they will not be included
in the HTML output and will obtain the Leaflet defaults listed above.
Expand All @@ -396,9 +402,13 @@ def line(self, locations,
'locations': locations,
'options': polyline_opts})

popup_out = self._popup_render(popup=popup, mk_name='line_',
count=count, popup_on=popup_on,
width=popup_width)

add_line = 'map.addLayer({});'.format(varname)
self.template_vars.setdefault('lines', []).append((line_rendered,
add_line))
append = (line_rendered, popup_out, add_line)
self.template_vars.setdefault('lines', []).append((append))

@iter_obj('multiline')
def multiline(self, locations, line_color=None, line_opacity=None,
Expand Down
3 changes: 2 additions & 1 deletion folium/templates/fol_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@
{{ add_mark }}
{% endfor %}

{% for line, add_line in lines %}
{% for line, popup, add_line in lines %}
{{ line }}
{{ popup }}
{{ add_line }}
{% endfor %}

Expand Down
3 changes: 2 additions & 1 deletion folium/templates/geojson_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@
{{ add_mark }}
{% endfor %}

{% for line, add_line in lines %}
{% for line, popup, add_line in lines %}
{{ line }}
{{ popup }}
{{ add_line }}
{% endfor %}

Expand Down
3 changes: 2 additions & 1 deletion folium/templates/ipynb_geojson_repr.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
{{ add_mark }}
{% endfor %}

{% for line, add_line in lines %}
{% for line, popup, add_line in lines %}
{{ line }}
{{ popup }}
{{ add_line }}
{% endfor %}

Expand Down
3 changes: 2 additions & 1 deletion folium/templates/ipynb_repr.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
{{ add_mark }}
{% endfor %}

{% for line, add_line in lines %}
{% for line, popup, add_line in lines %}
{{ line }}
{{ popup }}
{{ add_line }}
{% endfor %}

Expand Down
24 changes: 24 additions & 0 deletions tests/folium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,30 @@ def test_create_map(self):
# Test write
map.create_map()

def test_line(self):
'''Test multi_polyline'''

line_temp = self.env.get_template('polyline.js')

line_opts = {
'color': 'blue',
'weight': 2,
'opacity': 1
}
locations = [
[[45.5236, -122.6750], [45.5236, -122.6751]],
[[45.5237, -122.6750], [45.5237, -122.6751]],
[[45.5238, -122.6750], [45.5238, -122.6751]]
]
line_rendered = line_temp.render({'line': 'line_1',
'locations': locations, 'options': line_opts})

self.map.line(locations=locations,
line_color=line_opts['color'],
line_weight=line_opts['weight'],
line_opacity=line_opts['opacity'])
assert self.map.template_vars['lines'][0][0] == line_rendered

def test_multi_polyline(self):
'''Test multi_polyline'''

Expand Down