Building and Sharing an Interactive Data Report
Interactive charts become especially useful when combined into a report that addresses several related business questions.
Consider a retail dataset containing sales records for multiple products and regions.
Step 1: Prepare the Data
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
df = pd.DataFrame({
"Product": ["Laptop", "Phone", "Tablet", "Laptop", "Phone", "Tablet"],
"Region": ["North", "South", "East", "South", "North", "East"],
"Quantity": [10, 25, 15, 12, 30, 18],
"Price": [55000, 30000, 25000, 55000, 30000, 25000]
})
df["Revenue"] = df["Quantity"] * df["Price"]
print(df.head())
Step 2: Summarize Revenue by Product
product_summary = (
df.groupby("Product", as_index=False)["Revenue"]
.sum()
)
fig1 = px.bar(
product_summary,
x="Product",
y="Revenue",
title="Revenue by Product",
text="Revenue"
)
fig1.show()
This chart helps compare total revenue across products.
Step 3: Compare Regional Performance
region_summary = (
df.groupby("Region", as_index=False)["Revenue"]
.sum()
)
fig2 = px.pie(
region_summary,
names="Region",
values="Revenue",
title="Revenue Contribution by Region"
)
fig2.show()
A pie chart can communicate how each region contributes to total revenue when the number of categories is small. For precise comparisons, a bar chart is often easier to interpret.
Step 4: Combine Charts into a Single Figure
Plotly Graph Objects can arrange multiple charts within a shared figure.
fig = make_subplots(
rows=1,
cols=2,
subplot_titles=("Revenue by Product", "Revenue by Region"),
specs=[[{"type": "bar"}, {"type": "bar"}]]
)
fig.add_trace(
go.Bar(
x=product_summary["Product"],
y=product_summary["Revenue"],
name="Product Revenue"
),
row=1,
col=1
)
fig.add_trace(
go.Bar(
x=region_summary["Region"],
y=region_summary["Revenue"],
name="Regional Revenue"
),
row=1,
col=2
)
fig.update_layout(
title="Retail Sales Overview",
showlegend=False
)
fig.show()
Both charts appear in the same interactive figure, allowing viewers to examine product and regional performance together. The specs argument defines the type of chart in each subplot.
Step 5: Export and Share the Report
Save the combined figure as an HTML file:
fig.write_html(
"retail_sales_report.html",
include_plotlyjs=True
)
With include_plotlyjs=True, the file includes the JavaScript required to display the interactive charts offline. This can increase file size, but it makes the report more portable.
For larger reporting applications, Plotly charts can also be integrated into dashboard frameworks such as Dash. A standalone HTML report is suitable for sharing a chart-based report, while a dashboard application is better when users need additional controls, data filtering, or coordinated interactions.
Python for Data Science
Vishalini A








