|
1 |
| -# PythonNet.Helper |
| 1 | +# PythonNet.Helper |
| 2 | +Bring Python to C# !!! |
| 3 | + |
| 4 | +[](https://www.nuget.org/packages/PythonNet.Helper) |
| 5 | + |
| 6 | +>This library is a help library for [PythonNet](https://github.com/pythonnet/pythonnet), if you find any bugs, please report them. Also let me know if you have any comments |
| 7 | +
|
| 8 | +## Install via NuGet |
| 9 | +To install Atrob, run the following command in the Package Manager Console : |
| 10 | +```powershell |
| 11 | +PM> Install-Package PythonNet.Helper |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | +**To use this library, you must have .NET and Python installed** |
| 16 | + |
| 17 | +To start, make an instance of PyNet as below and give the Python DLL path |
| 18 | +```csharp |
| 19 | +IPyNet pyNet = new PyNet(@"C:\Program Files\Python311\python311.dll"); |
| 20 | +``` |
| 21 | + |
| 22 | +Now you can use `RunPython` which has many overloads whose parameter list and input type are listed below |
| 23 | + |
| 24 | +|Name|Type|Description| |
| 25 | +|----|----|-----------| |
| 26 | +|pythonCode|`string`|Python code to be executed| |
| 27 | +|parameters|`IDictionary<string, object>`|The list of input parameters in the form of a dictionary, which is the key of the variable name in Python| |
| 28 | +|returnedVariableNames|`string[]`|The names of the variables that are supposed to be taken from the Python code| |
| 29 | +|returnedVariableNames|`string[]`|The names of the variables that are supposed to be taken from the Python code| |
| 30 | +|parameter|`(string key, object value)`|To pass a parameter instead of using a dictionary| |
| 31 | +|returnedVariableName|`string`|To get just one return value instead of using an array| |
| 32 | + |
| 33 | +## Samples |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +### Run |
| 38 | +```csharp |
| 39 | +pyNet.RunPython("print('Welcome To PythonNet.Helper')"); |
| 40 | +``` |
| 41 | + |
| 42 | +### Run From File |
| 43 | +You can give the address of the code file and use `ReadFile` to read the code as below |
| 44 | +```csharp |
| 45 | +pyNet.RunPython("PythonFiles/HelloWorld.py".ReadFile()); |
| 46 | +``` |
| 47 | + |
| 48 | +### By Parameters |
| 49 | +- Parameter |
| 50 | +```csharp |
| 51 | +pyNet.RunPython(@$"print('Hello ' + name)", ("name", "Mohammad Taqi")); |
| 52 | +``` |
| 53 | + |
| 54 | +- Parameters |
| 55 | +```csharp |
| 56 | + pyNet.RunPython("print(fname + ' ' + lname)", new Dictionary<string, object> |
| 57 | +{ |
| 58 | + ["fname"] = "Mohammad Taqi", |
| 59 | + ["lname"] = "Nasiri" |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +- Model parameter |
| 64 | + |
| 65 | +```csharp |
| 66 | +public class UserModel |
| 67 | +{ |
| 68 | + public string Name { get; set; } = string.Empty; |
| 69 | + public string Family { get; set; } = string.Empty; |
| 70 | + public DateTime BirthDay { get; set; } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +```csharp |
| 75 | +pyNet.RunPython("print(f'Name : {user.Name} | Family : {user.Family} | BirthDay : {user.BirthDay}')",("user",new UserModel() |
| 76 | +{ |
| 77 | + Name = "Mohammad Taqi", |
| 78 | + Family = "Nasiri", |
| 79 | + BirthDay = DateTime.Now |
| 80 | +})); |
| 81 | +``` |
| 82 | + |
| 83 | +### By returned Variables |
| 84 | + |
| 85 | +- Without parameter |
| 86 | + |
| 87 | +```csharp |
| 88 | +pyNet.RunPython("from datetime import date\n" + "today = date.today()", "today") |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +- By parameter |
| 93 | +```csharp |
| 94 | +int sumNumbers = Convert.ToInt32(pyNet.RunPython("sum = sum(nums)", ("nums", new List<int>() { 1, 5, 8, 7, 15, 20, 36 }), "sum").ToString()) |
| 95 | +``` |
| 96 | + |
| 97 | +>You can also pass multiple parameters with a dictionary |
0 commit comments