Web Programming

Web programming tutorial code/nodes

app.py

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("web.html")

@app.route("/get-example", methods=["GET"])
def get_example():
    name = request.args.get("name", "")
    return render_template("web.html", get_name=name)

@app.route("/post-example", methods=["POST"])
def post_example():
    message = request.form.get("message", "")
    return render_template("web.html", post_message=message)

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

./templates/web.html

<!DOCTYPE html>
<html>
<head>
    <title>Web Programming Tutorial</title>
</head>

<body>

    <h1>Web Programming Basics</h1>

    <h2>1. HTML</h2>

    <p>This is a paragraph.</p>

    <ul>
        <li>This is a list item</li>
        <li>This is another list item</li>
    </ul>

    <a href="https://www.nd.edu">This is a link</a>


    <h2>2. JavaScript and the DOM</h2>

    <p id="message">Nothing has happened yet.</p>

    <button onclick="document.getElementById('message').innerHTML='You clicked the button!'">
        Click Me
    </button>


    <h2>3. GET Form</h2>

    <form action="/get-example" method="GET">
        Your name:
        <input type="text" name="name">
        <input type="submit" value="Submit">
    </form>

    {% if get_name %}
        <p>You entered: {{ get_name }}</p>
    {% endif %}


    <h2>4. POST Form</h2>

    <form action="/post-example" method="POST">
        Your message:
        <input type="text" name="message">
        <input type="submit" value="Submit">
    </form>

    {% if post_message %}
        <p>You entered: {{ post_message }}</p>
    {% endif %}

</body>
</html>