# SPDX-FileCopyrightText: 2023 MichaƂ Pokusa # # SPDX-License-Identifier: Unlicense import socketpool import wifi from adafruit_httpserver import GET, Headers, Request, Response, Server pool = socketpool.SocketPool(wifi.radio) server = Server(pool, debug=True) THEMES = { "dark": { "background-color": "#1c1c1c", "color": "white", "button-color": "#181818", }, "light": { "background-color": "white", "color": "#1c1c1c", "button-color": "white", }, } def themed_template(user_preferred_theme: str): theme = THEMES[user_preferred_theme] return f""" Cookie Example

After changing the theme, close the tab and open again. Notice that theme stays the same.

""" @server.route("/", GET) def themed_from_cookie(request: Request): """ Serve a simple themed page, based on the user's cookie. """ user_theme = request.cookies.get("theme", "light") wanted_theme = request.query_params.get("theme", user_theme) headers = Headers() headers.add("Set-Cookie", "cookie1=value1") headers.add("Set-Cookie", "cookie2=value2") return Response( request, themed_template(wanted_theme), content_type="text/html", headers=headers, cookies={} if user_theme == wanted_theme else {"theme": wanted_theme}, ) server.serve_forever(str(wifi.radio.ipv4_address))