IronPython for ASP.NET CTP
page 1 of 4
Published: 16 Nov 2006
Unedited - Community Contributed
Abstract
This article discusses the usage of IronPython with ASP.NET.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21812/ 32

Introduction

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

Figure 1

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

Figure 2

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: 

Listing 1

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

Listing 2

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.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 6:13:56 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search