Part V — The Modern Experience Layer · Chapter 29 · Lesson 2 of 2
SQL over records, reports and charts
analysis.sql runs SQL over in-memory record lists with SQLite: joins, GROUP BY, window functions and CTEs are all available. The default table name is data; pass table="orders" if your query uses another name. No external service is involved.
SQL over records
let result = analysis.sql(df, "SELECT city, SUM(amount) AS revenue, COUNT(*) AS orders FROM data WHERE status = 'paid' GROUP BY city HAVING SUM(amount) > 10000 ORDER BY revenue DESC")
let top = analysis.sql_value(df, "SELECT MAX(amount) FROM data")
Window functions and CTEs work too: analysis.sql(df, "WITH paid AS (SELECT * FROM data WHERE status = 'paid') SELECT city, ROW_NUMBER() OVER (PARTITION BY city ORDER BY amount DESC) AS rnk FROM paid"). It executes the supplied SQL against an in-memory SQLite table and returns the fetched rows; it is not a warehouse.
Reports people actually read
let prof = analysis.profile(df)
let by_city = analysis.group_by_multi(df, ["city"], "amount", "sum")
analysis.write_report_markdown("weekly.md", prof)
analysis.write_report_html("weekly.html", prof)
analysis.write_report_json("weekly.json", by_city)
print analysis.profile_markdown(df) # profile as markdown
print analysis.summary(df) # short text summary
report_markdown, report_html, profile_markdown, model_markdown and evaluation_markdown build the document; the write_report_* helpers take (path, report) and save it. Pass records, a profile, a fitted model or an evaluation — not a hand-written {"title", "sections"} object.
Charts without a plotting library
let cities = [r["city"] for r in by_city]
let revenue = [r["sum_amount"] for r in by_city]
print analysis.bar_chart_svg(cities, revenue, "Revenue by city")
print analysis.line_chart_svg([1, 2, 3, 4], [8, 12, 10, 15], "Weekly orders")
print analysis.scatter_plot_svg([10, 20, 30], [8, 15, 22], "Spend vs revenue")
print analysis.histogram_svg([r["amount"] for r in df], "Amounts", 20)
print analysis.ascii_bar_chart(cities, revenue)
Chart helpers take sequences, not a record plus an options record: bar labels can be strings, while line, scatter and histogram values are numeric. chart_to_file(svg, path) saves the SVG; ASCII variants are for logs, REPLs and CI output.
Model persistence
When the analysis produces a fitted artifact, stats.save_model(path, model) writes a versioned JSON document and stats.load_model(path) reads it back. stats.export_model(model) and stats.import_model(document) do the same in memory; stats.model_summary(model) returns a summary record. These helpers live in stats, not analysis.
stats — for inference
stats covers descriptive measures and distributions (normal_pdf, normal_cdf, poisson_pmf, binomial_pmf), hypothesis tests (t_test_ind, welch_t_test, chi_square_test, anova_oneway, mann_whitney_u), confidence intervals and bootstrap_ci, regression (ols, glm, ridge_regression, lasso_regression), time series (acf, pacf, seasonal_decompose, adf_test, arima, auto_arima) and causal estimators (ipw_ate, difference_in_differences, regression_discontinuity). It is standard-library Python only — no NumPy or SciPy required.
Guided practice
- Answer "revenue by city, paid orders only, top 5" with one
analysis.sqlcall; explain why you chose SQL overgroup_by. - Write a profile to markdown and HTML with the
write_report_*helpers; open the HTML file in a browser. - Run
analysis.profile, thenstats.t_test_indon two segments of the same column and interpret the result honestly.