Spaces:
Running on Zero
Running on Zero
Cap to top 30 categories, hide gr.File X button, single-row buttons, no duplicate title
Browse files
src/visualization/plotly_fallback.py
CHANGED
|
@@ -40,8 +40,10 @@ def _fmt(v: float) -> str:
|
|
| 40 |
|
| 41 |
|
| 42 |
def _theme_layout(title: str = "") -> dict:
|
|
|
|
|
|
|
| 43 |
return dict(
|
| 44 |
-
title=dict(text=
|
| 45 |
font=dict(family=FONT_FAMILY, size=12, color=INK),
|
| 46 |
plot_bgcolor="rgba(0,0,0,0)",
|
| 47 |
paper_bgcolor="rgba(0,0,0,0)",
|
|
@@ -66,6 +68,8 @@ def _theme_layout(title: str = "") -> dict:
|
|
| 66 |
class PlotlyRenderer:
|
| 67 |
"""Render a chart spec + data tuple as a themed inline SVG."""
|
| 68 |
|
|
|
|
|
|
|
| 69 |
def render(self, spec: Dict[str, Any], data: List[Dict[str, Any]]) -> str:
|
| 70 |
if not data:
|
| 71 |
return self._empty("No data returned by the query.")
|
|
@@ -76,6 +80,14 @@ class PlotlyRenderer:
|
|
| 76 |
x = spec.get("x_column") or (df.columns[0] if len(df.columns) >= 1 else None)
|
| 77 |
y = spec.get("y_column") or (df.columns[1] if len(df.columns) >= 2 else None)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
fig = self._build(chart_type, df, x, y, title, spec)
|
| 81 |
except Exception as e:
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def _theme_layout(title: str = "") -> dict:
|
| 43 |
+
# Don't render Plotly's title — the user's question is already shown
|
| 44 |
+
# above the chart card, this would just duplicate.
|
| 45 |
return dict(
|
| 46 |
+
title=dict(text="", font=dict(family=FONT_FAMILY, size=15, color=INK)),
|
| 47 |
font=dict(family=FONT_FAMILY, size=12, color=INK),
|
| 48 |
plot_bgcolor="rgba(0,0,0,0)",
|
| 49 |
paper_bgcolor="rgba(0,0,0,0)",
|
|
|
|
| 68 |
class PlotlyRenderer:
|
| 69 |
"""Render a chart spec + data tuple as a themed inline SVG."""
|
| 70 |
|
| 71 |
+
MAX_CATEGORIES = 30 # cap for bar/pie/scatter to keep charts readable
|
| 72 |
+
|
| 73 |
def render(self, spec: Dict[str, Any], data: List[Dict[str, Any]]) -> str:
|
| 74 |
if not data:
|
| 75 |
return self._empty("No data returned by the query.")
|
|
|
|
| 80 |
x = spec.get("x_column") or (df.columns[0] if len(df.columns) >= 1 else None)
|
| 81 |
y = spec.get("y_column") or (df.columns[1] if len(df.columns) >= 2 else None)
|
| 82 |
|
| 83 |
+
# If categorical chart with too many categories: take top N by y value
|
| 84 |
+
if chart_type in ("bar", "pie") and y and y in df.columns and len(df) > self.MAX_CATEGORIES:
|
| 85 |
+
try:
|
| 86 |
+
df = df.sort_values(by=y, ascending=False).head(self.MAX_CATEGORIES)
|
| 87 |
+
title = (title or "Top values") + f" (top {self.MAX_CATEGORIES})"
|
| 88 |
+
except Exception:
|
| 89 |
+
df = df.head(self.MAX_CATEGORIES)
|
| 90 |
+
|
| 91 |
try:
|
| 92 |
fig = self._build(chart_type, df, x, y, title, spec)
|
| 93 |
except Exception as e:
|