Skip to content

Latest commit

 

History

History
144 lines (117 loc) · 5.93 KB

README.md

File metadata and controls

144 lines (117 loc) · 5.93 KB

TableFlow Visualization

Chart generation for tabular artifacts. The module keeps two independent lanes so that a quick "show me this file" preview never pays for a model call, while a chat request such as "draw me a bar chart" can still get an LLM-planned, richer chart without going through the full analysis route.

Two Lanes

Fast preview lane (no LLM)
  build_preview()        -> row/column/type/missing-value summary + head rows
  recommend_charts()     -> deterministic rule-based chart candidates
  render_chart()         -> matplotlib PNG + JSON spec

Conversation lane (optional LLM)
  build_preview() + recommend_charts()   -> context for the planner
  plan_chart_with_llm()                  -> one structured chart plan (or a
                                             clarifying question)
  render_echarts_visualization()         -> validated ECharts JSON option
                                             (falls back to render_chart() in
                                             `quick` mode)

Both lanes share the same profiling and column-selection helpers in service.py, so a chart produced by either path uses the same column inference (numeric / categorical / temporal detection) and the same output contract (chart_id, image_path or echarts_option, spec_path).

Design Rationale

The fast lane exists because uploading a file and immediately wanting to see it should never wait on a model round trip. build_preview and recommend_charts are pure pandas — deterministic, cheap, and safe to call on every upload.

The conversation lane exists for the opposite case: a chat message like "make this result into a nice-looking chart" carries intent that a fixed rule can't resolve (which chart type, which fields, single chart or an overview). Here the LLM is used narrowly — as a planner, not a renderer:

  • The model never writes files or produces pixels directly.
  • It only returns a structured JSON plan (chart_type, x, y, color, style, transform, or needs_confirmation + a clarifying message).
  • The plan is re-validated against the real preview: unknown columns, unknown chart types, and out-of-range limits are all stripped or rejected before any data is touched.
  • If the model call fails or returns something unusable, the stage falls back to the deterministic recommendations instead of blocking the user.

This keeps the failure mode safe: a bad or missing model response degrades to "quick lane" behavior rather than crashing or hallucinating a chart.

Chart Types And Renderers

The fast lane's render_chart() supports four chart types with a fixed matplotlib style: bar, line, scatter, histogram.

The conversation lane's render_echarts_visualization() (in advanced.py) supports a superset aimed at richer, still-deterministic output once the plan is known:

bar, horizontal_bar, line, area, scatter, bubble, histogram, box, pie, donut,
heatmap, dashboard

dashboard combines several of the above into one spec when the request is broad ("give me an overview") or the recommendation set naturally spans more than one chart. All transforms (groupby, sort, limit, time-bucketing) run in pandas before rendering — the LLM selects which transform, never executes one itself.

Package Layout

visualization/
  __init__.py       Public exports for both lanes
  service.py         Fast lane: build_preview / recommend_charts / render_chart,
                      shared column-type inference and JSON-safe serialization
  conversation.py     LLM chart planner: prompt, strict-JSON parsing, plan
                      normalization against the real column set
  advanced.py         Conversation-lane ECharts renderer: rich spec handling,
                      per-chart-type spec builders, dashboard composition

Generated chart artifacts are named <chart_id>.png/.json where chart_id comes from tableflow.core.naming.new_artifact_name(chart_type) — a <UTC timestamp>_<chart type>_<short id> stem shared with every other route's generated artifacts (see tableflow.core.naming).

Configuration

The conversation lane's planner reuses the same Claude Agent SDK credentials as the rest of the SDK (see the root README). It runs with allowed_tools=[] and every filesystem/network tool explicitly disallowed, since its only output is a JSON plan.

Input Purpose
mode conversation (default, LLM-planned + ECharts) or quick (rule-based + matplotlib PNG)
planning / planner llm (default) or rule to force the deterministic path
task / instruction / objective / question user's natural-language visualization request, passed to the planner
chart_type, x, y, title, limit, output_mode explicit overrides; when set, they take precedence over the planner's choice
recommend_only return recommendations/plan without rendering

Usage

from tableflow.visualization import build_preview, recommend_charts, render_chart

preview = build_preview("sales.csv")
candidates = recommend_charts("sales.csv")
chart = render_chart(
    "sales.csv",
    output_dir="./out",
    chart_type="bar",
    x="region",
    y="sales",
)
print(chart["image_path"], chart["spec_path"])

Conversation-lane planning is normally invoked through the visualization system stage (see src/tableflow/system/stages.py), which resolves the target artifact from chat context, calls the planner, validates the plan, and renders it:

from tableflow.visualization import plan_chart_with_llm, render_echarts_visualization

plan = await plan_chart_with_llm(
    "Make this into a nice-looking bar chart, grouped by region",
    preview,
    candidates,
    model_name="claude-sonnet-4-6",
)
chart = render_echarts_visualization(
    "sales.csv",
    output_dir="./out",
    task="Make this into a nice-looking bar chart, grouped by region",
    plan=plan,
)