Skip to content

Commit

Permalink
Add method to update deckgl layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed May 31, 2024
1 parent 28d8fcb commit d27435b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
77 changes: 77 additions & 0 deletions docs/examples/deckgl_layer/app_update_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Shiny Express App

import json

from maplibre import Map, MapContext, MapOptions, render_maplibregl
from maplibre.basemaps import Carto
from maplibre.ui import use_deckgl
from shiny import reactive
from shiny.express import input, render, ui

m = Map(
MapOptions(
style=Carto.POSITRON,
center=(-122.4, 37.74),
zoom=12,
hash=True,
pitch=40,
)
)

layer_id = "GridLayer"

CELL_SIZES = [100, 200, 300]
DEFAULT_CELL_SIZE = CELL_SIZES[1]


def deck_grid_layer(cell_size: int = DEFAULT_CELL_SIZE):
return {
"@@type": layer_id,
"id": "GridLayer",
"data": "https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/sf-bike-parking.json",
"extruded": True,
"getPosition": "@@=COORDINATES",
"getColorWeight": "@@=SPACES",
"getElevationWeight": "@@=SPACES",
"elevationScale": 4,
"cellSize": cell_size,
"pickable": True,
}


m.add_deck_layers([deck_grid_layer()], tooltip_template="Number of points: {{ count }}")

# Shiny Express
use_deckgl()

ui.input_select(
"cell_size", "Cell size", choices=CELL_SIZES, selected=DEFAULT_CELL_SIZE
)
ui.input_action_button("update_layer", "Update layer")


@render_maplibregl
def render_map():
return m


@render.code
def picking_object():
obj = input.render_map_layer_GridLayer()
print(obj)
# return json.dumps(obj, indent=2) if obj else "Pick a feature!"
# return f"{obj['count']}" if obj else "Pick a feature!"
return json.dumps(obj["points"], indent=2) if obj else "Pick a feature!"


@reactive.Effect
@reactive.event(input.update_layer)
async def update_layer():
print(input.update_layer())
async with MapContext("render_map") as m:
m.set_deck_layers([deck_grid_layer(input.cell_size())])


if __name__ == "__main__":
with open("docs/examples/deckgl_layer/app.html", "w") as f:
f.write(m.to_html())
4 changes: 4 additions & 0 deletions maplibre/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,8 @@ def to_html(self, title: str = "My Awesome Map", **kwargs) -> str:
def add_deck_layers(
self, layers: list[dict], tooltip_template: str | dict = None
) -> None:
"""Add Deck.GL layers to the layer stack"""
self.add_call("addDeckOverlay", layers, tooltip_template)

def set_deck_layers(self, layers: list[dict]):
self.add_call("setDeckLayers", layers)
Loading

0 comments on commit d27435b

Please sign in to comment.