-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,60 @@ | ||
import uvicorn | ||
from starlette.applications import Starlette | ||
from starlette.routing import Mount, WebSocketRoute | ||
from starlette.staticfiles import StaticFiles | ||
from starlette.websockets import WebSocketDisconnect | ||
from h2o_wavelite import wave_serve, ui, Q | ||
from h2o_wavelite_web import web_directory | ||
# Wizard | ||
# Create a multi-step #wizard using #form cards. | ||
# --- | ||
from h2o_wave import Q, ui, main, app, cypress, Cypress | ||
|
||
|
||
@app('/demo') | ||
async def serve(q: Q): | ||
# Paint our UI on the first page visit. | ||
if not q.client.initialized: | ||
# Create a local state. | ||
q.client.count = 0 | ||
# Add a "card" with a text and a button | ||
q.page['hello'] = ui.form_card(box='1 1 2 2', items=[ | ||
ui.text_xl('Hello world'), | ||
ui.button(name='counter', label=f'Current count: {q.client.count}'), | ||
]) | ||
if not q.client.initialized: # First visit, create an empty form card for our wizard | ||
q.page['wizard'] = ui.form_card(box='1 1 2 4', items=[]) | ||
q.client.initialized = True | ||
|
||
# Handle counter button click. | ||
if q.args.counter: | ||
# Increment the counter. | ||
q.client.count += 1 | ||
# Update the counter button. | ||
q.page['hello'].items[1].button.label = f'Current count: {q.client.count}' | ||
wizard = q.page['wizard'] # Get a reference to the wizard form | ||
if q.args.step1: | ||
wizard.items = [ | ||
ui.text_xl('Wizard - Step 1'), | ||
ui.text('What is your name?', name='text'), | ||
ui.textbox(name='nickname', label='My name is...', value='Gandalf'), | ||
ui.buttons([ui.button(name='step2', label='Next', primary=True)]), | ||
] | ||
elif q.args.step2: | ||
q.client.nickname = q.args.nickname | ||
wizard.items = [ | ||
ui.text_xl('Wizard - Step 2'), | ||
ui.text(f'Hi {q.args.nickname}! How do you feel right now?', name='text'), | ||
ui.textbox(name='feeling', label='I feel...', value='magical'), | ||
ui.buttons([ui.button(name='step3', label='Next', primary=True)]), | ||
] | ||
elif q.args.step3: | ||
wizard.items = [ | ||
ui.text_xl('Wizard - Done'), | ||
ui.text( | ||
f'What a coincidence, {q.client.nickname}! I feel {q.args.feeling} too!', | ||
name='text', | ||
), | ||
ui.buttons([ui.button(name='step1', label='Try Again', primary=True)]), | ||
] | ||
else: | ||
wizard.items = [ | ||
ui.text_xl('Wizard Example'), | ||
ui.text("Let's have a conversation, shall we?"), | ||
ui.buttons([ui.button(name='step1', label='Of course!', primary=True)]), | ||
] | ||
|
||
# Send the UI changes to the browser. | ||
await q.page.save() | ||
|
||
|
||
async def socket(ws): | ||
try: | ||
await ws.accept() | ||
await wave_serve(serve, ws.send_text, ws.receive_text) | ||
await ws.close() | ||
except WebSocketDisconnect: | ||
print('Client disconnected') | ||
|
||
|
||
startlette_app = Starlette(routes=[ | ||
WebSocketRoute('/_s/', socket), | ||
Mount("/", app=StaticFiles(directory=web_directory, html=True), name="/") | ||
]) | ||
|
||
if __name__ == '__main__': | ||
uvicorn.run(startlette_app, host='0.0.0.0', port=5000) | ||
|
||
@cypress('Walk through the wizard') | ||
def try_walk_through(cy: Cypress): | ||
cy.visit('/demo') | ||
cy.locate('step1').click() | ||
cy.locate('text').should('contain.text', 'What is your name?') | ||
cy.locate('nickname').clear().type('Fred') | ||
cy.locate('step2').click() | ||
cy.locate('text').should('contain.text', 'Hi Fred! How do you feel right now?') | ||
cy.locate('feeling').clear().type('quirky') | ||
cy.locate('step3').click() | ||
cy.locate('text').should( | ||
'contain.text', 'What a coincidence, Fred! I feel quirky too!' | ||
) |