Web server

Use the small Python micro framework call Flask to create a web server. This can be used as a starting point to control devices from a web site.

A minimal web server

##
# Flask quick start
#
# To run 'sudo python <app name>', then open browser to IP address shown


from flask import Flask, render_template_string, redirect

# Create the Flask application
app = Flask(__name__)

# Create a route
@app.get("/")
def index():
    page_template = """
    <!DOCTYPE html>
    <html lang='en'>
        <head>
            <meta charset="utf-8">
            <title>Hello</title>
            <style>
                body {
                    background-color: gainboro;
                }
            </style>
    </head>
    <body>
        <h1>Hello world</h1>
    </body>
    </html>
"""
return render_template_string(page_template)

@app.get("/hello/<name>")
def say_hello(name):
    # Send a message to the console
    print(f"Hello from {name}")

    return f'Hello from {name}'

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=80)

Add a button and a link

<div>
    <a href="/hello/fred"><button>Hello</button></a>
</div>