Skip to content

Commit

Permalink
chore: Rename all occurences of handle_on, rework app templates. #1484
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Sep 7, 2023
1 parent edf5070 commit 73313cf
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 396 deletions.
4 changes: 2 additions & 2 deletions py/examples/hash_routing_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Use the browser's [location hash](https://developer.mozilla.org/en-US/docs/Web/API/Location/hash)
# for #routing using URLs, with parameters.
# ---
from h2o_wave import main, app, Q, ui, on, handle_on
from h2o_wave import main, app, Q, ui, on, run_on

air_passengers_fields = ['Year', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
air_passengers_rows = [
Expand Down Expand Up @@ -62,6 +62,6 @@ async def serve(q: Q):
content='Click on a cell in the table above!',
)

await handle_on(q)
await run_on(q)

await q.page.save()
4 changes: 2 additions & 2 deletions py/examples/plot_events_routing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Plot / Events / Routing
# Handle #events on a #plot card using routing.
# ---
from h2o_wave import main, app, on, handle_on, Q, ui, data
from h2o_wave import main, app, on, run_on, Q, ui, data


@on('pricing.select_marks')
Expand Down Expand Up @@ -32,4 +32,4 @@ async def serve(q: Q):
)
await q.page.save()
else:
await handle_on(q)
await run_on(q)
6 changes: 3 additions & 3 deletions py/examples/routing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Routing
# Use `on` and `handle_on` to simplify query handling by #routing queries to designated functions.
# Use `on` and `run_on` to simplify query handling by #routing queries to designated functions.
# ---
from h2o_wave import main, app, Q, ui, on, handle_on
from h2o_wave import main, app, Q, ui, on, run_on


# This function is called when q.args['empty_cart'] is True.
Expand Down Expand Up @@ -60,4 +60,4 @@ async def serve(q: Q):
],
)
await q.page.save()
await handle_on(q)
await run_on(q)
6 changes: 3 additions & 3 deletions py/examples/routing_predicates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Routing / Predicates
# Use `on` and `handle_on` with predicates to handle routing with custom conditions.
# Use `on` and `run_on` with predicates to handle routing with custom conditions.
# ---
from h2o_wave import main, app, Q, ui, on, handle_on
from h2o_wave import main, app, Q, ui, on, run_on


# This function is called when q.args['temperature'] < 15.
Expand Down Expand Up @@ -29,7 +29,7 @@ async def serve(q: Q):
q.args.temperature = 20
await show_slider(q, "")
else:
await handle_on(q)
await run_on(q)


async def show_slider(q: Q, message: str):
Expand Down
4 changes: 2 additions & 2 deletions py/examples/tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Dict, List, Optional, Tuple
from urllib.parse import urlparse

from h2o_wave import Q, app, handle_on, main, on, ui
from h2o_wave import Q, app, run_on, main, on, ui

example_dir = os.path.dirname(os.path.realpath(__file__))
tour_tmp_dir = os.path.join(example_dir, '_tour_apps_tmp')
Expand Down Expand Up @@ -415,7 +415,7 @@ async def serve(q: Q):
q.client.path = uuid.uuid4()
await setup_page(q)

await handle_on(q)
await run_on(q)

search = q.args[q.args['#'] or default_example_name]
if search and not q.events.editor:
Expand Down
2 changes: 1 addition & 1 deletion py/h2o_lightwave/h2o_lightwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from .core import Ref, data, pack, Expando, expando_to_dict, clone_expando, copy_expando
from .server import Q, wave_serve
from .routing import on, handle_on
from .routing import on, run_on, handle_on
from .types import *
from .version import __version__

Expand Down
2 changes: 1 addition & 1 deletion py/h2o_wave/h2o_wave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
.. include:: ../../docs/index.md
"""
from .core import Site, AsyncSite, site, Page, Ref, data, pack, Expando, expando_to_dict, clone_expando, copy_expando
from .server import listen, Q, app, main
from .server import Q, app, main
from .routing import on, handle_on, run_on
from .db import connect, WaveDBConnection, WaveDB, WaveDBError
from .types import *
Expand Down
4 changes: 2 additions & 2 deletions py/h2o_wave/project_templates/header.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from h2o_wave import main, app, Q, ui, on, handle_on
from h2o_wave import main, app, Q, ui, on, run_on


@app('/')
Expand All @@ -9,7 +9,7 @@ async def serve(q: Q):
q.client.initialized = True

# Other browser interactions
await handle_on(q)
await run_on(q)
await q.page.save()


Expand Down
109 changes: 51 additions & 58 deletions py/h2o_wave/project_templates/header_nav.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from h2o_wave import main, app, Q, ui, on, handle_on, data
from h2o_wave import main, app, Q, ui, on, run_on, data
from typing import Optional, List


Expand Down Expand Up @@ -112,65 +112,58 @@ async def page3(q: Q):


@on('#page4')
async def handle_page4(q: Q):
@on('page4_reset')
async def page4(q: Q):
q.page['sidebar'].value = '#page4'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card instead of recreating it every time, so ignore 'form' card on drop.
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q, ['form'])

if q.args.step1:
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1'),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 1'),
ui.buttons(justify='end', items=[
ui.button(name='step2', label='Next', primary=True),
])
]
elif q.args.step2:
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 2'),
ui.buttons(justify='end', items=[
ui.button(name='step1', label='Cancel'),
ui.button(name='step3', label='Next', primary=True),
])
]
elif q.args.step3:
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2', done=True),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox3', label='Textbox 3'),
ui.buttons(justify='end', items=[
ui.button(name='step2', label='Cancel'),
ui.button(name='submit', label='Next', primary=True),
])
]
else:
# If first time on this page, create the card.
add_card(q, 'form', ui.form_card(box='vertical', items=[
ui.stepper(name='stepper', items=[
ui.step(label='Step 1'),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox1', label='Textbox 1'),
ui.buttons(justify='end', items=[
ui.button(name='step2', label='Next', primary=True),
]),
]))
# If first time on this page, create the card.
add_card(q, 'form', ui.form_card(box='vertical', items=[
ui.stepper(name='stepper', items=[
ui.step(label='Step 1'),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox1', label='Textbox 1'),
ui.buttons(justify='end', items=[
ui.button(name='page4_step2', label='Next', primary=True),
]),
]))


@on()
async def page4_step2(q: Q):
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 2'),
ui.buttons(justify='end', items=[
ui.button(name='page4_step3', label='Next', primary=True),
])
]


@on()
async def page4_step3(q: Q):
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2', done=True),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox3', label='Textbox 3'),
ui.buttons(justify='end', items=[
ui.button(name='page4_reset', label='Finish', primary=True),
])
]


async def init(q: Q) -> None:
Expand Down Expand Up @@ -213,6 +206,6 @@ async def serve(q: Q):
q.client.initialized = True

# Handle routing.
await handle_on(q)
await run_on(q)
await q.page.save()

108 changes: 50 additions & 58 deletions py/h2o_wave/project_templates/header_sidebar_nav.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from h2o_wave import main, app, Q, ui, on, handle_on, data
from h2o_wave import main, app, Q, ui, on, run_on, data
from typing import Optional, List


Expand Down Expand Up @@ -117,66 +117,58 @@ async def page3(q: Q):


@on('#page4')
async def handle_page4(q: Q):
@on('page4_reset')
async def page4(q: Q):
q.page['sidebar'].value = '#page4'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card instead of recreating it every time, so ignore 'form' card on drop.
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q, ['form'])

if q.args.step1:
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1'),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 1'),
ui.buttons(justify='end', items=[
ui.button(name='step2', label='Next', primary=True),
])
]
elif q.args.step2:
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 2'),
ui.buttons(justify='end', items=[
ui.button(name='step1', label='Cancel'),
ui.button(name='step3', label='Next', primary=True),
])
]
elif q.args.step3:
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2', done=True),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox3', label='Textbox 3'),
ui.buttons(justify='end', items=[
ui.button(name='step2', label='Cancel'),
ui.button(name='submit', label='Next', primary=True),
])
]
else:
# If first time on this page, create the card.
add_card(q, 'form', ui.form_card(box='vertical', items=[
ui.stepper(name='stepper', items=[
ui.step(label='Step 1'),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox1', label='Textbox 1'),
ui.buttons(justify='end', items=[
ui.button(name='step2', label='Next', primary=True),
]),
]))
# If first time on this page, create the card.
add_card(q, 'form', ui.form_card(box='vertical', items=[
ui.stepper(name='stepper', items=[
ui.step(label='Step 1'),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox1', label='Textbox 1'),
ui.buttons(justify='end', items=[
ui.button(name='page4_step2', label='Next', primary=True),
]),
]))


@on()
async def page4_step2(q: Q):
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 2'),
ui.buttons(justify='end', items=[
ui.button(name='page4_step3', label='Next', primary=True),
])
]


@on()
async def page4_step3(q: Q):
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2', done=True),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox3', label='Textbox 3'),
ui.buttons(justify='end', items=[
ui.button(name='page4_reset', label='Finish', primary=True),
])
]


async def init(q: Q) -> None:
Expand Down Expand Up @@ -227,6 +219,6 @@ async def serve(q: Q):
q.client.initialized = True

# Handle routing.
await handle_on(q)
await run_on(q)
await q.page.save()

Loading

0 comments on commit 73313cf

Please sign in to comment.