Skip to content

Commit 509305e

Browse files
committed
Squash commits.
2 parents 0e2f3c9 + 15b0d20 commit 509305e

File tree

1 file changed

+32
-79
lines changed

1 file changed

+32
-79
lines changed

folium/features.py

Lines changed: 32 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -799,85 +799,39 @@ def get_bounds(self):
799799
]
800800

801801

802-
class GeoJsonTooltip(Tooltip):
803-
"""
804-
Create a tooltip that uses data from either geojson or topojson.
805-
806-
Parameters
807-
----------
808-
fields: list or tuple.
809-
Labels of GeoJson/TopoJson 'properties' or GeoPandas GeoDataFrame
810-
columns you'd like to display.
811-
aliases: list/tuple of strings, same length/order as fields, default None.
812-
Optional aliases you'd like to display in the tooltip as field name
813-
instead of the keys of `fields`.
814-
labels: bool, default True.
815-
Set to False to disable displaying the field names or aliases.
816-
localize: bool, default False.
817-
This will use JavaScript's .toLocaleString() to format 'clean' values
818-
as strings for the user's location; i.e. 1,000,000.00 comma separators,
819-
float truncation, etc.
820-
Available for most of JavaScript's primitive types (any data you'll
821-
serve into the template).
822-
style: str, default None.
823-
HTML inline style properties like font and colors. Will be applied to
824-
a div with the text in it.
825-
sticky: bool, default True
826-
Whether the tooltip should follow the mouse.
827-
**kwargs: Assorted.
828-
These values will map directly to the Leaflet Options. More info
829-
available here: https://leafletjs.com/reference-1.5.1#tooltip
802+
class GeoJsonDetail(MacroElement):
830803

831-
Examples
832-
--------
833-
# Provide fields and aliases, with Style.
834-
>>> GeoJsonTooltip(
835-
>>> fields=['CNTY_NM', 'census-pop-2015', 'census-md-income-2015'],
836-
>>> aliases=['County', '2015 Census Population', '2015 Median Income'],
837-
>>> localize=True,
838-
>>> style=('background-color: grey; color: white; font-family:'
839-
>>> 'courier new; font-size: 24px; padding: 10px;')
840-
>>> )
841-
# Provide fields, with labels off and fixed tooltip positions.
842-
>>> GeoJsonTooltip(fields=('CNTY_NM',), labels=False, sticky=False)
843804
"""
844-
_template = Template(u"""
845-
{% macro script(this, kwargs) %}
846-
{{ this._parent.get_name() }}.bindTooltip(
847-
function(layer){
848-
// Convert non-primitive to String.
849-
let handleObject = (feature)=>typeof(feature)=='object' ? JSON.stringify(feature) : feature;
850-
let fields = {{ this.fields|tojson }};
851-
{%- if this.aliases %}
852-
let aliases = {{ this.aliases|tojson }};
853-
{%- endif %}
854-
return '<table{% if this.style %} style={{ this.style|tojson }}{% endif%}>' +
855-
String(
856-
fields.map(
857-
columnname=>
858-
`<tr style="text-align: left;">{% if this.labels %}
859-
<th style="padding: 4px; padding-right: 10px;">{% if this.aliases %}
860-
${aliases[fields.indexOf(columnname)]
861-
{% if this.localize %}.toLocaleString(){% endif %}}
862-
{% else %}
863-
${ columnname{% if this.localize %}.toLocaleString(){% endif %}}
864-
{% endif %}</th>
865-
{% endif %}
866-
<td style="padding: 4px;">${handleObject(layer.feature.properties[columnname])
867-
{% if this.localize %}.toLocaleString(){% endif %}}</td></tr>`
868-
).join(''))
869-
+'</table>'
870-
}, {{ this.options|tojson }});
871-
{% endmacro %}
872-
""")
805+
Base class for GeoJsonTooltip and GeoJsonPopup to inherit methods and
806+
template structure from. Not for direct usage.
873807
874-
def __init__(self, fields, aliases=None, labels=True,
875-
localize=False, style=None, sticky=True, **kwargs):
876-
super(GeoJsonTooltip, self).__init__(
877-
text='', style=style, sticky=sticky, **kwargs
878-
)
879-
self._name = 'GeoJsonTooltip'
808+
"""
809+
base_template = u"""
810+
function(layer){
811+
let div = L.DomUtil.create('div');
812+
{% if this.fields %}
813+
let handleObject = feature=>typeof(feature)=='object' ? JSON.stringify(feature) : feature;
814+
let fields = {{ this.fields | tojson | safe }};
815+
let aliases = {{ this.aliases | tojson | safe }};
816+
let table = '<table>' +
817+
String(
818+
fields.map(
819+
(v,i)=>
820+
`<tr>{% if this.labels %}
821+
<th>${aliases[i]{% if this.localize %}.toLocaleString(){% endif %}}</th>
822+
{% endif %}
823+
<td>${handleObject(layer.feature.properties[v]){% if this.localize %}.toLocaleString(){% endif %}}</td>
824+
</tr>`).join(''))
825+
+'</table>';
826+
div.innerHTML=table;
827+
{% endif %}
828+
return div
829+
}
830+
"""
880831

832+
def __init__(self, fields, aliases=None, labels=True, localize=False, style=None,
833+
class_name="geojsondetail"):
834+
super(GeoJsonDetail, self).__init__()
881835
assert isinstance(fields, (list, tuple)), 'Please pass a list or ' \
882836
'tuple to fields.'
883837
if aliases is not None:
@@ -886,13 +840,12 @@ def __init__(self, fields, aliases=None, labels=True,
886840
' the same length.'
887841
assert isinstance(labels, bool), 'labels requires a boolean value.'
888842
assert isinstance(localize, bool), 'localize must be bool.'
889-
assert 'permanent' not in kwargs, 'The `permanent` option does not ' \
890-
'work with GeoJsonTooltip.'
891-
843+
self._name = "GeoJsonDetail"
892844
self.fields = fields
893-
self.aliases = aliases
845+
self.aliases = aliases if aliases is not None else fields
894846
self.labels = labels
895847
self.localize = localize
848+
self.class_name = class_name
896849
if style:
897850
assert isinstance(style, str), \
898851
'Pass a valid inline HTML style property string to style.'

0 commit comments

Comments
 (0)