Created
December 3, 2021 17:48
-
-
Save mtanco/f4de831f057d162dd9e0494a20423965 to your computer and use it in GitHub Desktop.
How to make a table from a pandas data frame and do something when one of the rows is clicked in H2O Wave.
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 h2o_wave import main, app, Q, ui | |
import pandas as pd | |
import numpy as np | |
@app('/') | |
async def serve(q: Q): | |
print(q.args) | |
if not q.client.initialized: | |
n = 100 | |
df = pd.DataFrame({'id': range(n), | |
'date': pd.date_range(start='2021-01-01', end='2021-12-31', periods=n), | |
'amount': np.random.randint(0, 100, n), | |
}) | |
q.page["home"] = ui.form_card( | |
box="2 2 8 8", | |
items=[ | |
ui.text_xl("Click a row!"), | |
ui.table( | |
name="my_table", | |
columns=[ui.table_column(name=str(col), label=str(col)) for col in df.columns.values], | |
rows=[ui.table_row( | |
name=str(i), | |
cells=[str(cell) for cell in row] | |
) for i, row in df.iterrows()], | |
height="400px" | |
), | |
ui.text("Row clicked: None") | |
] | |
) | |
q.client.df = df | |
q.client.initialized = True | |
if q.args.my_table is not None: | |
row_index = int(q.args.my_table[0]) | |
amount = q.client.df.iloc[row_index]["amount"] | |
q.page["home"].items[2].text.content = f"Amount of row clicked: {amount}" | |
await q.page.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment