Chart Utilities¶
cortex_agents.chart_utils.plot_charts ¶
plot_charts(charts: list[dict[str, Any]], interactive: bool = True, max_width: int = 900, max_height: int = 450, display_now: bool = True) -> list | None
Plot charts from agent response using Altair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
charts
|
list[dict[str, Any]]
|
List of chart dicts from response.get_charts() |
required |
interactive
|
bool
|
Enable interactive Altair features (default: True) |
True
|
max_width
|
int
|
Maximum chart width in pixels (default: 900) |
900
|
max_height
|
int
|
Maximum chart height in pixels (default: 600) |
450
|
display_now
|
bool
|
Auto-display in Jupyter (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
list | None
|
List of Altair Chart objects (or None if Altair not installed) |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Altair is not installed |
ValueError
|
If charts list is empty or malformed |
Examples:
response = agent.run("Show dual eligible trend", agent_name="MY_AGENT", database="MY_DB", schema="MY_SCHEMA")
charts = response.get_charts()
if charts:
plot_charts(charts)
Source code in cortex_agents/chart_utils.py
cortex_agents.chart_utils.plot_chart_dict ¶
plot_chart_dict(chart_spec: dict[str, Any], interactive: bool = True, max_width: int = 900, max_height: int = 600) -> alt.Chart
Plot a single chart from a Vega-Lite specification dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chart_spec
|
dict[str, Any]
|
Vega-Lite specification dict |
required |
interactive
|
bool
|
Enable interactive features (default: True) |
True
|
max_width
|
int
|
Maximum chart width in pixels |
900
|
max_height
|
int
|
Maximum chart height in pixels |
600
|
Returns:
| Type | Description |
|---|---|
Chart
|
Altair Chart object |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Altair is not installed |
Examples:
spec = {
"mark": "line",
"encoding": {
"x": {"field": "month", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"}
},
"data": {"values": [...]}
}
plot_chart_dict(spec)
To use it in Streamlit:
::
chart_objects = plot_charts(charts, interactive=True)
for i, chart in enumerate(chart_objects):
st.vega_lite_chart(json.loads(chart.to_json()), width='stretch')
Source code in cortex_agents/chart_utils.py
cortex_agents.chart_utils.extract_chart_specs ¶
Extract parsed Vega-Lite specs from chart dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
charts
|
list[dict[str, Any]]
|
List of chart dicts from response.get_charts() |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of parsed Vega-Lite specification dicts |
Examples:
charts = response.get_charts()
specs = extract_chart_specs(charts)
for spec in specs:
print(spec["title"])
Source code in cortex_agents/chart_utils.py
cortex_agents.chart_utils.get_chart_info ¶
Get metadata about charts without rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
charts
|
list[dict[str, Any]]
|
List of chart dicts from response.get_charts() |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of dicts with chart info (title, mark, fields) |
Examples:
charts = response.get_charts()
info = get_chart_info(charts)
for chart_info in info:
print(f"Title: {chart_info['title']}")
print(f"Type: {chart_info['mark']}")
print(f"Fields: {chart_info['fields']}")
Source code in cortex_agents/chart_utils.py
cortex_agents.chart_utils.chart_to_json ¶
Convert chart spec to JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chart_spec
|
dict[str, Any]
|
Vega-Lite specification dict |
required |
pretty
|
bool
|
Pretty-print JSON (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string |