Skip to main content

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() or mode="single": returns str | None
      • Multiple() or mode="multiple": returns list[str]
      • Cycle(n=4): cycles through n states, returns dict[str, int]
      • Count() or Count(max=10): counting mode, returns dict[str, int]
    • Aesthetics via aes parameter:
      • aes.ByState(base=..., hover=..., select=...): state-based styling
      • aes.ByGroup(__all=..., region_id=...): per-region styling
      • aes.Indexed(fill_color=[...]): indexed colors for cycle/count modes
  • output_map("map", geometry, ...) adds a placeholder with static parameters.
  • Map provides a fluent API for building map payloads with method chaining.
  • render_map decorator serializes a Map and mounts the React output map.
  • scale_sequential() and scale_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 files
  • geo.relabel({...}): Rename or merge regions
  • geo.set_overlays([...]): Mark overlay regions
  • geo.path_as_line("_dividers"): Mark regions as lines for stroke-only rendering
  • geo.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shinymap-0.2.1.tar.gz (262.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shinymap-0.2.1-py3-none-any.whl (279.5 kB view details)

Uploaded Python 3

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

Hashes for shinymap-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7300b78ec30d157462b9ff4a481d72ccfeb6611b08ee57aacbc8bf167a43d097
MD5 6f035ccafd463e781d287ee7b91b9387
BLAKE2b-256 26976415458ce6f4e9be4374896e5651f4c3d0c327868735d603569d66e427d6

See more details on using hashes here.

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

Hashes for shinymap-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1f1de6b9244c2fc77a8a459ed752808661de4958feea09d0ff430dcc1c99028
MD5 408b51df79fc60d598512b127fd82193
BLAKE2b-256 cb40588ae78260f36770d7b8028b09202b1bd1484d0f899ec739880a0d566b83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page