次の方法で共有


PythonAppResourceBuilderExtensions.AddPythonApp Method

Definition

Overloads

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String[])

Adds a python application with a virtual environment to the application model.

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[])

Adds a python application with a virtual environment to the application model.

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String[])

Source:
PythonAppResourceBuilderExtensions.cs
Source:
PythonAppResourceBuilderExtensions.cs

Adds a python application with a virtual environment to the application model.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, params string[] scriptArgs);
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath, params string[] scriptArgs);
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, projectDirectory As String, scriptPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, appDirectory As String, scriptPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)

Parameters

name
String

The name of the resource.

projectDirectoryappDirectory
String

The path to the directory containing the python app files.

scriptPath
String

The path to the script relative to the app directory to run.

scriptArgs
String[]

The arguments for the script.

Returns

A reference to the IResourceBuilder<T>.

Examples

Add a python app or executable to the application model. In this example the python code entry point is located in the PythonProject directory if this path is relative then it is assumed to be relative to the AppHost directory, and the virtual environment path if relative is relative to the project directory. In the example below, if the app host directory is $HOME/repos/MyApp/src/MyApp.AppHost then the ProjectPath would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject and the virtual environment path (defaulted) would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject/.venv.

var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonApp("python-project", "PythonProject", "main.py");

builder.Build().Run(); 

Remarks

The virtual environment must be initialized before running the app. By default the virtual environment folder is expected to be named .venv and be located in the app directory. If the virtual environment is located in a different directory this default can be specified by using the AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[]) overload of this method.

The virtual environment is setup individually for each app to allow each app to use a different version of Python and dependencies. To setup a virtual environment use the python -m venv .venv command in the app directory. This will create a virtual environment in the .venv directory.

To restore dependencies in the virtual environment first activate the environment by executing the activation script and then use the pip install -r requirements.txt command to restore dependencies.

To receive traces, logs, and metrics from the python app in the dashboard, the app must be instrumented with OpenTelemetry. You can instrument your app by adding the opentelemetry-distro, and opentelemetry-exporter-otlp to your Python app.

Add a python app or executable to the application model. In this example the python code entry point is located in the PythonApp directory if this path is relative then it is assumed to be relative to the AppHost directory, and the virtual environment path if relative is relative to the app directory. In the example below, if the app host directory is $HOME/repos/MyApp/src/MyApp.AppHost then the AppPath would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonApp and the virtual environment path (defaulted) would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonApp/.venv.
var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonApp("python-app", "PythonApp", "main.py");

builder.Build().Run();

Applies to

AddPythonApp(IDistributedApplicationBuilder, String, String, String, String, String[])

Source:
PythonAppResourceBuilderExtensions.cs
Source:
PythonAppResourceBuilderExtensions.cs

Adds a python application with a virtual environment to the application model.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, string virtualEnvironmentPath, params string[] scriptArgs);
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource> AddPythonApp(this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, string appDirectory, string scriptPath, string virtualEnvironmentPath, params string[] scriptArgs);
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
static member AddPythonApp : Aspire.Hosting.IDistributedApplicationBuilder * string * string * string * string * string[] -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Python.PythonAppResource>
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, projectDirectory As String, scriptPath As String, virtualEnvironmentPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)
<Extension()>
Public Function AddPythonApp (builder As IDistributedApplicationBuilder, name As String, appDirectory As String, scriptPath As String, virtualEnvironmentPath As String, ParamArray scriptArgs As String()) As IResourceBuilder(Of PythonAppResource)

Parameters

name
String

The name of the resource.

projectDirectoryappDirectory
String

The path to the directory containing the python project files.

scriptPath
String

The path to the script to run, relative to the app directory.

virtualEnvironmentPath
String

Path to the virtual environment.

scriptArgs
String[]

The arguments for the script.

Returns

A reference to the IResourceBuilder<T>.

Examples

Add a python app or executable to the application model. In this example the python code is located in the PythonProject directory if this path is relative then it is assumed to be relative to the AppHost directory, and the virtual environment path if relative is relative to the project directory. In the example below, if the app host directory is $HOME/repos/MyApp/src/MyApp.AppHost then the ProjectPath would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject and the virtual environment path (defaulted) would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonProject/.venv.

var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonApp("python-project", "PythonProject", "main.py");

builder.Build().Run(); 

Remarks

The virtual environment is setup individually for each app to allow each app to use a different version of Python and dependencies. To setup a virtual environment use the python -m venv .venv command in the app directory. This will create a virtual environment in the .venv directory (where .venv is the name of your virtual environment directory).

To restore dependencies in the virtual environment first activate the environment by executing the activation script and then use the pip install -r requirements.txt command to restore dependencies.

To receive traces, logs, and metrics from the python app in the dashboard, the app must be instrumented with OpenTelemetry. You can instrument your app by adding the opentelemetry-distro, and opentelemetry-exporter-otlp to your Python app.

Add a python app or executable to the application model. In this example the python code is located in the PythonApp directory if this path is relative then it is assumed to be relative to the AppHost directory, and the virtual environment path if relative is relative to the app directory. In the example below, if the app host directory is $HOME/repos/MyApp/src/MyApp.AppHost then the AppPath would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonApp and the virtual environment path (defaulted) would be $HOME/repos/MyApp/src/MyApp.AppHost/PythonApp/.venv.
var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonApp("python-app", "PythonApp", "main.py");

builder.Build().Run();

Applies to