From 7c7106e8772fae90ab4f0ff381433aa0b0d5e008 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Mon, 29 Jan 2024 09:49:47 +0100 Subject: [PATCH] Add new Renderer class --- .examples/shiny_express/app.py | 10 +++++++++ .../examples/every_person_in_manhattan/app.py | 5 ++++- maplibre/server.py | 2 ++ maplibre/shiny.py | 14 ++++++++++++ maplibre/ui.py | 7 ++++-- poetry.lock | 22 +++++++++---------- pyproject.toml | 2 +- 7 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 .examples/shiny_express/app.py create mode 100644 maplibre/shiny.py diff --git a/.examples/shiny_express/app.py b/.examples/shiny_express/app.py new file mode 100644 index 00000000..32c5abeb --- /dev/null +++ b/.examples/shiny_express/app.py @@ -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() diff --git a/docs/examples/every_person_in_manhattan/app.py b/docs/examples/every_person_in_manhattan/app.py index 89ea6278..7508d5a7 100644 --- a/docs/examples/every_person_in_manhattan/app.py +++ b/docs/examples/every_person_in_manhattan/app.py @@ -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 @@ -69,7 +72,7 @@ def create_map() -> Map: def server(input, output, session): - @render_maplibregl + @render_maplibre def maplibre(): return create_map() diff --git a/maplibre/server.py b/maplibre/server.py index 72a7b24a..78ff3a21 100644 --- a/maplibre/server.py +++ b/maplibre/server.py @@ -1,3 +1,5 @@ +### DEPRECATED: see 'shiny.py' + from __future__ import annotations from shiny.render.transformer import ( diff --git a/maplibre/shiny.py b/maplibre/shiny.py new file mode 100644 index 00000000..3f0eaf26 --- /dev/null +++ b/maplibre/shiny.py @@ -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()} diff --git a/maplibre/ui.py b/maplibre/ui.py index 05727826..e956867c 100644 --- a/maplibre/ui.py +++ b/maplibre/ui.py @@ -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 @@ -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" diff --git a/poetry.lock b/poetry.lock index 51db4cb3..8c61fb06 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1940,14 +1940,14 @@ test = ["pytest", "pytest-cov"] [[package]] name = "shiny" -version = "0.6.1.1" +version = "0.7.0" description = "A web development framework for Python." category = "main" optional = false python-versions = ">=3.8" files = [ - {file = "shiny-0.6.1.1-py3-none-any.whl", hash = "sha256:dfc4296ef2c9f35ad33cf14337c1d8e1da31958c32c3faf9d294514275133768"}, - {file = "shiny-0.6.1.1.tar.gz", hash = "sha256:42c4cf690bfd4d666e523e876ddd7afce1208516c5c4816663a12e5889f58292"}, + {file = "shiny-0.7.0-py3-none-any.whl", hash = "sha256:1de66c06a8e89ffd3cd6dcb49104c2ba2744e3043423ff2f85b97fdb34fa35ed"}, + {file = "shiny-0.7.0.tar.gz", hash = "sha256:d7eb78989259297eb2c3247dc9df16846890480526753a36e0505b881861e487"}, ] [package.dependencies] @@ -1960,16 +1960,16 @@ markdown-it-py = ">=1.1.0" mdit-py-plugins = ">=0.3.0" python-multipart = "*" questionary = {version = ">=2.0.0", markers = "platform_system != \"Emscripten\""} -starlette = ">=0.17.1" +starlette = ">=0.17.1,<0.35.0" typing-extensions = ">=4.0.1" uvicorn = ">=0.16.0" watchfiles = {version = ">=0.18.0", markers = "platform_system != \"Emscripten\""} websockets = ">=10.0" [package.extras] -dev = ["black (>=23.1.0)", "flake8 (>=6.0.0)", "flake8-bugbear (>=23.2.13)", "isort (>=5.10.1)", "matplotlib", "numpy", "pandas", "pandas-stubs", "pre-commit (>=2.15.0)", "pyright (>=1.1.334)", "shinyswatch (>=0.2.4)", "wheel"] -doc = ["griffe (==0.33.0)", "jupyter", "jupyter-client (<8.0.0)", "pydantic (==1.10)", "quartodoc (==0.7.2)", "shinylive (==0.1.1)", "tabulate"] -test = ["astropy", "bokeh", "coverage", "duckdb", "geodatasets", "geopandas", "holoviews", "ipyleaflet", "missingno", "plotly", "plotnine", "psutil", "pytest (>=6.2.4)", "pytest-asyncio (>=0.17.2)", "pytest-cov", "pytest-playwright (>=0.3.0)", "pytest-rerunfailures", "pytest-timeout", "pytest-xdist", "rsconnect-python", "scikit-learn", "seaborn", "shinywidgets", "suntime", "syrupy", "timezonefinder", "xarray"] +dev = ["black (>=23.1.0)", "flake8 (>=6.0.0)", "flake8-bugbear (>=23.2.13)", "isort (>=5.10.1)", "matplotlib", "numpy", "pandas", "pandas-stubs", "pre-commit (>=2.15.0)", "pyright (>=1.1.348)", "shinyswatch (>=0.2.4)", "wheel"] +doc = ["griffe (==0.33.0)", "jupyter", "jupyter-client (<8.0.0)", "pydantic (==1.10)", "quartodoc (==0.7.2)", "shinylive", "tabulate"] +test = ["astropy", "bokeh", "coverage", "duckdb", "folium", "geodatasets", "geopandas", "holoviews", "ipyleaflet", "missingno", "plotly", "plotnine", "psutil", "pytest (>=6.2.4)", "pytest-asyncio (>=0.17.2)", "pytest-cov", "pytest-playwright (>=0.3.0)", "pytest-rerunfailures", "pytest-timeout", "pytest-xdist", "rsconnect-python", "scikit-learn", "seaborn", "shinywidgets", "suntime", "syrupy", "timezonefinder", "xarray"] [[package]] name = "six" @@ -2017,14 +2017,14 @@ tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] [[package]] name = "starlette" -version = "0.35.1" +version = "0.34.0" description = "The little ASGI library that shines." category = "main" optional = false python-versions = ">=3.8" files = [ - {file = "starlette-0.35.1-py3-none-any.whl", hash = "sha256:50bbbda9baa098e361f398fda0928062abbaf1f54f4fadcbe17c092a01eb9a25"}, - {file = "starlette-0.35.1.tar.gz", hash = "sha256:3e2639dac3520e4f58734ed22553f950d3f3cb1001cd2eaac4d57e8cdc5f66bc"}, + {file = "starlette-0.34.0-py3-none-any.whl", hash = "sha256:2e14ee943f2df59eb8c141326240ce601643f1a97b577db44634f6d05d368c37"}, + {file = "starlette-0.34.0.tar.gz", hash = "sha256:ed050aaf3896945bfaae93bdf337e53ef3f29115a9d9c153e402985115cd9c8e"}, ] [package.dependencies] @@ -2394,4 +2394,4 @@ all = ["geopandas", "pandas"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4" -content-hash = "47ba10b55a403b30b258b7298c519ac5c9e0a4b5c611c578f0d3a665233d7d88" +content-hash = "e6a75af45f029a585627b63d2d75ae99e4fb1061602d07600811ac98494d232d" diff --git a/pyproject.toml b/pyproject.toml index af4a8927..17fee517 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"