Republished with Permission - Original Article
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
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">
Enter Photo: <asp:FileUpload ID="PhotoFile" … />
Enter Caption: <asp:TextBox ID="PhotoCaption" … />
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
def FormView1_ItemInserting(sender, e):
caption = FormView1.PhotoCaption.Text
bytes = FormView1.PhotoFile.FileBytes
if len(bytes) == 0:
e.Cancel = True
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.