PythonCaller 0.2.11

dotnet add package PythonCaller --version 0.2.11
                    
NuGet\Install-Package PythonCaller -Version 0.2.11
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PythonCaller" Version="0.2.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PythonCaller" Version="0.2.11" />
                    
Directory.Packages.props
<PackageReference Include="PythonCaller" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PythonCaller --version 0.2.11
                    
#r "nuget: PythonCaller, 0.2.11"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PythonCaller@0.2.11
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PythonCaller&version=0.2.11
                    
Install as a Cake Addin
#tool nuget:?package=PythonCaller&version=0.2.11
                    
Install as a Cake Tool

PythonCaller

A class library to call and get result from python file.

Use these two functions get_input() and set_output(obj) in python program by importing csconnector to receive/send object from/to the caller(C# program).
*** You must first use init environment() before using get_input() or set_output(obj). <br>

State of interprocess communication

[C#] source object
       ↓
---------------------------------------
[C#] serialize source object to json info
[C#] pass json_output_token and json info to python program
       ↓
 (can be simplified by using module csconnector)
[py] receive json_output_token and json info from stdin
[py] deserialize json info to source object
       ↓
[py] do some processes (C# can receive stdoutput from python program)
       ↓
 (can be simplified by using module csconnector)
[py] serialize result object to json info
[py] pass json_output_token and json info to standard output
       ↓
[C#] receive json_output_token and deserialize result object from json info 
---------------------------------------
       ↓
[C#] result object
(json_output_token is to identify the border between normal stdoutput and json info output)

<br>

How to use

example1

tsne.py

from sklearn.datasets import load_digits
from sklearn.manifold import TSNE
import csconnector as csc

csc.init_environment()

digits = load_digits()
tsne = TSNE(init="pca", learning_rate="auto")
digits_tsne = tsne.fit_transform(digits.data)
#                          ↓ convert ndarray to list
csc.set_output(digits_tsne.tolist())  # pass output to c#

C# test code

var engine = new Engine("tsne.py");

var result = engine.Call<List<List<double>>>();  // call python
Console.WriteLine($"Shape: ({result.Count}, {result[0].Count})");

Output

(1797, 2)

<br>

example2

Call python from string literal
'input', 'output' and 'csconnector' are reserved words.
init environment() is automatically implemented.

var scope = Scope.Create(@"
import numpy as np

ndarr = np.array(input)
ndarr.sort()
ndarr += np.array([10.0, 8.0, 15.0])
output = ndarr.tolist()  # set output
");
var engine = new Engine(scope);
var list = new List<double> { 5.0, 2.0, 3.0 };

var result = engine.Call<List<double>, List<double>>(list);
result.ForEach(i => Console.Write(i + " "));

Output

12 11 20 

<br>

example3

Define an object.

public class Weather
{
    public double Temp { get; set; }
    public string Description { get; set; }
}
class Weather(object):
    def __init__(self, temp, description):
        self.temp = temp
        self.description = description

test.py

import csconnector as csc

csc.init_environment()

input = csc.get_input()  # receive input from c#
input = Weather(**input)  # convert object to Weather object

input.temp += 12.8
input.description = "Too hot"

csc.set_output(input)  # pass output to c#

C# test code

var weather = new Weather
{
    Temp = 22.5,
    Description = "Warm"
};

var engine = new Engine("test.py");
var result = engine.Call<Weather, Weather>(weather);  // call python
Console.WriteLine(result);

Output

35.3, Too hot

example4

Receive stdoutput from python program

engine.StdOutput += str => Console.WriteLine(str)
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.11 232 7/9/2025