

@app.get("/")
def homepage():
    return str(
        p.html(
            p.head(
                p.title("Page Title")
            ),
            p.body(
                p.h1("Page Heading"),

                p.form(method="post", action="/")(
                    p.p("Label 1"),
                    p.input_(name="field1"),

                    p.p("Choose one"),
                    p.select(name="field2")(
                        p.option(value="option1")("Option 1"),
                        p.option(value="option2")("Option 2"),
                    ),

                    p.input_(type="submit", value="Submit"),
                )
            ),
        )
    )


@app.post("/")
def submit():
    field1 = request.form["field1"]
    field2 = request.form["field2"]

    return str(
        p.html(
            p.body(
                p.h1("Result"),
                p.p(field1),
                p.p(field2),
                p.a(href="/")("Back")
            )
        )
    )


#chess

import pyhtml as p
from flask import Flask, request, redirect

app = Flask(__name__)


def head_html(title):
    return p.head(
        p.title(title),
        p.link(rel="stylesheet", href="/static/style.css")
    )


def navbar_html():
    return p.nav(
        p.a("Home", href="/"),
        " | ",
        p.a("Add", href="/add"),
        " | ",
        p.a("Results", href="/results")
    )


@app.get("/")
def home():
    data = get_data()

    return str(
        p.html(
            head_html("Home"),
            p.body(
                navbar_html(),
                p.div(
                    p.h1("Home page"),

                    *[
                        p.p(
                            item["name"],
                            " ",
                            str(item["score"])
                        )
                        for item in data
                    ]
                )
            )
        )
    )


@app.get("/item/<item_id>")
def item_page(item_id):
    data = get_data()

    item = get_item_by_id(data, item_id)

    return str(
        p.html(
            head_html("Item page"),
            p.body(
                navbar_html(),
                p.div(
                    p.h1(item["name"]),
                    p.p(item["id"]),
                    p.p(str(item["score"]))
                )
            )
        )
    )


@app.get("/add")
def add_form():
    return str(
        p.html(
            head_html("Add"),
            p.body(
                navbar_html(),
                p.div(
                    p.h1("Add item"),

                    p.form(
                        p.p("ID"),
                        p.input(name="id"),

                        p.p("Name"),
                        p.input(name="name"),

                        p.p("Score"),
                        p.input(name="score"),

                        p.br(),

                        p.input(type="submit", value="Add"),

                        method="post",
                        action="/add"
                    )
                )
            )
        )
    )


@app.post("/add")
def add_submit():
    data = get_data()

    item_id = request.form["id"]
    name = request.form["name"]
    score = int(request.form["score"])

    new_item = {
        "id": item_id,
        "name": name,
        "score": score
    }

    data.append(new_item)

    save_data(data)

    return redirect("/item/" + item_id)


@app.get("/results")
def results():
    data = get_data()

    best = max(data, key=lambda x: x["score"])

    return str(
        p.html(
            head_html("Results"),
            p.body(
                navbar_html(),
                p.div(
                    p.h1("Results"),
                    p.p("Best: " + best["name"]),
                    p.p("Score: " + str(best["score"]))
                )
            )
        )
    )


@app.get("/clear")
def clear():
    save_data([])
    return redirect("/")


if __name__ == "__main__":
    app.run(port=5001)

#crochet

import pyhtml as p
from flask import Flask, request

app = Flask(__name__)


@app.get("/")
def homepage():

    return str(
        p.html(

            p.head(
                p.title("Website")
            ),

            p.body(

                p.h1("Website"),

                p.form(

                    p.p("Field 1"),
                    p.input(name="field1"),

                    p.p("Field 2"),
                    p.input(name="field2"),

                    p.input(
                        type="submit",
                        value="Submit"
                    ),

                    method="post",
                    action="/"
                )
            )
        )
    )


@app.post("/")
def submit():

    field1 = request.form["field1"]
    field2 = request.form["field2"]

    result = field1 + field2

    return str(
        p.html(

            p.head(
                p.title("Result")
            ),

            p.body(

                p.h1("Result"),

                p.p(str(result)),

                p.a(
                    href="/"
                )("Back")
            )
        )
    )


if __name__ == "__main__":
    app.run(port=5001)

#cookies




@app.get("/")
def home():

    data = json.loads(
        request.cookies.get("data", "[]")
    )

    return str(
        p.html(
            p.body(

                p.form(

                    p.input(name="text"),

                    p.input(
                        type="submit"
                    ),

                    method="post"
                ),

                *[
                    p.p(x)
                    for x in data
                ]
            )
        )
    )


@app.post("/")
def submit():

    data = json.loads(
        request.cookies.get("data", "[]")
    )

    text = request.form["text"]

    data.append(text)

    response = make_response(
        redirect("/")
    )

    response.set_cookie(
        "data",
        json.dumps(data)
    )

    return response


if __name__ == "__main__":
    app.run(port=5001)



#css


body {
    font-family: Arial;
    margin: 30px;
}

h1 {
    color: darkblue;
}

form {
    margin-top: 20px;
    margin-bottom: 20px;
}

input, select {
    margin: 5px;
    padding: 5px;
}

p {
    margin: 8px 0;
}

a {
    color: blue;
}


#css use

return str(
    p.html(

        p.head(
            p.title("Website"),

            p.link(
                rel="stylesheet",
                href="/static/style.css"
            )
        ),

        p.body(
            ...
        )
    )
)

#json
import pyhtml as p
import json
from flask import Flask, request

app = Flask(__name__)


def get_users():
    with open("users.json") as f:
        return json.load(f)


@app.get("/")
def login():
    return str(
        p.html(
            p.body(
                p.h1("Login"),
                p.form(
                    p.input(name="username"),
                    p.input(name="password"),
                    p.input(type="submit"),
                    method="post"
                )
            )
        )
    )


@app.post("/")
def login_post():
    users = get_users()

    username = request.form["username"]
    password = request.form["password"]

    if username in users and users[username] == password:
        return str(
            p.html(
                p.body(
                    p.h1("Home"),
                    p.p("Hello " + username)
                )
            )
        )

    return "Login failed"


if __name__ == "__main__":
    app.run(port=5001)
