Spaces:
Sleeping
Sleeping
Commit ·
320d35a
1
Parent(s): 5cee72e
Fix merchant CSV export
Browse files267 unmodified lines268269270271272273274275276277278279280281282267 unmodified linesreturn str(path) if path.exists() else Nonedef _write_csv_download(name: str, headers: List[str], rows: List[List[Any]]):path = Path(tempfile.gettempdir()) / namewith path.open("w", encoding="utf-8-sig", newline="") as fh:writer = csv.writer(fh)writer.writerow(headers)writer.writerows(rows or [])return str(path)@lru_cache(maxsize=1)def _predictor() -> AspectPredictor:return AspectPredictor(checkpoint_dir=CHECKPOINT_DIR)267 unmodified lines268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306267 unmodified linesreturn str(path) if path.exists() else Nonedef _write_csv_download(name: str, headers: List[str], rows):path = Path(tempfile.gettempdir()) / namenormalized = _normalize_table_rows(rows)with path.open("w", encoding="utf-8-sig", newline="") as fh:writer = csv.writer(fh)writer.writerow(headers)writer.writerows(normalized)return str(path)def _normalize_table_rows(rows) -> List[List[Any]]:if rows is None:return []if hasattr(rows, "values") and hasattr(rows, "columns"):return rows.fillna("").values.tolist()if isinstance(rows, dict):data = rows.get("data") or rows.get("values") or []return data if isinstance(data, list) else []if isinstance(rows, tuple):rows = list(rows)if not isinstance(rows, list):return []out = []for row in rows:if isinstance(row, dict):out.append(list(row.values()))elif isinstance(row, (list, tuple)):out.append(list(row))else:out.append([row])return out@lru_cache(maxsize=1)def _predictor() -> AspectPredictor:return AspectPredictor(checkpoint_dir=CHECKPOINT_DIR)
app.py+26-2