Interactive Mermaid diagram component for Streamlit with click event handling
Project description
Streamlit Mermaid Interactive
A Streamlit component for rendering interactive Mermaid diagrams with click event handling.
Installation
with uv:
uv add streamlit-mermaid-interactive
with pip:
pip install streamlit-mermaid-interactive
Demo
https://mermaid-component.streamlit.app/
Supported Diagram Types
This component currently supports 5 diagram types with full interactivity:
✅ Fully Working
- Flowchart - Click on nodes
- Sequence Diagram - Click on actors/participants
- Entity Relationship Diagram (ERD) - Click on entity boxes
- State Diagram - Click on state boxes
- Class Diagram - Click on class boxes
⚠️ Rendered but Not Interactive
The following diagram types are rendered correctly but clicks do not yet work:
- Pie Chart - Legend items not yet clickable
- Gantt Chart - No identifiable parent containers for tasks
- Git Graph - Commits without IDs have no clickable targets
- User Journey - Task text elements lack identifiable parents
- Timeline - Text elements have no IDs or clickable parents
Usage
import streamlit as st
from streamlit_mermaid_interactive import mermaid
# Example: Flowchart
flowchart_code = """
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[OK]
B -->|No| D[End]
"""
result = mermaid(flowchart_code, theme="neutral", key="flowchart")
if result.get("entity_clicked"):
st.info(f"Clicked: {result['entity_clicked']}")
Supported Themes
"neutral"(default)"dark""forest""base"
Known Limitations
Technical Challenges
The unsupported diagram types have specific issues with Mermaid 11's SVG rendering:
- Pie Charts: Legend items not structured for easy click detection
- Gantt Charts: Task rectangles exist but have no parent containers with IDs
- Git Graphs: Commits created with bare
commitcommand have no commit IDs to extract - User Journey/Timeline: Text elements for tasks/events have no identifiable parent elements with IDs
Why Not More Diagrams?
Mermaid uses different SVG structures for different diagram types. Some structures make it straightforward to identify clickable elements (like ERD entity boxes or State diagram states), while others require complex DOM traversal or text-based parsing that is error-prone.
Development
Building the Frontend
The component uses Vite to bundle the frontend JavaScript with Mermaid 11:
# Install dependencies (first time only)
npm install
# Build the frontend
npm run build
After editing src/streamlit_mermaid_interactive/frontend/component.js, run npm run build to rebuild main.js.
Running Tests
# Run tests
uv run pytest
Manual Testing
Run the demo app:
streamlit run streamlit_app.py
To add support for more diagram types, you would need to:
- Inspect the SVG structure for that diagram type
- Find a reliable selector or ID pattern for clickable elements
- Update the click detection logic in
src/streamlit_mermaid_interactive/frontend/component.js - Rebuild the frontend with
npm run build - Test thoroughly with various diagram configurations
Example App
See streamlit_app.py for examples of all diagram types (both working and non-working).
License
MIT
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
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 streamlit_mermaid_interactive-0.1.12.tar.gz.
File metadata
- Download URL: streamlit_mermaid_interactive-0.1.12.tar.gz
- Upload date:
- Size: 888.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1367b1c50f2aedc3cb48dd592026817d0d2a0b325f864c9832e0c524b14173b
|
|
| MD5 |
7aeafabad514be2a3963dadb682194f0
|
|
| BLAKE2b-256 |
87273668571de823b95405c28ab56881b5a427215c85b462035b7111a70ea11a
|
File details
Details for the file streamlit_mermaid_interactive-0.1.12-py3-none-any.whl.
File metadata
- Download URL: streamlit_mermaid_interactive-0.1.12-py3-none-any.whl
- Upload date:
- Size: 891.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60aa439d1acacbfdc128735abf47cd1b839f60117e0c54f08438478fe3c95c94
|
|
| MD5 |
e00e616f1763013a17a3e499f60fe46c
|
|
| BLAKE2b-256 |
ec7fc4294f862bf9865c04ffdf623170c67c2096bea4efc8518132d3e7cb639b
|