Created
August 24, 2024 15:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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