technique:python:quart

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
technique:python:quart [2024/03/02 20:51] francoisatechnique:python:quart [2024/04/02 15:08] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
-====== Python et quart ====== +**Cette page est obsolèteVeuillez accéder au contenu à son nouvel emplacement : [[https://bibliotech.cemea.org/books/outils-technique/page/quart|quart]]**
- +
-Framwork Python pour Flask-like ! +
- +
-serveur web, websocket, jinja templates, et des trucs pour de l'"asynchrone que c'est bien"(asyncio) +
- +
-[[https://quart.palletsprojects.com/en/latest/|https://quart.palletsprojects.com/en/latest/]] +
- +
-===== Installation ===== +
- +
-Dans un virtualenv : +
-<code> +
- +
-pip install quart +
- +
-</code> +
- +
-Et aussi, si on veut ajouter les variables d'environnement : +
- +
-<code> +
-pip install python-dotenv +
- +
-</code> +
- +
-===== Démarrage ===== +
- +
-Basique, simple : +
- +
-<code python> +
-from quart import Quart +
- +
-app = Quart(__name__) +
- +
-@app.route("/"+
-async def hello(): +
-    return "Hello, World!" +
- +
-if __name__ == "__main__": +
-    app.run(debug=True) +
- +
- +
-</code> +
- +
-===== Démarrage plus élaboré ===== +
- +
-<code python> +
-from quart import Quart, render_template, request, url_for, current_app, redirect +
- +
-async def admin() -> str: +
-    if request.method == "GET": +
-        return await render_template('admin.html',appconfig=current_app.config) +
-    if request.method == "POST": +
-        form = await request.form +
-    try: +
-        actionadmin = form["actionadmin"] +
-    except BadRequestKeyError: +
-        actionadmin = "" +
-    return await render_template('admin.html',actionadmin=actionadmin,appconfig=current_app.config) +
- +
-async def index() -> str: +
-    return await render_template('index.html',appconfig=current_app.config) +
- +
-def create_app(mode='Development') -> Quart: +
-    app = Quart(__name__) +
-    app.config.from_object(f"config.{mode}"+
-    with open("settings.toml", "r") as f: +
-        app.config.update(toml.load(f)) +
-    app.add_url_rule("/", view_func=index, methods=['GET']) # fonction index +
-    # GET is allowed here only to redirect to / +
-    app.add_url_rule("/admin/", view_func=admin, methods=['POST', 'GET']) # fonction admin +
-    return app +
- +
-if __name__ == '__main__': +
-    app = create_app() +
-    app.run(host='127.0.0.1', debug=True) +
- +
- +
-</code> +
- +
-Et démarrage en mode production avec : +
- +
-<code> +
-QUART_APP=app:create_app && quart run +
- +
-</code> +
- +
-Et démarrage en mode Dev avec : +
- +
-<code> +
-QUART_APP=app:create_app && QUART_DEBUG=1 && quart run +
- +
-</code> +
- +
-Ou à placer dans un .env : +
- +
-<code> +
-QUART_APP=app:create_app +
-QUART_DEBUG=1 +
- +
-</code> +
- +
-et lancer un : +
- +
-<code> +
-source .env && quart run +
- +
-</code> +
- +
  • technique/python/quart.1709409101.txt.gz
  • Dernière modification : 2024/03/02 20:51
  • de francoisa