-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Tooltip classes (simple and geojson/topojson) #883
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
Conversation
Added a new Tooltip class to reference GeoJson feature properties, with tests, documentation, and examples. It's flexible, and can take field names, aliases, that can be turned on/off with the 'labels' boolean, a 'sticky' property, and can incorporate Javascript's .toLocaleString() functionality if desired. The same string can be passed for each object with the 'text' variable as well if you'd rather not use the 'fields'. Also updated the GeoJson JS template to reference this Tooltip object, and added a test to make sure that the values passed exist in the properties.
folium/features.py
Outdated
@@ -416,7 +431,14 @@ def __init__(self, data, style_function=None, name=None, | |||
self.highlight = highlight_function is not None | |||
|
|||
self.highlight_function = highlight_function or (lambda x: {}) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
folium/features.py
Outdated
if self.tooltip: | ||
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), f"The value {value} is not in the available properties. For your review, they are {tuple(self.data['features'][0]['properties'].keys())}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (227 > 120 characters)
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
folium/features.py
Outdated
@@ -652,6 +674,63 @@ def __init__(self, html=None, icon_size=None, icon_anchor=None, | |||
self.className = class_name | |||
|
|||
|
|||
class Tooltip: | |||
""" | |||
Creates a Tooltip object for adding to features to display text as a property a Map by executing a javascript function when hovering the cursor over each feature. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (162 > 120 characters)
folium/features.py
Outdated
---------- | ||
fields: list or tuple. | ||
list or tuple of labels of the GeoJson 'properties' or GeoPandas GeodataFrame columns you'd like to display. | ||
aliases: list or tuple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W291 trailing whitespace
folium/features.py
Outdated
assert not all((fields,text)), "Please choose either fields or text." | ||
assert any((fields,text)), "Please choose either fields or text." | ||
assert isinstance(toLocaleString,bool), "toLocaleString must be either True or False" | ||
self.fields=fields |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E225 missing whitespace around operator
folium/features.py
Outdated
self.fields=fields | ||
self.aliases = aliases | ||
self.text = text | ||
self.labels=labels |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E225 missing whitespace around operator
folium/features.py
Outdated
self.aliases = aliases | ||
self.text = text | ||
self.labels=labels | ||
self.sticky=sticky |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E225 missing whitespace around operator
folium/features.py
Outdated
self.text = text | ||
self.labels=labels | ||
self.sticky=sticky | ||
self.toLocaleString=toLocaleString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E225 missing whitespace around operator
folium/features.py
Outdated
self.result = self.fields | ||
else: | ||
self.result = self.text | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
@jtbaker god start! Can you please add a test and, if possible, a notebook example? PS: it seems you have some linter issue there. |
folium/features.py
Outdated
if self.tooltip: | ||
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), | ||
(f"The value {value} is not in the available properties."+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E225 missing whitespace around operator
folium/features.py
Outdated
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), | ||
(f"The value {value} is not in the available properties."+ | ||
f"For your review, they are {tuple(self.data['features'][0]['properties'].keys())}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E128 continuation line under-indented for visual indent
folium/features.py
Outdated
@@ -651,6 +674,69 @@ def __init__(self, html=None, icon_size=None, icon_anchor=None, | |||
self.html = html | |||
self.className = class_name | |||
|
|||
class Tooltip: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E302 expected 2 blank lines, found 1
folium/features.py
Outdated
fields: list or tuple. | ||
list or tuple of labels of the GeoJson 'properties' or GeoPandas GeodataFrame columns you'd like to display. | ||
aliases: list or tuple | ||
list or tuple of optional 'aliases' you'd like to display the each field name as, to describe the data in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W291 trailing whitespace
folium/features.py
Outdated
if fields: | ||
assert isinstance(fields, (list, tuple)), "Please pass a list or tuple to Fields." | ||
if bool(fields) & bool(aliases): | ||
assert isinstance(aliases, (list,tuple)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E231 missing whitespace after ','
folium/features.py
Outdated
assert isinstance(fields, (list, tuple)), "Please pass a list or tuple to Fields." | ||
if bool(fields) & bool(aliases): | ||
assert isinstance(aliases, (list,tuple)) | ||
assert len(fields)==len(aliases), "Fields and Aliases must have the same length." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E225 missing whitespace around operator
Further linting.
folium/features.py
Outdated
if self.tooltip: | ||
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()),( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E231 missing whitespace after ','
folium/features.py
Outdated
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()),( | ||
f"The value {value} is not in the available properties.\n"+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
E225 missing whitespace around operator
folium/features.py
Outdated
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()),( | ||
f"The value {value} is not in the available properties.\n"+ | ||
f"For your review, they are {tuple(self.data['features'][0]['properties'].keys())}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E101 indentation contains mixed spaces and tabs
W191 indentation contains tabs
folium/features.py
Outdated
assert value in tuple(self.data['features'][0]['properties'].keys()),( | ||
f"The value {value} is not in the available properties.\n"+ | ||
f"For your review, they are {tuple(self.data['features'][0]['properties'].keys())}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E101 indentation contains mixed spaces and tabs
W191 indentation contains tabs
folium/features.py
Outdated
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), ( | ||
f"The value {value} is not in the available properties.\n" + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
W291 trailing whitespace
folium/features.py
Outdated
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), ( | ||
f"The value {value} is not in the available properties.\n" + | ||
f"For your review, they are {tuple(self.data['features'][0]['properties'].keys())}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E101 indentation contains mixed spaces and tabs
W191 indentation contains tabs
folium/features.py
Outdated
if self.tooltip: | ||
if self.tooltip.fields: | ||
for value in self.tooltip.fields: | ||
assert value in tuple(self.data['features'][0]['properties'].keys()), f"The value {value} is not in the available properties.\nFor your review, they are {tuple(self.data['features'][0]['properties'].keys())}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (228 > 120 characters)
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
@@ -651,6 +672,70 @@ def __init__(self, html=None, icon_size=None, icon_anchor=None, | |||
self.html = html | |||
self.className = class_name | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, f"The value {value} is not in the available properties.\nFor your review, they are {keys}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (132 > 120 characters)
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, f"The value {value} is not available.\nFor your review, they are {keys}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, f"The value {value} is not available. For your review, they are {keys}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, f"{value} is not available. For your review, they are {keys}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, f"{value} is not available." + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, (f"{value} is not available. " + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
folium/features.py
Outdated
if self.tooltip.fields: | ||
keys = tuple(self.data['features'][0]['properties'].keys()) | ||
for value in self.tooltip.fields: | ||
assert value in keys, (f"{value} is not available in {keys}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E999 SyntaxError: invalid syntax
Swap text to self for Tooltip -> GeoJsonTooltip inheritance.
geojson `properties`, using `JSON.stringify`. Will allow display of underlying data instead of `object Object`. Are we cool with >= ES6 syntax? It's easier to write.
folium/map.py
Outdated
@@ -186,21 +187,29 @@ class Icon(MacroElement): | |||
iconColor: '{{this.icon_color}}', | |||
markerColor: '{{this.color}}', | |||
prefix: '{{this.prefix}}', | |||
extraClasses: 'fa-rotate-{{this.angle}}' | |||
extraClasses: 'fa-rotate-{{this.angle}}', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're adding an option to Icon
, which is fine in itself but please do this in a separate PR.
folium/map.py
Outdated
super(Icon, self).__init__() | ||
self._name = 'Icon' | ||
self._name = 'AwesomeMarkers.icon' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the name could have unintended consequences so please do this in a separate PR so it doesn't go unnoticed.
folium/map.py
Outdated
self.color = color | ||
self.icon = icon | ||
self.icon_color = icon_color | ||
self.angle = angle | ||
self.prefix = prefix | ||
self.spin = spin | ||
self.options = {'icon': icon, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the way options work here doesn't belong in this PR.
folium/map.py
Outdated
|
||
def __init__(self, location, popup=None, tooltip=None, icon=None): | ||
def __init__(self, location=None, popup=None, tooltip=None, icon=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the default of location
here doesn't have to do with tooltip, please remove it here.
folium/map.py
Outdated
self.tooltip = tooltip | ||
self.location = _validate_coordinates(location) | ||
self._name = 'marker' | ||
if location: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check fails with Numpy arrays. Also adding it doesn't belong in this PR.
Reverted changes to Marker that were outside scope.
tests/test_utilities.py
Outdated
@@ -0,0 +1,12 @@ | |||
from __future__ import (absolute_import, division, print_function) | |||
|
|||
import pytest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
F401 'pytest' imported but unused
I removed the stowaway xml files, @jtbaker maybe add them to your I think we're good to go! |
@Conengmo Awesome! Thanks for your help on this. This is my first real open source contribution, and I learned quite a bit in the process - have a decent grasp on JS and lower level Python, project best practices and scoping now. Looking forward to future PRs (with less errors). :-) Since the changes have been approved, and I created this PR, am I good to close it out now? Is the merge onto the master complete, or does it need another review from @ocefpaf or someone else? |
Merged! Congrats @jtbaker and glad to hear you learned stuff. It took some time to figure out how to get this right, so I appreciate your patience on this PR. Future PRs sounds good, let's do it! |
@jtbaker thank you for your efforts. I would really appreciate an example that combines chloropleth and tooltip into one map from the same dataframe / geojson. The example in the GeoJsonTooltip class docstring just seems to call "Tooltip(" and I can't figure out where that class/function is coming from. If someone could point me in the right direction I would be happy to share a ipynb for the examples directory. thanks again. |
@Conengmo Looks like we forgot to update the naming in the docstring after the class split. Whoops! @baskoes, Thank you! It was fun, and I learned quite a bit about how the library works under the hood. Those references to I'll get a new notebook added to the examples directory to do a demo on how to use both classes. The usage would looks something like this:
|
Good catch, didn't notice that. A notebook with examples of |
Added a new Tooltip class to reference GeoJson feature properties, with tests, documentation, and examples. It's flexible, and can take field names, aliases, that can be turned on/off with the 'labels' boolean, a 'sticky' property, and can incorporate Javascript's .toLocaleString() functionality if desired. The same string can be passed for each object with the 'text' variable as well if you'd rather not use the 'fields'.
Also updated the GeoJson JS template to reference this Tooltip object, and added a test to make sure that the values passed exist in the properties.
Use case at this nbviewer