Skip to content

Instantly share code, notes, and snippets.

@mtanco
Created June 12, 2024 18:31
Show Gist options
  • Save mtanco/4d11362c9684bf958ba9adb704061ccb to your computer and use it in GitHub Desktop.
Save mtanco/4d11362c9684bf958ba9adb704061ccb to your computer and use it in GitHub Desktop.
wave_filter_button.py
from h2o_wave import main, app, Q, ui, on, run_on, data
@app('/')
async def serve(q: Q):
print(q.args)
print(q.events)
# First time a browser comes to the app
if not q.client.initialized:
await initialize_client_session(q)
if q.events.chatbot is not None and q.events.chatbot.suggestion == "filter":
q.page["meta"].dialog = ui.dialog(
title="Filter Documents",
blocking=True,
items=[
ui.dropdown(
name="titles",
label="Titles",
choices=[ui.choice(choice, choice) for choice in q.app.title_choices],
values=q.client.title_choices,
width="400px",
),
ui.buttons(justify="end", items=[ui.button("save", "Save", primary=True)])
]
)
# Other browser interactions
await run_on(q)
await q.page.save()
async def initialize_client_session(q: Q) -> None:
if not q.app.initialized:
# These variables are the options used by all users
q.app.title_choices = [f"Title{i}" for i in range(5)]
q.app.initialized = True
# These variables will change for each user, but we start chatting on all docs
q.client.title_choices = q.app.title_choices
q.client.location_choices = q.app.location_choices
q.page['meta'] = ui.meta_card(
box='',
layouts=[
ui.layout(
breakpoint='xs',
min_height='100vh',
max_width='900px',
zones=[
ui.zone(name='header'),
ui.zone(name='content', size='1'),
ui.zone(name='footer'),
]
)
]
)
q.page['header'] = ui.header_card(
box='header',
title='My Wave App',
subtitle="Example to get us started",
image='https://wave.h2o.ai/img/h2o-logo.svg',
)
q.page["chatbot"] = ui.chatbot_card(
box="content",
name="chatbot",
data=data(fields='content from_user', t='list', rows=[
['Welcome! Ask me questions about any and all titles or locations!', False],
]),
suggestions=[ui.chat_suggestion(name="filter", label="Filter Documents", icon="Filter")],
events=["suggestion"]
)
q.page['footer'] = ui.footer_card(
box='footer',
caption='Made with 💛 using [H2O Wave](https://wave.h2o.ai).'
)
q.client.initialized = True
@on()
async def titles(q: Q):
if len(q.args.titles) == 0 or len(q.args.titles) == len(q.app.title_choices):
bot_message = "Based on your filter, questions will be asked to all Titles."
else:
bot_message = f"Based on your filter, questions will be asked to {q.args.titles}."
q.page["chatbot"].data += [bot_message, False]
q.client.title_choices = q.args.titles
@on()
async def save(q: Q):
await titles(q)
q.page["meta"].dialog = None
@on()
async def chatbot(q: Q):
q.page["chatbot"].data += [q.args.chatbot, True]
q.page["chatbot"].data += [f"I am fake, but if I was real I would chat about {q.client.title_choices}", False]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment