BUG: Escape special characters in explore (#2657) (#2658)

This commit is contained in:
Xiaokang Fu
2023-01-22 12:18:38 +01:00
committed by GitHub
parent 20527da2b2
commit b2a51f65ec
3 changed files with 23 additions and 1 deletions
+1
View File
@@ -17,6 +17,7 @@ Bug fixes:
- Ensure that GeoDataFrame created from DataFrame is a copy, not a view (#2667)
- Fix mismatch between geometries and colors in ``plot()`` if an empty or missing
geometry is present (#2224)
- Escape special characters to avoid TemplateSyntaxError in ``explore()`` (#2657)
- Fix `to_parquet`/`to_feather` to not write an invalid bbox (with NaNs) in the
metadata in case of an empty GeoDataFrame (#2653)
+13 -1
View File
@@ -278,6 +278,7 @@ GON (((180.00000 -16.06713, 180.00000...
try:
import branca as bc
import folium
import re
import matplotlib
import matplotlib.colors as colors
import matplotlib.pyplot as plt
@@ -618,10 +619,21 @@ GON (((180.00000 -16.06713, 180.00000...
else:
tooltip = None
popup = None
# escape the curly braces {{}} for jinja2 templates
feature_collection = gdf.__geo_interface__
for feature in feature_collection["features"]:
for k in feature["properties"]:
# escape the curly braces in values
if type(feature["properties"][k]) == str:
feature["properties"][k] = re.sub(
r"\{{2,}",
lambda x: "{% raw %}" + x.group(0) + "{% endraw %}",
feature["properties"][k],
)
# add dataframe to map
folium.GeoJson(
gdf.__geo_interface__,
feature_collection,
tooltip=tooltip,
popup=popup,
marker=marker,
+9
View File
@@ -466,6 +466,15 @@ class TestExplore:
out_str = self._fetch_map_string(m)
assert "BoroName" in out_str
def test_escape_special_characters(self):
# check if special characters are escaped
gdf = self.world.copy()
gdf["name"] = """{{{what a mess}}} they are so different."""
m = gdf.explore()
out_str = self._fetch_map_string(m)
assert """{{{""" in out_str
assert """}}}""" in out_str
def test_default_markers(self):
# check overridden default for points
m = self.cities.explore()