Coder Social home page Coder Social logo

aspire-python's Introduction

Aspire hosting extensions for Python projects

Continuous integration NuGet Version


IMPORTANT I merged most of the code here into the .NET Aspire code base. If you want to use Python projects with Aspire, make sure to check out the docs here: https://learn.microsoft.com/en-us/dotnet/aspire/get-started/build-aspire-apps-with-python?tabs=bash


This project adds additional support for running Python projects as smoothly as C# projects.

System requirements

  • Python
  • .NET SDK 8 or higher
  • .NET Aspire workload preview 7

Getting started

Before using this extension you should know how to set up an Aspire project. There's an excellent guide in the documentation.

Once you've set up a basic Aspire project, follow the instructions below to get started.

Before you start building

These extensions make a few assumptions about your project:

  • You have a virtual environment in the .venv directory in your Python project. You can set up a virtual environment with venv, rye or poetry.
  • You have a Dockerfile in the root directory of your Python project. A sample Dockerfile can be found here

Note: I haven't added support for Anaconda because I don't use it myself. Feel free to open a PR to add support for Anaconda.

Setting up a Python project

Basic Python projects can be configured using the following code:

using FizzyLogic.Aspire.Python.Hosting;

var builder = DistributedApplication.CreateBuilder(args);

builder.AddPythonProjectWithVirtualEnvironment("batch", "apps/batch");

builder.Build().Run();

By adding a Python project resource, you'll automatically get tracing, logging, and metrics configured. Note that you need to add extra HTTP and HTTPS endpoints yourself by calling .WithHttpEndpoint or WithHttpsEndpoint.

The Python project is deployed as a container when you publish the Aspire application. You'll need a Dockerfile in the Python project directory for the publication to work properly.

Telemetry data is automatically gathered when you have the following package available in your virtual environment:

Setting up a Flask project

Flask applications differ from regular Python projects in that we'll automatically expose an HTTP endpoint for the project. You can configure a new Flask project using the following code:

using FizzyLogic.Aspire.Python.Hosting;

var builder = DistributedApplication.CreateBuilder(args);

builder.AddFlaskProjectWithVirtualEnvironment("flask-service", "apps/flask-service");

builder.Build().Run();

As with regular Python projects, the Flask project is published as a container. You'll need a Dockerfile in the Python project directory for the publication to work properly.

Telemetry data is automatically gathered when you have the following two packages available in your virtual environment:

Developing

Setting up your environment

Before you can work on the code, make sure you have .NET 8, Python, and Rye configured. After install .NET 8, run the following commands to get the correct workload setup:

dotnet workload update
dotnet workload install aspire

Running the sample in the repository

The repository contains a sample application which demonstrates various scenarios that are supported by this extension. Before running the sample, make sure you've synced the environments:

pushd sample/apps/batch && rye sync && popd
pushd sample/apps/flask-service && rye sync && popd

Syncing the environments for the various components shouldn't take too long to complete. After completing the sync step, you can run the host like so:

cd sample/host/FizzyLogic.Aspire.Python.AppHost
dotnet run

aspire-python's People

Contributors

wmeints avatar

Stargazers

Kyungjun Lee avatar  avatar  avatar Mitchell Barry avatar firstfu avatar  avatar Frans Lytzen avatar Virgil avatar Rocky LIU Yan avatar David Pendray avatar

Watchers

 avatar

aspire-python's Issues

Add unit-tests for various scenarios

Bad, bad, bad, I didn't add any unit-tests. I have a few things in mind for testing:

  • Test detection of virtual environments
  • Test detection of OTEL support
  • Test validation of various properties

Add samples for FastAPI

This is in the same area as #2 . I'll need to add a sample for FastAPI and verify that everything is working as intended.
Maybe it's fun to automatically detect the correct ports and set them up for the developer.

Add samples for Flask

This is in the same area as #2 . I'll need to add a sample for Flask and verify that everything is working as intended.
Maybe it's fun to automatically detect the correct ports and set them up for the developer.

Add samples for Django

I suspect that running Django projects is a little more complicated than a regular Python project. So I'm going to have to add a sample and maybe modify a few things to get that working correctly. Maybe it's fun to automatically detect the correct ports and set them up for the developer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.