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:
<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:
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