Integrating Python and .NET Using IronPython
By using the IronPython runtime library, you can run Python scripts from your .NET application. This article shows you how to get and display feedback to the user from your Python script from within a .NET application.
If you have ever had a use-case in which you need to run a Python script from a .NET application, a good way to integrate the two is to use IronPython. I had such a use-case. An organization I was working for needed to extend a pre-existing toolset that was already available through a .NET mapping application. The goal was to extend this existing toolset using code already written in Python for another set of tools not yet part of the application. After doing some research, I decided to use IronPython-and the process worked out well.
This article is based on that experience, and I will re-create the same scenario, which can be applied to your own situation.
The primary challenge for the desired solution was to get output back from the Python script that would run within the .NET application environment and display this output to the user using a .NET application dialog. To my surprise, it was easier than anticipated, and you will also learn how to do the same. The whole process is relatively easy and outlined in the following sections.
Embedding Python Scripts in .NET Applications
In this example, I show you how to embed a Python script within a Windows Desktop application. The objective is to add a new tool button to an existing application dialog. After that tool button is clicked, the application should bring up a dialog that displays the running output of a Python script to be embedded as part of that dialog's form code.
To give the example more context, the application is a desktop-mapping application that allows users to create their own maps. The new tool enables users to parse and standardize their address points on the map.
Standardizing an address simply means making sure the address has a street name, prefix or suffix, house or building number, and pre-direction or post-direction. Each address segment must follow the U.S. Post Office guide to postal addressing standards.
Assume that an existing Python script already does this; it will be the one doing the parsing in this example. The Python script will output or flash the address that is being parsed to the user on the screen using the current dialog that was triggered to appear by selecting the address parsing tool. (I do not want to get into too much detail, but only illustrate the process here.)
The code is not important, but knowing how to embed and direct the script's output to a dialog as part of the application to the screen is important. The process goes like this:
- The user launches the mapping application and opens a custom map.
- The user selects the new address standardizer tool from an existing toolset appearing at the top of the screen.
- The address standardize dialog appears with a Start button to launch the address-standardizing process.
- A text box on the form shows the address currently being parsed. These boxes go by quickly, and the addresses appear to be flashing to the user, indicating that the process is active and parsing.
- A message indicating that the process has finished is shown to the user, using the same text box as in step 4.