Skip to content

Commit

Permalink
Add new Renderer class
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Jan 29, 2024
1 parent 6f09cf9 commit 7c7106e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
10 changes: 10 additions & 0 deletions .examples/shiny_express/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from maplibre import Map
from maplibre.shiny import render_maplibre
from shiny.express import ui

ui.h1("Hello world!")


@render_maplibre
def mapylibre():
return Map()
5 changes: 4 additions & 1 deletion docs/examples/every_person_in_manhattan/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
)
from maplibre.basemaps import Carto
from maplibre.controls import ScaleControl

# New since Shiny v0.7.0
from maplibre.shiny import render_maplibre
from maplibre.sources import GeoJSONSource
from maplibre.utils import df_to_geojson
from shiny import App, reactive, ui
Expand Down Expand Up @@ -69,7 +72,7 @@ def create_map() -> Map:


def server(input, output, session):
@render_maplibregl
@render_maplibre
def maplibre():
return create_map()

Expand Down
2 changes: 2 additions & 0 deletions maplibre/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### DEPRECATED: see 'shiny.py'

from __future__ import annotations

from shiny.render.transformer import (
Expand Down
14 changes: 14 additions & 0 deletions maplibre/shiny.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from htmltools import Tag

from shiny.render.renderer import Renderer

from .map import Map
from .ui import output_maplibregl


class render_maplibre(Renderer[Map]):
def auto_output_ui(self) -> Tag:
return output_maplibregl(self.output_id, height=600)

async def transform(self, value: Map) -> dict:
return {"mapData": value.to_dict()}
7 changes: 5 additions & 2 deletions maplibre/ui.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
### TODO: move to 'shiny.py'

from __future__ import annotations

from htmltools import HTMLDependency
from htmltools import HTMLDependency, Tag

from shiny import ui
from shiny.module import resolve_id

Expand All @@ -24,7 +27,7 @@
)


def output_maplibregl(id_: str, height: [int | str] = 200):
def output_maplibregl(id_: str, height: [int | str] = 200) -> Tag:
if isinstance(height, int):
height = f"{height}px"

Expand Down
22 changes: 11 additions & 11 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include = [

[tool.poetry.dependencies]
python = ">=3.9,<4"
shiny = "^0.6.1"
shiny = "0.7.0"
htmltools = "^0.5.1"
jinja2 = "^3.1.3"
pydantic = "^2.5.3"
Expand Down

0 comments on commit 7c7106e

Please sign in to comment.