|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIronPython is an implementation of Python for .NET. It provides full access to .NET libraries through the Python programming language, while also providing access to standard Python libraries. While it can be used to write standalone applications, this article is about using it as a scripting language for your application. Why a Scripting Language?
Integrating IronPythonIronPython is incredibly easy to integrate. You'll need to:
The following snippet of code shows how it has been done in the attached sample application: public Form1()
{
InitializeComponent();
InitializeIronPython();
}
private void InitializeIronPython()
{
Options.PrivateBinding = true;
pythonEngine = new PythonEngine();
MessageBoxStream s = new MessageBoxStream();
pythonEngine.SetStandardError(s);
pythonEngine.SetStandardOutput(s);
pythonEngine.AddToPath(AppDomain.CurrentDomain.BaseDirectory);
pythonEngine.Globals.Add("form", this);
pythonEngine.Globals.Add("bl", bl);
}
The sample application also has a "command prompt" at the bottom, which is actually a private void commandPrompt_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
try
{
pythonEngine.ExecuteToConsole(commandPrompt.Text);
commandPrompt.Clear();
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
It simply forwards the text to the Using IronPythonThe attached project demonstrates how to do things mentioned in the previous section. It's a pretty contrived application - it has two input Change Functionality at Run TimeThe "Custom" button executes a script named custom.py in the application's directory. That script gets the input private void customButton_Click(object sender, EventArgs e)
{
Dictionary
The custom.py script automatically gets the global objects added to the def append(x, y):
return ''.join([x,y])
form.ShowResult(int(append(input1.Text, input2.Text)))
This script simply concatenates the text in the input As another example, type the following command: form.TransformResult = lambda x : '-'.join(str(x))
Then do an Add or Subtract - you'll notice that the result has hyphens between the digits. If you look at the code for Provide "Extra" FunctionalityYou can simply type For a more interesting example, let's say that you as a developer would like to know the results generated in the current session. Assume that this is something that you wouldn't want to write as part of the application. With IronPython, you could do something like: resultTextBox = form.Controls.Find('result', True)[0]
results = []
def fn(arg1, arg2) : results.append(resultTextBox.Text)
resultTextBox.TextChanged += fn
You could then type... results
... at any point in the command prompt to get the results calculated so far. The script basically subscribes to the Live TestingUsing the input1TextBox = form.Controls.Find('input1', True)[0]
input2TextBox = form.Controls.Find('input2', True)[0]
resultTextBox = form.Controls.Find('result', True)[0]
input1TextBox.Text = '1'
input2TextBox.Text = '2'
addButton = form.Controls.Find('addButton', True)[0]
addButton.PerformClick()
print int(resultTextBox.Text) == 3
print int(resultTextBox.Text) == 4
As you can see, this piece of code gets references to the UI objects, sets values, executes an action and then verifies the result. You could also choose to expose the UI objects by adding them to Poking Around Live SystemsYou have already seen that you can access UI objects and manipulate them. You can do... form.Text = 'IronPython'
... to change the bl.LastOperation
... to view the last executed operation. ConclusionI hope this article got you interested in IronPython and integrating scripting into your application. The IronPython project is hosted at CodePlex, so that's the place to go to download and learn more about it. The command prompt in the sample application is very rudimentary and there are alternatives like IronTextBox that do a much better job. You could also add "meta" commands to your prompt that allow you to execute arbitrary python files. And please keep in mind that the attached application is a sample application, intended to demonstrate IronPython integration, so if the code is missing error checking, you know why. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||