# # utils.py # import io # import pandas as pd # def pick_existing_columns(all_cols: list[str], desired: list[str]) -> list[str]: # seen = {c.lower(): c for c in all_cols} # out = [] # for d in desired: # key = d.lower() # if key in seen: # out.append(seen[key]) # return out or list(all_cols) # fall back to everything if none matched # def slice_up_to_page(df: pd.DataFrame, page: int, page_size: int) -> pd.DataFrame: # start = page * page_size # end = start + page_size # return df.iloc[:end].copy() # def contains_any_column(df: pd.DataFrame, term: str) -> pd.Series: # if not term: # return pd.Series([True] * len(df), index=df.index) # term = str(term).strip() # if not term: # return pd.Series([True] * len(df), index=df.index) # # Combine columns as strings (fast-ish) # joined = df.astype(str).apply(lambda col: col.str.contains(term, case=False, na=False)) # return joined.any(axis=1) # def export_csv(df: pd.DataFrame) -> tuple[str, bytes]: # buf = io.StringIO() # df.to_csv(buf, index=False) # name = "buildscout_export.csv" # return name, buf.getvalue().encode("utf-8") # utils.py import io import pandas as pd def pick_existing_columns(all_cols, wanted): s = set(all_cols) return [c for c in wanted if c in s] def export_csv(df: pd.DataFrame) -> io.BytesIO: bio = io.BytesIO() df.to_csv(bio, index=False) bio.seek(0) return bio