Spaces:
Sleeping
Sleeping
Commit ·
320d35a
1
Parent(s): 5cee72e
Fix merchant CSV export
Browse files
app.py
CHANGED
|
@@ -268,15 +268,39 @@ def _report_image(name: str):
|
|
| 268 |
return str(path) if path.exists() else None
|
| 269 |
|
| 270 |
|
| 271 |
-
def _write_csv_download(name: str, headers: List[str], rows
|
| 272 |
path = Path(tempfile.gettempdir()) / name
|
|
|
|
| 273 |
with path.open("w", encoding="utf-8-sig", newline="") as fh:
|
| 274 |
writer = csv.writer(fh)
|
| 275 |
writer.writerow(headers)
|
| 276 |
-
writer.writerows(
|
| 277 |
return str(path)
|
| 278 |
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
@lru_cache(maxsize=1)
|
| 281 |
def _predictor() -> AspectPredictor:
|
| 282 |
return AspectPredictor(checkpoint_dir=CHECKPOINT_DIR)
|
|
|
|
| 268 |
return str(path) if path.exists() else None
|
| 269 |
|
| 270 |
|
| 271 |
+
def _write_csv_download(name: str, headers: List[str], rows):
|
| 272 |
path = Path(tempfile.gettempdir()) / name
|
| 273 |
+
normalized = _normalize_table_rows(rows)
|
| 274 |
with path.open("w", encoding="utf-8-sig", newline="") as fh:
|
| 275 |
writer = csv.writer(fh)
|
| 276 |
writer.writerow(headers)
|
| 277 |
+
writer.writerows(normalized)
|
| 278 |
return str(path)
|
| 279 |
|
| 280 |
|
| 281 |
+
def _normalize_table_rows(rows) -> List[List[Any]]:
|
| 282 |
+
if rows is None:
|
| 283 |
+
return []
|
| 284 |
+
if hasattr(rows, "values") and hasattr(rows, "columns"):
|
| 285 |
+
return rows.fillna("").values.tolist()
|
| 286 |
+
if isinstance(rows, dict):
|
| 287 |
+
data = rows.get("data") or rows.get("values") or []
|
| 288 |
+
return data if isinstance(data, list) else []
|
| 289 |
+
if isinstance(rows, tuple):
|
| 290 |
+
rows = list(rows)
|
| 291 |
+
if not isinstance(rows, list):
|
| 292 |
+
return []
|
| 293 |
+
out = []
|
| 294 |
+
for row in rows:
|
| 295 |
+
if isinstance(row, dict):
|
| 296 |
+
out.append(list(row.values()))
|
| 297 |
+
elif isinstance(row, (list, tuple)):
|
| 298 |
+
out.append(list(row))
|
| 299 |
+
else:
|
| 300 |
+
out.append([row])
|
| 301 |
+
return out
|
| 302 |
+
|
| 303 |
+
|
| 304 |
@lru_cache(maxsize=1)
|
| 305 |
def _predictor() -> AspectPredictor:
|
| 306 |
return AspectPredictor(checkpoint_dir=CHECKPOINT_DIR)
|