In these series of posts I'll be using IronPython 2.7 (which is in beta right now) and Silverlight 4 .
Once these packages are installed the first step is to copy a basic program template to a your work directory. The template is located in
This template contains the following structure:
app.xamlfiles contain the code for the entry point of the demo Silverlight application. The
index.htmlfile contains the Silverlight control host.
app.xamlcode looks like this:
<Grid x:Name="layout_root" Background="White">
<TextBlock x:Name="Message" FontSize="30" />
app.pycode looks like this:
from System.Windows import Application
from System.Windows.Controls import UserControl
root = Application.Current.LoadRootVisual(UserControl(), "app.xaml")
root.Message.Text = "Welcome to Python and Silverlight!"
As you can see this code loads the XAML file for defining the 'root visual' . The 'root.Message.Text = ...' assignment changes the text displayed by the 'Message' TextBlock instance. Notice that it uses the name of the control as if it were a member of the UserControl instance returned by LoadRootVisual. This is accomplished by using a DLR feature defined in the
FrameworkElementExtensionclass (see ExtensionTypes.cs )
You can easily run this example by going to the command line and running Chiron:
Chiron - Silverlight Dynamic Language Development Utility. Version 188.8.131.52
Chiron serving 'C:\development\blog\ipy\basictest' as http://localhost:2060/
21:03:45 200 1,185 /
21:03:45 200 792 /style.css!
21:03:45 200 2,492 /sl.png!
21:03:45 200 642 /slx.png!
21:03:49 404 580 /favicon.ico [Resource not found]
Running Chiron this way will start the web server and will open a web browser in its root.
(Note: For IronPython 2.7 Beta you have to copy the
System.Numerics.dllassembly in the %IPY_HOME\Silverlight\bin folder, this file could be found in the Silverlight SDK distribution).
When you select the
index.htmlfile the Silverlight program is executed.
As you can see an IronPython REPL console is activated by default in this template. This console is activated by using the following tag in the HTML file.
<param name="initParams" value="reportErrors=errorLocation, console=true" />
The nice thing about this is that you can manipulate the Silverlight application while is executing. To see an example of this, we can change the definition of the
Appclass to be like this:
self.root = Application.Current.LoadRootVisual(UserControl(), "app.xaml")
self.root.Message.Text = "Welcome to Python and Silverlight!"
theApp = App()
With this change we can now have access to the UI elements defined in XAML. For example:
For future posts I'm going to talk about specific topics on the use of Silverlight and IronPython.
A nice source of information on this topic is the following page by Michael Foord "Python in your Browser with Silverlight"