Interactive SVG map components for Shiny for Python
Project description
shinymap (Python)
Shiny for Python adapter for the core shinymap renderer. It bundles the prebuilt JS assets and exposes helpers to drop maps into Shiny apps without touching React.
Installation
pip install shinymap
Or with uv:
uv add shinymap
API
from shinymap import Map, input_map, output_map, render_map, aes
from shinymap import scale_sequential, scale_qualitative
from shinymap.mode import Single, Multiple, Cycle, Count
from shinymap.geometry import Geometry
Map Components
input_map(id, geometry, mode, ...)renders an interactive input.- Mode classes (v0.2.0):
Single()ormode="single": returnsstr | NoneMultiple()ormode="multiple": returnslist[str]Cycle(n=4): cycles through n states, returnsdict[str, int]Count()orCount(max=10): counting mode, returnsdict[str, int]
- Aesthetics via
aesparameter:aes.ByState(base=..., hover=..., select=...): state-based stylingaes.ByGroup(__all=..., region_id=...): per-region stylingaes.Indexed(fill_color=[...]): indexed colors for cycle/count modes
- Mode classes (v0.2.0):
output_map("map", geometry, ...)adds a placeholder with static parameters.Mapprovides a fluent API for building map payloads with method chaining.render_mapdecorator serializes aMapand mounts the React output map.scale_sequential()andscale_qualitative()generate fill color maps.
Geometry Utilities
The shinymap.geometry subpackage provides tools for working with SVG geometry:
Geometry.from_svg(svg_path): Extract geometry from SVG files (v1.x polymorphic elements)Geometry.from_json(json_path): Load geometry from shinymap JSON filesgeo.relabel({...}): Rename or merge regionsgeo.set_overlays([...]): Mark overlay regionsgeo.path_as_line("_dividers"): Mark regions as lines for stroke-only renderinggeo.to_json(path): Export to JSON file
Polymorphic elements (v0.2.0): Circle, Rect, Ellipse, Path, Polygon, Line, Text
Interactive converter app:
uv run python -m shinymap.geometry.converter -b
Minimal example
from shiny import App, ui
from shinymap import Map, input_map, output_map, render_map, scale_sequential, aes
from shinymap.mode import Count
DEMO_GEOMETRY = {
"circle": ["M25,50 A20,20 0 1 1 24.999,50 Z"],
"square": ["M10 10 H40 V40 H10 Z"],
"triangle": ["M75 70 L90 40 L60 40 Z"],
}
TOOLTIPS = {"circle": "Circle", "square": "Square", "triangle": "Triangle"}
app_ui = ui.page_fluid(
ui.h2("shinymap demo"),
ui.layout_columns(
input_map(
"region",
DEMO_GEOMETRY,
tooltips=TOOLTIPS,
mode="single", # Returns str | None
),
output_map("summary"),
),
ui.br(),
ui.h4("Counts"),
ui.layout_columns(
input_map(
"clicks",
DEMO_GEOMETRY,
tooltips=TOOLTIPS,
mode=Count(), # Returns dict[str, int]
),
output_map("counts"),
),
)
def server(input, output, session):
@render_map
def summary():
selected = input.region()
return (
Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
.with_active(selected)
)
@render_map
def counts():
counts_data = input.clicks() or {}
return (
Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
.with_fill_color(scale_sequential(counts_data, list(DEMO_GEOMETRY.keys()), max_count=10))
.with_counts(counts_data)
)
app = App(app_ui, server)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shinymap-0.2.1.tar.gz.
File metadata
- Download URL: shinymap-0.2.1.tar.gz
- Upload date:
- Size: 262.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7300b78ec30d157462b9ff4a481d72ccfeb6611b08ee57aacbc8bf167a43d097
|
|
| MD5 |
6f035ccafd463e781d287ee7b91b9387
|
|
| BLAKE2b-256 |
26976415458ce6f4e9be4374896e5651f4c3d0c327868735d603569d66e427d6
|
File details
Details for the file shinymap-0.2.1-py3-none-any.whl.
File metadata
- Download URL: shinymap-0.2.1-py3-none-any.whl
- Upload date:
- Size: 279.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1f1de6b9244c2fc77a8a459ed752808661de4958feea09d0ff430dcc1c99028
|
|
| MD5 |
408b51df79fc60d598512b127fd82193
|
|
| BLAKE2b-256 |
cb40588ae78260f36770d7b8028b09202b1bd1484d0f899ec739880a0d566b83
|