Calling Python code from C#

David Thielen 2,956 Reputation points
2024-08-07T23:03:00.6233333+00:00

Hi;

I may be asking this totally wrong and if so, please let me know. We have a web app written in C#. It's a Blazor Server app. And in it we are creating a recommendation engine.

The person writing the recommendation engine is doing so in Python using the usual suspects: pandas, numpy, scipy, & sklearn.

Very basic questions:

  1. How do we call this code from C#? Preferably where the Python code is run on a GPU.
  2. How do we provide the data, that's in an Azure SQL Database, to the Python code?

And if there's a sample out there that does all this, that would be great. I don't care what the sample does as long as it is C# calling Python and accesses a database.

And the simpler the better so I'm not lost in the guts of the Python code. Learning how to get all this working together is going to be enough of a challenge. (If it uses AI Search, that will be awesome, but I'll take simple over Search.)

thanks - dave

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
948 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,782 questions
{count} votes

Accepted answer
  1. Konstantinos Passadis 19,066 Reputation points MVP
    2024-08-08T22:30:03.4566667+00:00

    Hello @David Thielen

    There are a few primary approaches to consider:

    Python.NET: This library allows you to embed the Python interpreter directly within your C# application. This provides tight integration but can add some complexity to your deployment.

    Process execution: You can use System.Diagnostics.Process to execute your Python script as a separate process. This is simpler to set up but involves inter-process communication (IPC) for data exchange.

    REST API: Wrap your Python code in a simple web service (e.g., using Flask or FastAPI). Your C# app can then make HTTP requests to interact with the recommendation engine. This provides a clear separation of concerns but adds some overhead.

    GPU execution

    If your Python code utilizes GPU acceleration (e.g., through TensorFlow or PyTorch), you'll need to ensure your execution environment has access to a compatible GPU.

    Cloud: If you're deploying to the cloud (like Azure), consider using a virtual machine or container instance with GPU capabilities.

    On-premises: Ensure the machine running your Python code has the necessary GPU hardware and drivers.

    Providing data from Azure SQL Database

    You'll need to establish a connection from your Python code to your Azure SQL Database.

    1. Install necessary libraries: pip install pyodbc or pip install sqlalchemy

    Establish connection: Use the appropriate library to connect to your database using your connection string.

    Retrieve data: Execute SQL queries to fetch the necessary data for your recommendation engine.

    There are a few primary approaches to consider:

    Python.NET: This library allows you to embed the Python interpreter directly within your C# application. This provides tight integration but can add some complexity to your deployment.

    Process execution: You can use System.Diagnostics.Process to execute your Python script as a separate process. This is simpler to set up but involves inter-process communication (IPC) for data exchange.

    REST API: Wrap your Python code in a simple web service (e.g., using Flask or FastAPI). Your C# app can then make HTTP requests to interact with the recommendation engine. This provides a clear separation of concerns but adds some overhead.

    GPU execution

    If your Python code utilizes GPU acceleration (e.g., through TensorFlow or PyTorch), you'll need to ensure your execution environment has access to a compatible GPU.

    Cloud: If you're deploying to the cloud (like Azure), consider using a virtual machine or container instance with GPU capabilities.

    On-premises: Ensure the machine running your Python code has the necessary GPU hardware and drivers.

    Providing data from Azure SQL Database

    You'll need to establish a connection from your Python code to your Azure SQL Database.

    1. Install necessary libraries: pip install pyodbc or pip install sqlalchemy
    2. Establish connection: Use the appropriate library to connect to your database using your connection string.
    3. Retrieve data: Execute SQL queries to fetch the necessary data for your recommendation engine.

    --

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.