Skip to content

Commit 853dc7a

Browse files
authored
Update README.md
1 parent 78d26f9 commit 853dc7a

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
1-
# IronPythonExamples
2-
Getting IronPython running in C#
1+
# IronPython Integration with C#
2+
3+
This repository demonstrates how to integrate Python scripts with C# using IronPython. Two primary examples are provided to illustrate different functionalities:
4+
5+
## Installation
6+
7+
To get IronPython running in C# the IronPython package should be installed through NuGet.
8+
9+
`Install-Package IronPython -Version 2.7.10`
10+
11+
## Getting a List from Python into C#
12+
13+
This example demonstrates how to:
14+
15+
- Execute a Python script in C# that generates a list of squared numbers.
16+
- Access and retrieve variables from the executed script in C#.
17+
- Convert the Python list to a C# list and print its values.
18+
19+
To run, execute the code within the file `getting_a_list.cs`.
20+
21+
### Code Highlights:
22+
- A Python script is created as a string, and it defines a list `x_squared` using Python's list comprehension.
23+
- The list `x_squared` is then retrieved in C# and printed.
24+
- The Python list is converted to a C# list using a custom wrapper and then printed.
25+
26+
## Basic IronPython Example
27+
28+
This example demonstrates how to:
29+
30+
- Execute a Python script in C#.
31+
- Access and retrieve variables from the executed script in C#.
32+
- Use custom C# functions within the Python script.
33+
34+
To run, execute the code within the file `calling_cs_function_example.cs`.
35+
36+
### Code Highlights:
37+
- A Python script is defined as a string.
38+
- The script utilizes two C# functions, `log` and `custom_add`.
39+
- The `log` function is an alternative to Python's `print` function since `print` cannot be called directly. It prints provided arguments to the console.
40+
- The `custom_add` function is a demonstration of how C# functions can be seamlessly used within the Python script.
41+
- Variables (`x` and `y`) are created within the Python script, and their values are accessed and printed in C#.
42+
43+
---
44+
45+
**Dependencies**:
46+
- IronPython: Install via NuGet in your project. These examples used 2.7.10
47+
`Install-Package IronPython -Version 2.7.10`
48+
---
349

4-
Install IronPython with Nuget. I'm using 2.7.10 in these examples.
550

6-
Install-Package IronPython -Version 2.7.10
751

0 commit comments

Comments
 (0)