Skip to content

Instantly share code, notes, and snippets.

@iamaziz
Created August 24, 2024 15:11
Show Gist options
  • Save iamaziz/acac31258a2c4e98d0fca71fe5156908 to your computer and use it in GitHub Desktop.
Save iamaziz/acac31258a2c4e98d0fca71fe5156908 to your computer and use it in GitHub Desktop.
Convert pd.dataframe df to FastHTML table. Similar to `df.to_html()` but different
from fasthtml import common as fh
def df2fhtml(df: pd.DataFrame, with_headers: bool=True, **kwargs):
cols = df.columns
header = fh.Tr(*[fh.Th(fh.Label(col)) for col in cols])
rows = [fh.Tr(*[fh.Td(df[col][i]) for col in cols]) for i in range(len(df))]
return fh.Table(header if with_headers else '', *rows, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment