Last active
November 4, 2023 22:45
-
-
Save drearondov/c736573b8559337bbc36a486bb99f094 to your computer and use it in GitHub Desktop.
Constructs a Plotly Dash HTML table from a Pandas Dataframe
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
def create_stats_table(data_frame: pd.DataFrame) -> html.Table: | |
"""Constructor for a Dash Table from a Data Frame | |
Args: | |
data_frame (pd.DataFrame): Source data for Table | |
Returns: | |
html.Table: Dash Table element | |
""" | |
table_rows: list[html.Tr] = [] | |
for _, row in data_frame.iterrows(): | |
single_row: list[html.Td] = [] | |
for item in row.tolist(): | |
if isinstance(item, str): | |
item = item.replace("_", " ") | |
item = item.title() | |
elif isinstance(item, float): | |
item = "{:.2f}".format(item) | |
single_row.append(html.Td(item)) | |
table_rows.append(html.Tr(children=single_row)) | |
dash_table = html.Table( | |
className="table is-fullwidth is-striped", | |
children=[ | |
html.Thead( | |
children=html.Tr( | |
children=[html.Th(column_name) for column_name in data_frame.columns] | |
) | |
), | |
html.Tbody( | |
children=table_rows | |
) | |
] | |
) | |
return dash_table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment