Skip to content

Instantly share code, notes, and snippets.

@mtanco
Created June 3, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtanco/54bc1d48c1cc22b6accfd4a7f73d5c79 to your computer and use it in GitHub Desktop.
Save mtanco/54bc1d48c1cc22b6accfd4a7f73d5c79 to your computer and use it in GitHub Desktop.
The app shows how you can route to a different hash page, and then go to the home of the app by clicking an image in the header card.
from h2o_wave import main, app, Q, ui, on, handle_on
@app('/')
async def serve(q: Q):
print(q.args)
# First time a browser comes to the app
if not q.client.initialized:
await init(q)
q.client.initialized = True
elif q.args["#"] == "my-test":
clear_cards(q)
add_card(q, 'form2', ui.form_card(box='vertical', items=[
ui.text('You clicked a button, look at the URL! You can now go back home by clicking the image'),
]))
else:
# No specific action happened - probably the user clicked the H2O.ai image
await home(q)
# Other browser interactions
await handle_on(q)
await q.page.save()
async def init(q: Q) -> None:
q.client.cards = set()
q.client.dark_mode = False
q.page['meta'] = ui.meta_card(
box='',
title='My Wave App',
theme='light',
layouts=[
ui.layout(
breakpoint='xs',
min_height='100vh',
max_width='1200px',
zones=[
ui.zone('header'),
ui.zone('content', size='1', zones=[
ui.zone('horizontal', direction=ui.ZoneDirection.ROW),
ui.zone('vertical', size='1', ),
ui.zone('grid', direction=ui.ZoneDirection.ROW, wrap='stretch', justify='center')
]),
ui.zone(name='footer'),
]
)
]
)
q.page['header'] = ui.header_card(
box='header',
title='My Wave App',
subtitle="Example to get us started",
image='https://www.h2o.ai/wp-content/themes/h2o2018/templates/dist/images/h2o_logo.svg',
items=[ui.menu(icon='', items=[ui.command(name='change_theme', icon='ClearNight', label='Dark Mode')])]
)
q.page['footer'] = ui.footer_card(
box='footer',
caption='Made with 💛 using [H2O Wave](https://wave.h2o.ai).'
)
await home(q)
@on()
async def home(q: Q):
clear_cards(q)
add_card(q, 'form', ui.form_card(box='vertical', items=[
ui.text('This is my app!'),
ui.button(name="#my-test", label="Click me!")
]))
@on()
async def change_theme(q: Q):
"""Change the app from light to dark mode"""
if q.client.dark_mode:
q.page["header"].items = [ui.menu([ui.command(name='change_theme', icon='ClearNight', label='Dark mode')])]
q.page["meta"].theme = "light"
q.client.dark_mode = False
else:
q.page["header"].items = [ui.menu([ui.command(name='change_theme', icon='Sunny', label='Light mode')])]
q.page["meta"].theme = "h2o-dark"
q.client.dark_mode = True
# Use for cards that should be deleted on calling `clear_cards`. Useful for routing and page updates.
def add_card(q, name, card) -> None:
q.client.cards.add(name)
q.page[name] = card
def clear_cards(q, ignore=[]) -> None:
for name in q.client.cards.copy():
if name not in ignore:
del q.page[name]
q.client.cards.remove(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment