### Python Hello World App for Quart Source: https://quart.palletsprojects.com/en/latest/tutorials/quickstart A simple Quart application that returns 'hello' when accessed at the root URL. This serves as a basic example for getting started with Quart. ```python from quart import Quart app = Quart(__name__) @app.route('/') async def hello(): return 'hello' app.run() ``` -------------------------------- ### Start Quart Application Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Starts the Quart web server using the defined 'start' script via Poetry. ```bash poetry run start ``` -------------------------------- ### Start Quart App with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Starts the Quart video server application using the configured 'start' script via Poetry. ```console poetry run start ``` -------------------------------- ### Quart Hello World App Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/quickstart A minimal Quart application that returns 'hello' when the root URL is accessed. This is the core Python code for the web service. ```python from quart import Quart app = Quart(__name__) @app.route('/') async def hello(): return 'hello' app.run() ``` -------------------------------- ### Configure Poetry Script to Start App Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Configures a script named 'start' in the `pyproject.toml` file to run the Quart application. This allows the application to be started with a simple command. ```toml [tool.poetry.scripts] start = "blog:run" ``` -------------------------------- ### Start Quart App with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Starts the Quart application using the 'start' script defined in `pyproject.toml`. This command executes the `run` function within the blog module. ```console poetry run start ``` -------------------------------- ### Start Quart Application Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Starts the Quart web server using the configured script in pyproject.toml. ```shell poetry run start ``` -------------------------------- ### Start Quart Chat App Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial Starts the Quart chat application using the defined 'start' script via Poetry. ```bash poetry run start ``` -------------------------------- ### Install Quart using pip Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/installation Installs the Quart web framework using pip. This is the basic installation command. ```console pip install quart ``` -------------------------------- ### Start Quart App with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Starts the Quart API server using the configured 'start' script via Poetry. ```console poetry run start ``` -------------------------------- ### Start Quart App with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/api_tutorial Starts the Quart application using the configured 'start' script via Poetry. ```bash poetry run start ``` -------------------------------- ### Configure Start Script in pyproject.toml Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Configures a 'start' script in pyproject.toml to easily run the Quart application using Poetry. ```toml [tool.poetry.scripts] start = "video:run" ``` -------------------------------- ### Running Quart App via Quart CLI Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/quickstart Shows how to run a Quart application using the Quart command-line interface by setting the QUART_APP environment variable. ```console $ export QUART_APP=hello-world:app $ quart run ``` -------------------------------- ### Define Start Script in pyproject.toml Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Configures a 'start' script in pyproject.toml to easily run the Quart application using Poetry. ```toml [tool.poetry.scripts] start = "video:run" ``` -------------------------------- ### Running Quart App via Python Script Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/quickstart Demonstrates how to execute a Quart application directly using the Python interpreter. ```console python hello-world.py ``` -------------------------------- ### Testing Quart App with cURL Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/quickstart Provides a command-line instruction using cURL to send a request to the running Quart application and verify its response. ```sh curl localhost:5000 ``` -------------------------------- ### Define Start Script in pyproject.toml Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial Configures a 'start' script in pyproject.toml to easily run the Quart chat application. ```toml [tool.poetry.scripts] start = "chat:run" ``` -------------------------------- ### Start Chat Server with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Starts the Quart chat server using a script defined in 'pyproject.toml'. The 'start' script is configured to run the 'run' function from the 'chat' package. ```console poetry run start ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Installs all project dependencies, including Quart, as defined in the pyproject.toml file using Poetry. ```console poetry install ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Installs Poetry, a dependency management tool for Python projects, using pip. ```shell pip install poetry ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Installs Poetry, a dependency management tool for Python projects, using pip. ```console pip install poetry ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Installs Poetry, a dependency management tool for Python projects, using pip. ```bash pip install poetry ``` -------------------------------- ### Install Quart Source: https://quart.palletsprojects.com/en/latest/tutorials/installation Installs the Quart web framework using pip. Quart requires Python 3.9 or higher. ```bash pip install quart ``` -------------------------------- ### Quart App Example Source: https://quart.palletsprojects.com/en/latest/tutorials/deployment A basic Quart application with a single route that returns 'Hello World'. This serves as a minimal example for demonstrating Quart's functionality. ```python from quart import Quart app = Quart(__name__) @app.route('/') async def hello(): return 'Hello World' ``` -------------------------------- ### Configure Poetry Scripts for Blog Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Defines scripts in pyproject.toml for starting the Quart application and initializing the database. ```toml [tool.poetry.scripts] start = "blog:run" ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Installs all project dependencies defined in pyproject.toml using Poetry, ensuring the environment is set up correctly. ```bash poetry install ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Installs the Poetry dependency management tool using pip. This is a prerequisite for managing project dependencies in the tutorial. ```console pip install poetry ``` -------------------------------- ### Install Quart with dotenv support Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/installation Installs Quart along with the 'dotenv' extra, which includes the 'python-dotenv' package for automatic environment variable loading. ```console pip install quart[dotenv] ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Installs all project dependencies defined in pyproject.toml using Poetry, ensuring the correct versions are set up. ```shell poetry install ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Installs all project dependencies defined in the pyproject.toml file using Poetry. This command ensures that the project has all necessary libraries, including Quart. ```console poetry install ``` -------------------------------- ### Configure Start Script in pyproject.toml Source: https://quart.palletsprojects.com/en/latest/tutorials/api_tutorial Configures a 'start' script in pyproject.toml to easily run the Quart application using Poetry. ```toml [tool.poetry.scripts] start = "api:run" ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial Installs Poetry, a dependency management tool for Python projects, using pip. ```bash pip install poetry ``` -------------------------------- ### Configure Start Script in pyproject.toml Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Configures a 'start' script in pyproject.toml to easily run the Quart application using Poetry. ```toml [tool.poetry.scripts] start = "api:run" ``` -------------------------------- ### Test Quart App Locally with `curl` Source: https://quart.palletsprojects.com/en/latest/_sources/how_to_guides/developing Provides an example of using the `curl` command-line tool to test a locally running Quart application. It demonstrates making a GET request to the root URL (`/`) with verbose output enabled using the `-v` flag. ```console $ curl -v localhost:5000/ ``` -------------------------------- ### Configure Start Script in pyproject.toml Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Configures a 'start' script in the 'pyproject.toml' file for the Poetry project. This script is used to run the Quart chat application. ```toml [tool.poetry.scripts] start = "chat:run" ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/api_tutorial Installs Poetry, a dependency management and packaging tool for Python, using pip. ```bash pip install poetry ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial Installs all project dependencies defined in pyproject.toml using Poetry, ensuring paths are correct. ```bash poetry install ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Installs Poetry, a dependency management tool for Python projects, using pip. ```console pip install poetry ``` -------------------------------- ### Define Quart App and Run Function Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Defines the main Quart application instance and a `run` function to start the server. This code is typically placed in the project's main application file. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Update Poetry Scripts for Database Initialization Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Updates the `pyproject.toml` file to include a new script 'init_db' for initializing the database, alongside the existing 'start' script. ```toml [tool.poetry.scripts] init_db = "blog:init_db" start = "blog:run" ``` -------------------------------- ### Quart App Initialization Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Initializes a basic Quart application and defines a run function to start the server. This is the core of the web server. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Define Quart App and Run Function Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Creates a Quart application instance and defines a 'run' function to start the server. This is the main entry point for the chat application. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Install pytest-asyncio Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Installs pytest-asyncio as a development dependency using Poetry, required for running asynchronous tests. ```console poetry add --dev pytest-asyncio ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Installs all project dependencies defined in 'pyproject.toml' using Poetry. This command ensures the project has all necessary libraries. ```console poetry install ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/api_tutorial Installs all project dependencies defined in pyproject.toml using Poetry, ensuring paths are correct. ```bash poetry install ``` -------------------------------- ### Install Quart-Schema with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Installs the Quart-Schema extension using Poetry to enable schema validation and OpenAPI documentation. ```console poetry add quart-schema ``` -------------------------------- ### Quart App Initialization Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Initializes a basic Quart application and defines a run function to start the server. This is the core of the web server. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Install pytest-asyncio with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Installs the pytest-asyncio development dependency using the Poetry package manager. ```console poetry add --dev pytest-asyncio ``` -------------------------------- ### Install Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Installs the Poetry dependency management tool using pip. Poetry is used for creating and managing Python projects. ```console pip install poetry ``` -------------------------------- ### Install pytest-asyncio Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial This console command installs the pytest-asyncio package, which is required for running asynchronous tests with pytest. ```console poetry add --dev pytest-asyncio ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Installs all project dependencies defined in pyproject.toml using Poetry, ensuring correct paths and availability. ```console poetry install ``` -------------------------------- ### Initialize Quart App Source: https://quart.palletsprojects.com/en/latest/tutorials/api_tutorial Initializes a Quart application instance and defines a run function to start the server. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Install pytest-asyncio (Bash) Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial This command installs the pytest-asyncio package as a development dependency using Poetry. This package is necessary for running asynchronous tests with pytest. ```bash poetry add --dev pytest-asyncio ``` -------------------------------- ### Install Pytest-Asyncio Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial This command installs `pytest-asyncio` as a development dependency using poetry. This package is required for running asynchronous tests with pytest. ```bash poetry add --dev pytest-asyncio ``` -------------------------------- ### Install pytest-asyncio Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial This console command installs the pytest-asyncio development dependency using Poetry, which is necessary for running asynchronous tests with pytest. ```console poetry add --dev pytest-asyncio ``` -------------------------------- ### Running a Quart App Source: https://quart.palletsprojects.com/en/latest/tutorials/quickstart Demonstrates two methods for running a Quart application: directly using the Python interpreter or by setting an environment variable and using the Quart CLI. Both methods are suitable for development. ```bash python hello-world.py ``` ```bash $ export QUART_APP=hello-world:app $ quart run ``` -------------------------------- ### Install Quart with Dotenv Support Source: https://quart.palletsprojects.com/en/latest/tutorials/installation Installs Quart with the 'dotenv' extra, which includes the python-dotenv package for automatic environment variable loading. ```bash pip install quart[dotenv] ``` -------------------------------- ### Basic Quart App Setup Source: https://quart.palletsprojects.com/en/latest/reference/cheatsheet Sets up a basic Quart application with a single route '/hello' that returns 'Hello, World!' and runs the app in debug mode. ```Python from quart import Quart app = Quart(__name__) @app.route("/hello") async def hello(): return "Hello, World!" if __name__ == "__main__": app.run(debug=True) ``` -------------------------------- ### Quart Route Handler for Creating Posts Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial This Python code defines a Quart route handler for '/create/' that accepts both GET and POST requests. For POST requests, it inserts blog post data into the database and redirects to the posts page. For GET requests, it renders the 'create.html' template. ```python from quart import redirect, request, url_for @app.route("/create/", methods=["GET", "POST"]) async def create(): if request.method == "POST": db = _get_db() form = await request.form db.execute( "INSERT INTO post (title, text) VALUES (?, ?)", [form["title"], form["text"]], ) db.commit() return redirect(url_for("posts")) else: return await render_template("create.html") ``` -------------------------------- ### Add Quart Dependency with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Adds Quart as a project dependency using Poetry. This command ensures that Quart is installed and managed within the project's virtual environment. ```console poetry add quart ``` -------------------------------- ### Add Quart Dependency with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Adds Quart as a project dependency using Poetry. This ensures Quart is installed and managed within the project's environment. ```console poetry add quart ``` -------------------------------- ### Quart Route for Displaying Posts Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Defines a GET route for the root URL ('/') that queries the database for posts and renders them using the 'posts.html' template. ```python from quart import render_template, g def _get_db(): if not hasattr(g, "sqlite_db"): g.sqlite_db = _connect_db() return g.sqlite_db @app.get("/") async def posts(): db = _get_db() cur = db.execute( """SELECT title, text FROM post ORDER BY id DESC""", ) posts = cur.fetchall() return await render_template("posts.html", posts=posts) ``` -------------------------------- ### Testing a Quart App with Curl Source: https://quart.palletsprojects.com/en/latest/tutorials/quickstart Shows how to test a running Quart application using the `curl` command. This command sends a request to the local server and displays the response. ```bash curl localhost:5000 ``` -------------------------------- ### Initialize Quart App Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Creates a basic Quart application instance and a run function for the web server. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Use curl for HTTP/2 Communication Source: https://quart.palletsprojects.com/en/latest/_sources/how_to_guides/using_http2 An example of how to use the curl command-line tool to communicate with a server over HTTP/2. Ensure that HTTP/2 support is installed for curl. ```console $ curl --http2 ... ``` -------------------------------- ### Run Database Initialization Command Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Executes the 'init_db' script using Poetry to create and update the blog's SQLite database based on the defined schema. ```console poetry run init_db ``` -------------------------------- ### Add Background Task on App Startup Source: https://quart.palletsprojects.com/en/latest/how_to_guides/background_tasks Shows how to schedule a background task to run when the Quart application starts. This is useful for initializing services or performing setup operations that can run in the background. ```python async def background_task(): ... @app.before_serving async def startup(): app.add_background_task(background_task) ``` -------------------------------- ### Load Environment Variables with Prefix in Quart Source: https://quart.palletsprojects.com/en/latest/_sources/how_to_guides/configuration Demonstrates how to load environment variables into Quart's configuration using `from_prefixed_env`. Only variables starting with the specified prefix (default 'QUART_') are loaded. This example shows setting the 'TESTING' variable. ```python app = Quart(__name__) app.config.from_prefixed_env() assert app.config["TESTING"] is True ``` -------------------------------- ### Quart SSE Streaming Route Source: https://quart.palletsprojects.com/en/latest/how_to_guides/server_sent_events An example of a Quart GET route that serves Server Sent Events. It checks for the 'text/event-stream' accept type, defines a generator to yield encoded events, and configures the response headers and timeout for streaming. ```Python from quart import abort, make_response @app.get("/sse") async def sse(): if "text/event-stream" not in request.accept_mimetypes: abort(400) async def send_events(): while True: data = ... # Up to you where the events are from event = ServerSentEvent(data) yield event.encode() response = await make_response( send_events(), { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Transfer-Encoding': 'chunked', }, ) response.timeout = None return response ``` -------------------------------- ### Run Database Initialization Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Executes the 'init_db' script to create the database and its tables. ```shell poetry run init_db ``` -------------------------------- ### Quart SSE Endpoint with Streaming Source: https://quart.palletsprojects.com/en/latest/_sources/how_to_guides/server_sent_events An example Quart GET route that serves Server-Sent Events. It checks for the 'text/event-stream' accept type, defines an asynchronous generator to yield encoded events, and configures the response with appropriate headers and disabled timeout. ```python from quart import abort, make_response @app.get("/sse") async def sse(): if "text/event-stream" not in request.accept_mimetypes: abort(400) async def send_events(): while True: data = ... # Up to you where the events are from event = ServerSentEvent(data) yield event.encode() response = await make_response( send_events(), { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Transfer-Encoding': 'chunked', }, ) response.timeout = None return response ``` -------------------------------- ### Initialize Database and Schema Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Connects to the SQLite database, executes the schema SQL, and commits the changes. ```python from pathlib import Path from sqlite3 import dbapi2 as sqlite3 app.config.update({ "DATABASE": Path(app.root_path) / "blog.db", }) def _connect_db(): engine = sqlite3.connect(app.config["DATABASE"]) engine.row_factory = sqlite3.Row return engine def init_db(): db = _connect_db() with open(Path(app.root_path) / "schema.sql", mode="r") as file_: db.cursor().executescript(file_.read()) db.commit() ``` -------------------------------- ### Create New Video Project with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Creates a new Python project structure for a video server using Poetry. ```console poetry new --src video ``` -------------------------------- ### Create New Blog Project with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Creates a new Python project structure for a blog using Poetry, with source files in a 'src' directory. ```shell poetry new --src blog ``` -------------------------------- ### Initialize Database with Schema Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Initializes the SQLite database by executing the SQL schema defined in `schema.sql`. This function reads the schema file and applies it to the database. ```python def init_db(): db = _connect_db() with open(Path(app.root_path) / "schema.sql", mode="r") as file_: db.cursor().executescript(file_.read()) db.commit() ``` -------------------------------- ### Update Poetry Scripts for Database Initialization Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Adds the 'init_db' script to pyproject.toml for creating and updating the database. ```toml [tool.poetry.scripts] init_db = "blog:init_db" start = "blog:run" ``` -------------------------------- ### Create New Python Project with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Creates a new Python project structure using Poetry, with the source code in a 'src' directory. ```bash poetry new --src video ``` -------------------------------- ### Configure Database Path and Connection Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Configures the database path for the Quart application and provides a helper function `_connect_db` to establish a connection to the SQLite database. It also sets the row factory for convenient data access. ```python from pathlib import Path from sqlite3 import dbapi2 as sqlite3 app.config.update({ "DATABASE": Path(app.root_path) / "blog.db", }) def _connect_db(): engine = sqlite3.connect(app.config["DATABASE"]) engine.row_factory = sqlite3.Row return engine ``` -------------------------------- ### Run Tests with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Executes all tests in the 'tests/' directory using Poetry, ensuring the video server functions correctly. ```console poetry run pytest tests/ ``` -------------------------------- ### Quart Startup and Shutdown Decorators Source: https://quart.palletsprojects.com/en/latest/how_to_guides/startup_shutdown Demonstrates the use of Quart's `before_serving`, `while_serving`, and `after_serving` decorators to manage application startup and shutdown logic, including database pool creation and cleanup. It also shows how to use `g` within these contexts. ```python from quart import g @app.before_serving async def create_db_pool(): app.db_pool = await ... g.something = something @app.before_serving async def use_g(): g.something.do_something() @app.while_serving async def lifespan(): ... # startup yield ... # shutdown @app.route("/") async def index(): app.db_pool.execute(...) # g.something is not available here @app.after_serving async def create_db_pool(): await app.db_pool.close() ``` -------------------------------- ### Add Quart Dependency with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/blog_tutorial Adds Quart as a project dependency using Poetry, ensuring it's available for the blog server. ```shell poetry add quart ``` -------------------------------- ### Create New Python Project with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial Creates a new Python project named 'chat' with a src layout using Poetry. ```bash poetry new --src chat ``` -------------------------------- ### Quart 0.18.0: Adopt Flask’s logging setup Source: https://quart.palletsprojects.com/en/latest/changes Quart has adopted Flask's logging setup, ensuring a consistent and familiar logging experience for developers transitioning between the frameworks. ```Python import logging logging.basicConfig(level=logging.INFO) # Quart now uses Flask's logging configuration style ``` -------------------------------- ### Quart JSON Responses Source: https://quart.palletsprojects.com/en/latest/_sources/reference/cheatsheet Provides an example of how to return JSON data directly from a Quart route handler. ```python @app.route("/hello") async def hello(): return {"Hello": "World!"} ``` -------------------------------- ### Add Quart Dependency with Poetry Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Adds Quart as a project dependency using Poetry, ensuring it's available for the video server. ```bash poetry add quart ``` -------------------------------- ### Get Response Body as JSON Source: https://quart.palletsprojects.com/en/latest/reference/source/quart Parses the response body as JSON. The `force` parameter allows parsing even if the mimetype is not JSON. ```python json_data = await response.get_json() json_data_forced = await response.get_json(force=True) ``` -------------------------------- ### Get Response Body Data Source: https://quart.palletsprojects.com/en/latest/reference/source/quart Retrieves the response body data as bytes or text. The `as_text` parameter controls the return type. ```python data_bytes = await response.get_data(as_text=False) data_text = await response.get_data(as_text=True) ``` -------------------------------- ### Quart Request Methods Source: https://quart.palletsprojects.com/en/latest/reference/cheatsheet Shows how to specify allowed HTTP request methods for routes in Quart. By default, routes accept GET requests. ```Python @app.route("/get") # GET Only by default @app.route("/get", methods=["GET", "POST"]) # GET and POST @app.route("/get", methods=["DELETE"]) # Just DELETE ``` -------------------------------- ### Add Quart Dependency with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/video_tutorial Adds Quart as a project dependency using Poetry, ensuring it's available for the video server. ```console poetry add quart ``` -------------------------------- ### Quart Request Methods Source: https://quart.palletsprojects.com/en/latest/_sources/reference/cheatsheet Illustrates how to specify allowed HTTP request methods for a given route in Quart, defaulting to GET if not explicitly defined. ```python @app.route("/get") # GET Only by default @app.route("/get", methods=["GET", "POST"]) # GET and POST @app.route("/get", methods=["DELETE"]) # Just DELETE ``` -------------------------------- ### Quart App Initialization Source: https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial Initializes a basic Quart application instance and defines a run method for the server. ```python from quart import Quart app = Quart(__name__) def run() -> None: app.run() ``` -------------------------------- ### Blueprint Context Processor Source: https://quart.palletsprojects.com/en/latest/how_to_guides/templating Example of defining a context processor specifically for a blueprint, making the provided context available only to routes within that blueprint. ```Python from quart import Blueprint blueprint = Blueprint(__name__) @blueprint.context_processor async def blueprint_only(): return {'context': 'value'} ``` -------------------------------- ### Communicate via HTTP/2 with curl Source: https://quart.palletsprojects.com/en/latest/how_to_guides/using_http2 Shows how to use the `curl` command-line tool to communicate via HTTP/2. This requires `curl` to be installed with HTTP/2 support. ```bash $ curl --http2 ... ``` -------------------------------- ### Run Quart with Hypercorn Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/deployment This snippet shows how to create a basic Quart application and run it using the Hypercorn ASGI server. Hypercorn is recommended for production deployments over Quart's development server. ```python from quart import Quart app = Quart(__name__) @app.route('/') async def hello(): return 'Hello World' ``` ```bash hypercorn example:app ``` -------------------------------- ### Application-Wide Context Processor Source: https://quart.palletsprojects.com/en/latest/how_to_guides/templating Example of defining a context processor that applies to all requests handled by the Quart application, making the provided context globally available. ```Python from quart import Blueprint blueprint = Blueprint(__name__) @blueprint.app_context_processor async def app_wide(): return {'context': 'value'} ``` -------------------------------- ### Return JSON Array with jsonify Source: https://quart.palletsprojects.com/en/latest/tutorials/api_tutorial Demonstrates using Quart's `jsonify` function to return a JSON array from a GET route. ```python from quart import jsonify @app.get("/example") async def example(): return jsonify(["a", "b"]) ``` -------------------------------- ### Create New Blog Project with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/blog_tutorial Creates a new Python project structure for the blog using the Poetry command-line tool. The `--src` flag indicates that the source code will be in a 'src' directory. ```console poetry new --src blog ``` -------------------------------- ### Test Streaming Request and Response Source: https://quart.palletsprojects.com/en/latest/how_to_guides/testing Provides an example of testing routes that handle streaming requests and responses using the test client's request method. ```python async def test_stream() -> None: test_client = app.test_client() async with test_client.request(...) as connection: await connection.send(b"data") await connection.send_complete() ... # receive a chunk of the response data = await connection.receive() ... # assemble the rest of the response without the first bit response = await connection.as_response() ``` -------------------------------- ### Quart Route for Conditional Video Serving Source: https://quart.palletsprojects.com/en/latest/tutorials/video_tutorial Implements a route for '/video.mp4' that serves the video file with conditional GET requests enabled, allowing for range requests. ```python from quart import send_file @app.route("/video.mp4") async def auto_video(): return await send_file(app.static_folder / "video.mp4", conditional=True) ``` -------------------------------- ### Serve Chat UI from Root Path Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/chat_tutorial Adds a route to the Quart application to serve the 'index.html' template for the root path ('/'). This makes the chat UI accessible to users. ```python from quart import render_template @app.get("/") async def index(): return await render_template("index.html") ``` -------------------------------- ### Basic Test Client Usage in Quart Source: https://quart.palletsprojects.com/en/latest/_sources/how_to_guides/testing Demonstrates how to create a test client for a Quart app and make a GET request to the root path, asserting the response status code. ```python async def test_app(app): client = app.test_client() response = await client.get('/') assert response.status_code == 200 ``` -------------------------------- ### Create New API Project with Poetry Source: https://quart.palletsprojects.com/en/latest/_sources/tutorials/api_tutorial Creates a new Python project structure for an API server using the Poetry command-line tool. ```console poetry new --src api ```