Skip to main content

Table / Filter / Backend

Filter table using Python.

import pandas as pd
from faker import Faker
from h2o_wave import main, app, Q, ui

fake = Faker()

N = 50 # number of rows

# Make a synthetic data frame
addresses = pd.DataFrame(dict(
ID=[i + 1 for i in range(N)],
Name=[ for _ in range(N)],
Language=[fake.language_name() for _ in range(N)],
Job=[fake.job() for _ in range(N)],
Address=[fake.address() for _ in range(N)],
City=[ for _ in range(N)],

column_names = ['ID', 'Name', 'Language', 'Job', 'Address', 'City']

def df_to_rows(df: pd.DataFrame):
return [ui.table_row(str(row['ID']), [str(row[name]) for name in column_names]) for i, row in df.iterrows()]

def search_df(df: pd.DataFrame, term: str):
str_cols = df.select_dtypes(include=[object])
return df[str_cols.apply(lambda column: column.str.contains(term, case=False, na=False)).any(axis=1)]

async def serve(q: Q):
if not q.client.initialized:['form'] = ui.form_card(box='1 1 -1 7', items=[
ui.textbox(name='search', label='Search address', placeholder='Enter a keyword...', trigger=True),
columns=[ui.table_column(name=name, label=name) for name in column_names],
q.client.initialized = True
table =['form'].issues
term: str =
term = term.strip() if term else ''
table.rows = df_to_rows(search_df(addresses, term) if len(term) else addresses)


Tags:  table