Created
July 9, 2022 00:34
-
-
Save lo5/2d1fe22123ce84ec60c1ba9fa6e6d6cb to your computer and use it in GitHub Desktop.
A Nitro app with back button support and state handling
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
# === | |
# About: Back button and state handling demo | |
# Author: Prithvi Prabhu <prithvi.prabhu@gmail.com> | |
# License: Apache-2.0 | |
# Source: https://github.com/h2oai/nitro/py/examples | |
# Keywords: [basic, flask] | |
# | |
# Setup: | |
# FILE requirements.txt EOF | |
# flask | |
# simple-websocket | |
# h2o-nitro[web] >= 0.13 | |
# EOF | |
# RUN python -m pip install -r requirements.txt | |
# ENV FLASK_APP hello.py | |
# ENV FLASK_ENV development | |
# START python -m flask run | |
# === | |
import simple_websocket | |
from flask import Flask, request, send_from_directory | |
# ┌─────────────── Nitro app starts here ───────────────┐ | |
from h2o_nitro import View, box, option | |
from h2o_nitro_web import web_directory | |
class Person: | |
def __init__(self, first_name, last_name): | |
self.first_name = first_name | |
self.last_name = last_name | |
class State: | |
def __init__(self): | |
self.person = Person('Boaty', 'McBoatface') | |
self.father = Person('Papa', 'McBoatface') | |
self.mother = Person('Mama', 'McBoatface') | |
def ask_name(view: View): | |
person: Person = view.context['state'].person | |
person.first_name, person.last_name = view( | |
f"# Step 1", | |
box('Your first name?', value=person.first_name), | |
box('Your last name?', value=person.last_name) | |
) | |
view.jump(ask_father_name) | |
def ask_father_name(view: View): | |
father: Person = view.context['state'].father | |
father.first_name, father.last_name = view( | |
f"# Step 2", | |
box("Father's first name?", value=father.first_name), | |
box("Father's last name?", value=father.last_name) | |
) | |
view.jump(ask_mother_name) | |
def ask_mother_name(view: View): | |
mother: Person = view.context['state'].mother | |
mother.first_name, mother.last_name = view( | |
f"# Step 3", | |
box("Mother's first name?", value=mother.first_name), | |
box("Mother's last name?", value=mother.last_name) | |
) | |
view.jump(show_results) | |
def show_results(view: View): | |
state: State = view.context['state'] | |
view( | |
f"# Results", | |
f"Your name: {state.person.first_name} {state.person.last_name}.", | |
f"Your father's name: {state.father.first_name} {state.father.last_name}.", | |
f"Your mother's name: {state.mother.last_name} {state.mother.last_name}.", | |
halt=True, | |
) | |
def main(view: View): | |
view.context['state'] = State() | |
view( | |
'# Welcome to the wizard!', | |
'Use the back button, Luke!' | |
) | |
view.jump(ask_name) | |
nitro = View( | |
main, | |
title='Hello Nitro!', | |
caption='v1.0', | |
routes=[ | |
option(ask_name), | |
option(ask_father_name), | |
option(ask_mother_name), | |
option(show_results), | |
], | |
) | |
# └─────────────── Nitro app ends here ───────────────┘ | |
app = Flask(__name__, static_folder=web_directory, static_url_path='') | |
@app.route('/') | |
def home_page(): | |
return send_from_directory(web_directory, 'index.html') | |
@app.route('/nitro', websocket=True) | |
def socket(): | |
ws = simple_websocket.Server(request.environ) | |
try: | |
nitro.serve(ws.send, ws.receive) | |
except simple_websocket.ConnectionClosed: | |
pass | |
return '' | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment