Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

article for python - uvicorn #2134

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/community-toolkit/hosting-python-extensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Python hosting extensions
tommasodotNET marked this conversation as resolved.
Show resolved Hide resolved
description: Learn about the .NET Aspire Community Toolkit Python hosting extensions package which provides extra functionality to the .NET Aspire Python hosting package.
ms.date: 11/19/2024
---

# .NET Aspire Community Toolkit Python hosting extensions
tommasodotNET marked this conversation as resolved.
Show resolved Hide resolved

[!INCLUDE [includes-hosting](../includes/includes-hosting.md)]

[!INCLUDE [banner](includes/banner.md)]

In this article, you learn about the .NET Aspire Community Toolkit Python hosting extensions package which provides extra functionality to the .NET Aspire [Python hosting package](https://nuget.org/packages/Aspire.Hosting.Python).
The extensions package lets you run [Uvicorn](https://www.uvicorn.org/) applications.

## Hosting integration

To get started with the .NET Aspire Community Toolkit Python hosting extensions, install the [📦 CommunityToolkit.Aspire.Hosting.Python.Extensions](https://nuget.org/packages/CommunityToolkit.Aspire.Hosting.Python.Extensions) NuGet package in the AppHost project.

### [.NET CLI](#tab/dotnet-cli)

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Hosting.Python.Extensions
```

### [PackageReference](#tab/package-reference)

```xml
<PackageReference Include="CommunityToolkit.Aspire.Hosting.Python.Extensions"
Version="*" />
```

---

For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-package) or [Manage package dependencies in .NET applications](/dotnet/core/tools/dependencies).

## Example usage

To work with Python apps, they need to be within a virtual environment. To create a virtual environment, refer to the [Initialize the Python virtual environment](../get-started/build-aspire-apps-with-python.md?tabs=powershell#initialize-the-python-virtual-environment) section.

In the _:::no-loc text="Program.cs":::_ file of your app host project, call the `AddUvicornApp` method to add a Uvicorn application to the builder.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var uvicorn = builder.AddUvicornApp("uvicornapp", "../uvicornapp-api", "main:app")
.WithHttpEndpoint(env: "PORT");
tommasodotNET marked this conversation as resolved.
Show resolved Hide resolved
```

The `PORT` environment variable is used to determine the port the Uvicorn application should listen on. By default, this port is randomly assigned by .NET Aspire. The name of the environment variable can be changed by passing a different value to the <xref:Aspire.Hosting.ResourceBuilderExtensions.WithHttpEndpoint*> method.

The Uvicorn application can be added as a reference to other resources in the AppHost project.

## See also

- [Orchestrate Python apps in .NET Aspire](../get-started/build-aspire-apps-with-Python.md)
- [.NET Aspire Community Toolkit GitHub repo](https://github.com/CommunityToolkit/Aspire)
- [Sample Python apps](https://github.com/CommunityToolkit/Aspire/tree/main/examples/python)
2 changes: 2 additions & 0 deletions docs/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ items:
href: community-toolkit/hosting-java.md
- name: Node.js extensions
href: community-toolkit/hosting-nodejs-extensions.md
- name: Python extensions
href: community-toolkit/hosting-python-extensions.md
- name: Ollama
href: community-toolkit/ollama.md
- name: Meilisearch
Expand Down
Loading