Share via


PythonAppResourceBuilderExtensions.AddUvicornApp Method

Definition

Adds a Uvicorn-based Python application to the distributed application builder with HTTP endpoint configuration.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.UvicornAppResource> AddUvicornApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string app);
static member AddUvicornApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.UvicornAppResource>
<Extension()>
Public Function AddUvicornApp (builder As IDistributedApplicationBuilder, name As String, appDirectory As String, app As String) As IResourceBuilder(Of UvicornAppResource)

Parameters

builder
IDistributedApplicationBuilder

The distributed application builder to which the Uvicorn application resource will be added.

name
String

The unique name of the Uvicorn application resource.

appDirectory
String

The directory containing the Python application files.

app
String

The ASGI app import path which informs Uvicorn which module and variable to load as your web application. For example, "main:app" means "main.py" file and variable named "app".

Returns

A resource builder for further configuration of the Uvicorn Python application resource.

Examples

Add a FastAPI application using Uvicorn:

var builder = DistributedApplication.CreateBuilder(args);

var api = builder.AddUvicornApp("api", "../fastapi-app", "main:app")
    .WithUv()
    .WithExternalHttpEndpoints();

builder.Build().Run();

Remarks

This method configures the application to use Uvicorn as the ASGI server and exposes an HTTP endpoint. When publishing, it sets the entry point to use the Uvicorn executable with appropriate arguments for host and port.

By default, the virtual environment folder is expected to be named .venv and located in the app directory. Use WithVirtualEnvironment<T>(IResourceBuilder<T>, String, Boolean) to specify a different virtual environment path.

In non-publish mode, the --reload flag is automatically added to enable hot reload during development.

Applies to