Counting charts, Plotly.js, external addresses, and points in a saved bakery report
Write code · Medium · §10.5 Reviewing the report
The starter code makes a line chart line_fig and a scatter plot scatter_fig from January to June data for two bakeries, Crumb and Crust, and saves them as the report file bakery_report.html. Read the saved file back and count the charts (the number of times class="plotly-graph-div" appears), the copies of Plotly.js inside the file (the number of times plotly.js v appears), and the places that load another file or a web address (the number of times src= appears after one space, that is ' src='). Then add up the points in all the traces of scatter_fig.
Print the four values in the order charts, plotly.js, src, points, one per line, in the form name: value. With the starting data, these four lines are printed.
charts: 2
plotly.js: 1
src: 0
points: 12
Grading changes the lines that make the report and the rows put into the scatter plot and runs your code again.
Starter code
from pathlib import Path
import pandas as pd
import plotly.express as px
dates = pd.date_range("2026-01-01", periods=6, freq="MS")
bakery = pd.DataFrame({
"date": list(dates) * 2,
"shop": ["Crumb"] * 6 + ["Crust"] * 6,
"loaves": [520, 480, 610, 590, 640, 700, 430, 450, 470, 520, 500, 560],
"flour": [210, 195, 240, 236, 250, 275, 180, 182, 190, 205, 201, 222],
})
line_fig = px.line(bakery, x="date", y="loaves", color="shop", markers=True, title="Loaves sold per month")
scatter_fig = px.scatter(bakery, x="flour", y="loaves", color="shop", symbol="shop",
hover_data={"date": "|%b %Y"}, title="Flour used and loaves sold")
parts = [line_fig.to_html(full_html=False, include_plotlyjs=True),
scatter_fig.to_html(full_html=False, include_plotlyjs=False)]
report_path = Path("bakery_report.html")
report_path.write_text(f"""<html><body>
<h1>Bakery report</h1>
{parts[0]}
{parts[1]}
</body></html>""", encoding="utf-8")
# Write your code hereSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.