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, MapPayload, input_map, output_map, render_map
from shinymap import scale_sequential, scale_qualitative, SEQUENTIAL, QUALITATIVE
  • input_map(id, geometry, mode="single"|"multiple"|"count", cycle=None, max_selection=None, hover_highlight=None, ...) renders an interactive input.
    • For mode="single": returns a single selected region ID (string) or None
    • For mode="multiple": returns a list of selected region IDs
    • For mode="count": returns a dict mapping region IDs to counts
    • hover_highlight accepts a dict with keys: stroke_width, fill_opacity, stroke_color, fill_color for customizing hover effects
  • output_map("map") adds a placeholder in your UI; pair it with a @render_map output in the server.
  • Map (alias for MapBuilder) provides a fluent API for building map payloads with method chaining.
  • MapPayload models the data you can send to an output map: geometry, tooltips, fills, counts, active ids, default aesthetics, etc.
  • render_map is a convenience decorator that serializes a Map/MapPayload (or dict) and mounts the React output map.
  • scale_sequential(counts, region_ids, max_count=None) and scale_qualitative(categories, region_ids, palette=None) are helper functions for generating fill color maps.

Minimal example

from shiny import App, ui
from shinymap import Map, input_map, output_map, render_map, scale_sequential

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",
            hover_highlight={"stroke_width": 1},
        ),
        output_map("summary"),
    ),
    ui.br(),
    ui.h4("Counts"),
    ui.layout_columns(
        input_map(
            "clicks",
            DEMO_GEOMETRY,
            tooltips=TOOLTIPS,
            mode="count",
            hover_highlight={"stroke_width": 2, "fill_opacity": -0.3},
        ),
        output_map("counts"),
    ),
)


def server(input, output, session):
    @render_map
    def summary():
        # mode="single" returns a single ID (string) or None
        selected = input.region()
        return (
            Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
            .with_active(selected)
            .with_stroke_width(1.5)
        )

    @render_map
    def counts():
        # mode="count" returns a dict mapping region IDs to counts
        counts_data = input.clicks() or {}
        return (
            Map(DEMO_GEOMETRY, tooltips=TOOLTIPS)
            .with_fills(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.1.0a1.tar.gz (198.1 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.1.0a1-py3-none-any.whl (201.3 kB view details)

Uploaded Python 3

File details

Details for the file shinymap-0.1.0a1.tar.gz.

File metadata

  • Download URL: shinymap-0.1.0a1.tar.gz
  • Upload date:
  • Size: 198.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for shinymap-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 b1b45c76c996545eb103de178fce312c1a6f13a15c4b387275ca56fa8328c00d
MD5 f12bc7db5a95b14f43b33f1d46730e2b
BLAKE2b-256 dec0e484ed1f2bc685b3e88ab4a645d793d792494be3f528c9d779834c661e7d

See more details on using hashes here.

File details

Details for the file shinymap-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: shinymap-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 201.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for shinymap-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 653b1e27fc6a42381fffe81a886c58a1adef53ef72730c6da7357728ed0d540c
MD5 5063aef5db3e4a7fd4f86d8f5b99743a
BLAKE2b-256 3186071ace8501143e1b9bfc2b48e27c41cdbe41c10a3f24a0ef47897ddae269

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