Skip to main content

A Streamlit component that integrates Plotly's interactive Mapbox visualizations, enabling bidirectional communication between the map and Streamlit. It allows for seamless rendering of Mapbox plots within Streamlit applications while supporting event handling, such as click, select, hover, and relayout events, to facilitate dynamic interactions and data updates.

Project description

Streamlit Plotly MapBox Events

PyPI PyPI - Downloads

Example Image

First things first

This Repository is heavily inspired my Null Jones - Plotly Events

Since it didn't cover scattermaps in a way I need. I decided to reuse her repository and fit it to the needs of a scattermap

Installation

Install via Pip!

pip install streamlit-plotly-mapbox-events

Usage

Import the component, and use it like any other Streamlit custom component!

import streamlit as st

from streamlit_plotly_mapbox_events import plotly_mapbox_events
import plotly.express as px
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({
    'lat': [49.058, 50.383, 49.599, 50.677, 53.036, 50.541, 51.524, 54.992, 49.88],
    'lon': [11.115, 12.528, 11.231, 10.408, 8.185, 8.055, 7.639, 11.636, 7.678],
    'hover': [1, 2, 3, 4, 5, 6, 7, 8, 9],
    'color_1': [3, 3, 4, 3, 5, 5, 5, 4, 2],
    'color_2': [5, 5, 3, 1, 1, 2, 5, 2, 2],
    'color_3': [3, 2, 1, 5, 3, 2, 5, 2, 2]
})

# Create a Plotly Mapbox figure
mapbox = px.scatter_mapbox(df, lat="lat", lon="lon", hover_name="hover", zoom=5.5, height=600)
mapbox.update_layout(mapbox_style="carto-positron")
mapbox.update_layout(margin={"r":0, "t":0, "l":0, "b":0})

# Create an instance of the plotly_mapbox_events component
mapbox_events = plotly_mapbox_events(
    mapbox,
    click_event=True,
    select_event=True,
    hover_event=True,
    override_height=600
)

# Display the captured events
plot_name_holder_clicked = st.empty()
plot_name_holder_selected = st.empty()
plot_name_holder_hovered = st.empty()

plot_name_holder_clicked.write(f"Clicked Point: {mapbox_events[0]}")
plot_name_holder_selected.write(f"Selected Point: {mapbox_events[1]}")
plot_name_holder_hovered.write(f"Hovered Point: {mapbox_events[2]}")

Parameters

  • plot_fig (Plotly Figure): Plotly figure that we want to render in Streamlit.
  • click_event (boolean, default: True): Watch for click events on plot and return point data when triggered.
  • select_event (boolean, default: False): Watch for select events on plot and return point data when triggered.
  • hover_event (boolean, default: False): Watch for hover events on plot and return point data when triggered.
  • override_height (int, default: 450): Integer to override component height. Defaults to 450 (px).
  • override_width (string, default: '100%'): String (or integer) to override width. Defaults to 100% (whole width of iframe).
  • key (str or None): An optional key that uniquely identifies this component. If this is None, and the component's arguments are changed, the component will be re-mounted in the Streamlit frontend and lose its current state.

Return Value

A tuple consisting of lists or dictionaries for the captured events:

Click Event Select Event Hover Event For selected, click, and hover events, a list of dictionaries containing marker details is returned (in case multiple overlapping markers have been clicked/selected/hovered). The dictionary format is as follows:

{
    'lat': int (lat value of point),
    'lon': int (lon value of point),
    'pointNumber': (index of selected point),
    'pointIndex': (index of selected point)
}

Events

Currently, a number of plotly events can be enabled. They can be enabled/disabled using kwargs on the plotly_event() function.

  • Click click_event (defaults to True): Triggers event on mouse click of marker
  • Select select_event: Triggers event when markers have been selected
  • Hover hover_event: Triggers event on mouse hover of marker
  • Relayout relayout_event: Triggers if the layout has changed. Occurs on Zoom and Moving

Deprecation Warning

The relayout_event parameter is deprecated and will have no effect. Relayout events have been removed due to interferences with Streamlit.

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

streamlit_plotly_mapbox_events-0.2.1.tar.gz (5.8 MB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file streamlit_plotly_mapbox_events-0.2.1.tar.gz.

File metadata

File hashes

Hashes for streamlit_plotly_mapbox_events-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c30e2ff25898eed638139930cefb42a8a2af6a57a3f22ed7658a5ce34eac80e8
MD5 b2bd032512b5f7b4ff6e95038b36b371
BLAKE2b-256 2d322361570238b2f223d9bfd5c115bc86de3e22a4a01a75141031c9ff436edd

See more details on using hashes here.

File details

Details for the file streamlit_plotly_mapbox_events-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_plotly_mapbox_events-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25f807442d14be6ee86b2b92c4cb5c7542233f6770d28bac4579265c041603ac
MD5 551aa72671c8857ac6159f241a7e17a0
BLAKE2b-256 4db711e6ba482ee2cc70e715dca79347a3e7bea73c65a3b3080fbffa779d05a3

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