Streamlit
D3Blocks charts are self-contained HTML + JavaScript. You can embed them in a
Streamlit app by generating the HTML with
showfig=False and return_html=True, then displaying it with
st.iframe.
Recommended pattern
import streamlit as st
from d3blocks import D3Blocks
st.set_page_config(page_title="D3Blocks", layout="wide")
# Optional: put chart options in the Streamlit sidebar
show_side_panel = st.sidebar.checkbox("Show D3Blocks side panels", value=False)
show_top_panel = st.sidebar.checkbox("Show D3Blocks top panel", value=True)
height = st.sidebar.slider("Chart height", 400, 1200, 700, 50)
d3 = D3Blocks()
# Build HTML without opening a browser window
html = d3.scatter(
x=..., y=...,
show_side_panel=show_side_panel,
show_top_panel=show_top_panel,
dark_mode=False,
color_background='streamlit',
showfig=False,
return_html=True,
)
st.iframe(html, height=height, width="stretch")
Rules of thumb
Prefer ``st.iframe(html, height=…)``. Do not use the deprecated
components.htmlfor these charts, and avoidst.htmlfor full interactive charts: it is not iframed, and JavaScript is off unlessunsafe_allow_javascript=True. Fixed-position CSS from the chart would also overlay the Streamlit page.Always pass an explicit pixel height (or
height="stretch").height="content"is unreliable when the chart usesposition: fixedand contributes almost no document-flow height.Generate with ``showfig=False`` and ``return_html=True``.
When Streamlit already owns the controls (sidebar), set
show_side_panel=False(and optionallyshow_top_panel=False) so the chart does not duplicate UI. See UI controls (panels and theme).color_background='streamlit'picks backgrounds that match a typical Streamlit light/dark page.
Demo apps
Ready-to-run demos live in the repository under the streamlit/ directory.
Each file is a minimal Streamlit app for one chart. Run from a machine with
d3blocks and streamlit installed:
streamlit run streamlit/scatter_streamlit_demo.py
Available demos
Chart |
Demo file |
Notes |
|---|---|---|
Scatter |
|
Point size/color/tooltip; panel toggles |
Sankey |
|
Link color/opacity, node layout |
Chord |
|
Energy-flow style demo |
Heatmap |
|
Matrix heatmap controls |
Violin |
|
Distribution view |
Timeseries |
|
Multi-series time chart |
MovingBubbles |
|
Animated state transitions |
Imageslider |
|
Before/after images; dark mode & background |
Treemap |
|
Hierarchical rectangles |
Circlepacking |
|
Nested circles |
Maps |
|
Lat/lon markers |
D3graph |
|
Force-directed network |
Elasticgraph |
|
Elastic force network |
Radialgraph |
|
Focus-centered radial network |
Typical layout of a demo
Load data once with
@st.cache_data(often viad3.import_example(...)).Sidebar — Streamlit widgets for data encodings and for
show_side_panel/show_top_panel/ height.Build HTML inside a cached function that calls the D3Blocks chart method with
showfig=False,return_html=True.Main area —
st.iframe(html, height=height, width="stretch").
This keeps the interactive D3 chart inside a fixed-height frame while Streamlit owns navigation and global controls.
See also
UI controls (panels and theme) —
show_side_panel,show_top_panel,color_background,dark_mode.Per-chart pages under Blocks for input data shapes and API details.