IronPython Sample Application

By | August 15, 2014

I first learned that I had a passion for programming via an undergraduate course that all Mathematics majors pursuing the Bachelor of Science option were required to take. The course used the Python programming language which I rather enjoyed using. When I heard that their was an open-source implementation of the language that was integrated into the .NET Framework called IronPython my interest peaked. Not only did it give me an excuse to get back into Python, but also it allowed me to expand my horizons as a .NET developer. At the time of writing this, I primarily write software in statically typed C# and don’t venture much into the dynamically typed side of things.

So why would we want to use IronPython in our .NET applications? I believe the best use of IronPython is to add dynamic details into our .NET applications. Imagine if you have a scenario where you have an application that contains an area that requires custom business logic for each client. You can use IronPython to “script” out these special processes. This way the main application code can stay static and the dynamic portions can be handled by the IronPython code per the client specification. The alternative methods to solving this issue may get real ugly in comparison.

What follows is a little toy application that I wrote to experiment and learn the basics of IronPython. What makes this all possible inside .NET is the Dynamic Language Runtime (DLR) Hosting API.

Step One: Creating a class to simplify interacting with IronPython

To get started, download the IronPython installer here. Once you have IronPython set up on your machine, you may then add references to the following DLLs: IronPython.dll and Microsoft.Scripting.dll.

The three major components of the Hosting API that we are concerned with are: ScriptEngine, ScriptRuntime, and ScriptScope. The ScriptEngine class allows you to create IronPython script source, modules, scopes, etc. The ScriptRuntime is part of the IronPython ScriptEngine object and is used to execute Python code files, load assemblies, etc. Lastly, the ScriptScope is a container which holds the variables, functions, and classes and groups them together.

For this example, I wrote a simple class called PythonNet which simplifies and encapsulates much of the logic for interacting with the IronPython and DLR Hosting APIs.

Add a new class to your Project called PythonNet. Begin constructing this “interfacing” class by adding these using directives to your code:

Next we add fields for the ScriptEngine and ScriptScope instances and initialize them in the class constructor:

Add the following method CompileSourceAndExecute(string code) to the PythonNet class. This method is quite helpful is generating, compiling, and executing IronPython code.

Lastly, add the following method AddToScope(string name, object obj) to the PythonNet class. This particular method truly demonstrates the potential power of using IronPython within our .NET applications. We can add our various C# objects to our IronPython scope and then interact with them dynamically via our Python script.

Step Two: Create a WinForm to illustrate the capabilities of IronPython

For demonstration purposes, I created the following WinForm:

IronPython Demo Form Screenshot

The logic inside the WinForm class is pretty simple. Inside the constructor we register our C# objects for use via our Python code. We also add an event handler for the executeScriptButton’s Click event.

And that’s it! That’s all you need to do. Now let’s see this application in action. Add the following IronPython code to the TextBox.

Entering in the IronPython Script into the TextBox

Then click the “Execute Script” Button and see the magic before your eyes!

Executing the IronPython Script in the TextBox

Nowhere in the compiled C# source code of this application was there any logic to change the color of the Button control. We have just used IronPython to dynamically interact with our C# objects in ways that were not defined at compile time.

Step three: Improving the application

You might be thinking at this moment in time that adding those CLR references is going to get tiresome after while. You can easily sidestep this by adding the following changes to your code:

Add this to the PythonNet class:

Then make use of it in the MainForm constructor like so:

Now we no longer have to add the CLR import and references at the top of our Python script. I also took the opportunity to showcase some more of the capabilities of IronPython within our .NET application.

Improvements to our sample application - Before Execution

After clicking execute:

Improvements to our sample application - After Execution

Sources:

One thought on “IronPython Sample Application

  1. Pingback: Example of the Usefulness of IronPython | Derek Will

Leave a Reply

Your email address will not be published. Required fields are marked *