Skip to content

Commit

Permalink
Support mustache templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Jan 24, 2024
1 parent 803c4ea commit 0571b89
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/examples/geopandas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
def create_map():
m = Map(map_options)
m.add_layer(wilderness_layer)
m.add_tooltip(LAYER_ID, "NAME")
# m.add_tooltip(LAYER_ID, "NAME", template="NAME: {{ NAME }}")
# m.add_tooltip(LAYER_ID)
# m.add_popup(LAYER_ID, "NAME")
m.add_popup(LAYER_ID, template="<b>Name:</b> {{ NAME }}")
return m


Expand Down
12 changes: 8 additions & 4 deletions maplibre/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,27 @@ def add_marker(self, marker: Marker) -> None:
"""
self.add_call("addMarker", marker.to_dict())

def add_popup(self, layer_id: str, prop: str = None) -> None:
def add_popup(self, layer_id: str, prop: str = None, template: str = None) -> None:
"""Add a popup to the map
Args:
layer_id (str): The layer to which the popup is added.
prop (str): The property of the source to be displayed. If `None`, all properties are displayed.
template (str): A mustache template. If supplied, `prop` is ignored.
"""
self.add_call("addPopup", layer_id, prop)
self.add_call("addPopup", layer_id, prop, template)

def add_tooltip(self, layer_id: str, prop: str = None) -> None:
def add_tooltip(
self, layer_id: str, prop: str = None, template: str = None
) -> None:
"""Add a tooltip to the map
Args:
layer_id (str): The layer to which the tooltip is added.
prop (str): The property of the source to be displayed. If `None`, all properties are displayed.
template (str): A mustache template. If supplied, `prop` is ignored.
"""
self.add_call("addTooltip", layer_id, prop)
self.add_call("addTooltip", layer_id, prop, template)

def set_filter(self, layer_id: str, filter_: list):
"""Update the filter of a layer
Expand Down
13 changes: 12 additions & 1 deletion maplibre/srcjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"devDependencies": {
"esbuild": "0.19.10",
"prettier": "3.1.1"
},
"dependencies": {
"mustache": "^4.2.0"
}
}
15 changes: 10 additions & 5 deletions srcjs/pymaplibregl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import mustache from "mustache";
// import { getCustomMapMethods } from "./mapmethods";

function getTextFromFeature(feature, property) {
function getTextFromFeature(feature, property, template) {
if (template !== null) {
return mustache.render(template, feature.properties);
}

if (property === null) {
const text = Object.keys(feature.properties)
.map((key) => `${key}: ${feature.properties[key]}`)
Expand Down Expand Up @@ -70,28 +75,28 @@ export default class PyMapLibreGL {
}
}

addPopup(layerId, property) {
addPopup(layerId, property, template = null) {
const popupOptions = {
closeButton: false,
};
const popup = new maplibregl.Popup(popupOptions);
this._map.on("click", layerId, (e) => {
const feature = e.features[0];
// const text = feature.properties[property];
const text = getTextFromFeature(feature, property);
const text = getTextFromFeature(feature, property, template);
popup.setLngLat(e.lngLat).setHTML(text).addTo(this._map);
});
}

addTooltip(layerId, property) {
addTooltip(layerId, property, template = null) {
const popupOptions = {
closeButton: false,
closeOnClick: false,
};
const popup = new maplibregl.Popup(popupOptions);
this._map.on("mousemove", layerId, (e) => {
const feature = e.features[0];
const text = getTextFromFeature(feature, property);
const text = getTextFromFeature(feature, property, template);
popup.setLngLat(e.lngLat).setHTML(text).addTo(this._map);
});

Expand Down

0 comments on commit 0571b89

Please sign in to comment.