IronPython for ASP.NET CTP

A few months back I blogged about the release of IronPython v1.0.  IronPython is an implementation of the Python dynamic language for .NET.  It supports an interactive console with full dynamic compilation support and makes all .NET libraries easily available to Python programmers -- while still maintaining full compatibility with the Python language.

IronPython provides a great example of how .NET languages can leverage the new dynamic language features that we added into the CLR with last year's .NET 2.0 release.  For example, CLR features like "lightweight code-generation" now make it possible for a dynamic language to quickly compile and JIT source in-memory (giving you really fast code execution without ever having to generate or persist a .dll file).  CLR 2.0 also now has the ability to garbage collect out JIT'd code (meaning you can quickly adapt types on the fly and not have to leak code). 

Last week we released a really cool CTP that provides IronPython integration support within ASP.NET and Visual Web Developer Express (which is of course free).  Once installed you can create new projects and pages using Python as your language and easily take advantage of dynamic language scripting support:

Once the IronPython for ASP.NET CTP download is installed, Visual Studio/Visual Web Developer will provide both inline code and code-behind page support for Python with ASP.NET -- with syntax highlighting, WYSIWYG designer, and full debugging support.  You can also use it to create standalone python module files that live under the App_Script directory, and which can be used across a site.

The easiest way to explore Python usage with it is to choose the File->New Web Site menu item and create the Python edition of the "Personal Starter Kit" sample site (note the .py code-behind files in the screen-shot below):

The Personal Starter Kit Python example above provides a cool way to start to learn Python, as well as take advantage of some of the dynamic language capabilities that Python provides (as well as the really nice integration within ASP.NET that the IronPython team has added). 

For example, the Photos.aspx page contains support for allowing an administrator to upload a new photo into an album.  This is implemented using a FormView control with a templated insert mode UI like so: 

<asp:FormView ID="FormView1" DefaultMode="insert" OnItemInserting="FormView1_ItemInserting" runat="server">
    
<InsertItemTemplate>

        Enter Photo: 
<asp:FileUpload ID="PhotoFile" … />
        Enter Caption: <
asp:TextBox ID="PhotoCaption" … />

        ....

    </
InsertItemTemplate>
</asp:FormView>

In a strongly-typed language you would typically need to use the "FindControl()" method of FormView1 in order to reach into its template to retrieve a reference to the FileUpload or TextBox control, and then cast it the returned object type in order to use it. With a dynamic language like Python you can instead just write the below code in your Photos.aspx.py code-behind file:

import PhotoManager

def FormView1_ItemInserting(sender, e):

    caption 
FormView1.PhotoCaption.Text
    bytes 
FormView1.PhotoFile.FileBytes
    
    
if len(bytes) == 0:
        e.Cancel 
True
    
else:
        PhotoManager.AddPhoto(Request.AlbumID, caption, bytes)

Note how you can just write "FormView1.PhotoCaption" to access the sub-control within the template, and then reference subproperties off of it directly.  This technique can also be used with templated controls like DataLists, Repeaters, Wizards, etc.  Pretty neat.

How to Learn More About IronPython

Check out the IronPython for ASP.NET home-page to learn more about the IronPython for ASP.NET CTP download.  You can also read a great whitepaper that David Ebbo wrote that describes the changes made to the ASP.NET parser to better support dynamic languages and compilation here (these can be used by both IronPython and any other dynamic language). 

Lastly, to learn more about IronPython itself I'd recommend watching this great videocast (with demos) that Jod Udell did with Jim Hugunin back in September.  Jim codes up some cool demos on the fly, including building a calculator within WPF using IronPython that integrates with the Speech APIs.  He also then shows how you can optionally refactor performance critical code from Python into strongly typed languages like C# and seamlessly work across the two.  You can now use all of the same techniques with IronPython and ASP.NET.

Hope this helps,

Scott

13 Comments

  • Very impressive. I like how Visual Studio can be extended this way without us having to wait for a completely new release.

    So do you get IntelliSense support when you access child controls this way? If so how do you do it, how can Visual Studio know what type of control it is?

    I hope that the kind of flexibility that IronPython offers will come to C# in the future.

  • Hi Mike,

    In the current CTP, you will not get Intellisense on child controls. This is indeed a hard problem, but I think we will be able to solve it in a future release.

    David

  • There's a similar thread for speeding up php interoperability. Is there any plans for a php implementation with .net support?

  • Hi Rob,

    The Phalanger project is working on adding PHP as a managed language on top of .NET.

    You can download the project and source code for it for free here: http://www.codeplex.com/Phalanger

    Hope this helps,

    Scott

  • Yes!!!!!!!!!!!!!!!! I love Python but this is whats needed to keep us productive bye c# ;)

  • Wonderful!

    (But if only the ASP.Net support in Boo ( boo.codehaus.org ) worked properly, we'd have strong typing (with type inference!), and Pythonic simplicity and easily accessible data structures - the best of both worlds...)

  • wow this is rally very very cool

  • The opensource cms Umbraco (which i mentioned here before) will support IronPython scripts next major release. Seeing more products using this would be great!

  • It is possible to use IronPython as scripting engine in signed app ?

  • Is there a similar IronPython integration with Visual Studio for Windows applications?

  • Hi Joseph,

    I believe we are going to be releasing a Windows project template soon that will enable you to use Python for Windows apps as well.

    Thanks,

    Scott

  • Hi Frederic,

    That looks very cool!

    Scott

  • Awesome! But how is the support for Visual Studio 2005 going? I'm thinking both ASP.NET and WinForms. I'd love to be using IronPython wherever it makes the most sense and only C# where that fits the spot. Now C# has an edge over IronPython because of the tool support, so please get IronPython "right up there" so it's an equivalent alternative to C#! :)

Comments have been disabled for this content.