Skip to content

Instantly share code, notes, and snippets.

@mtanco
Created June 10, 2024 14:54
Show Gist options
  • Save mtanco/9096a73b3bbd4b961704b7c68337abd3 to your computer and use it in GitHub Desktop.
Save mtanco/9096a73b3bbd4b961704b7c68337abd3 to your computer and use it in GitHub Desktop.
from h2o_wave import main, app, Q, ui, on, run_on, data
@app('/')
async def serve(q: Q):
print(q.args)
# First time a browser comes to the app
if not q.client.initialized:
await initialize_client_session(q)
# 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.location_choices = [f"Location{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["filters"] = ui.form_card(
box=ui.box("content", size="0"), # size=0 to take up less space
items=[
ui.inline(justify="center", 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",
trigger=True
),
ui.dropdown(
name="locations",
label="Locations",
choices=[ui.choice(choice, choice) for choice in q.app.location_choices],
values=q.client.location_choices,
width="400px"
),
])
]
)
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],
]),
)
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 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