MSDN Magazine - February 2008 - (Page 22) Figure 5 DoSingleLine Parsing def DoSingleLine(self, text, forceExecute): text1 = self.le.TryExpression(text) if text1: self.WriteInputToConsole(text) try: ret = self.Engine.Evaluate(text1, self.CurrentModule) if ret is not None: self.console.write(repr(ret) + ‘\n’, ‘output’) self.CurrentModule.SetVariable(‘_’, ret) except Exception, e: self.HandleException(e) else: self.DoMultiLine(text, forceExecute) py> import System py> from System.Collections import * py> h = Hashtable() py> h[“a”] = “IronPython” py> h{“b”] = “in Silverlight” py> for e in h: print e.Key, “:”, e.Value a: IronPython b: in Silverlight While typing you would expect to see what you’re typing in. However, from a WPF point of view, this works by the first line in Application’s Initialize method: self.KeyHandler = TextInputHandler(self.root) As you can see, you can use System.Collections from IronPython, and it can act like Python hashes or .NET Framework hashes, whichever style you choose. I used this same example in the October 2006 issue of MSDN® Magazine (msdn.microsoft.com/msdnmag/issues/06/10/ CLRInsideOut), but now it’s running in a browser. Feel free to look through that article for a more detailed overview of IronPython and .NET integration, though keep in mind that the information is slightly outdated. You can find additional information at blogs.msdn.com/ironpython. This constructs the TextInputHandler, which defines textual representations of keys, and binds the OnKeyDown method of TextInputHandler to the root Canvas’s KeyDown event. On every KeyPress, it constructs a KeyPress object with a textual representation for the pressed key and tells its Target to handle the key. Target can be any class that has the HandleKey method. In this case, it is a ConsoleInput so it handles the key by inserting the textual representation through the TextBuffer object at the cursor and moves the cursor to the next column. Though that takes a bit of work, there’s nothing new here other than using WPF. However, when you press Enter, you expect to have the language compute the result of the line and display the answer. In this case, it’s doing that computation in the chosen language on the user’s browser! Now that’s the interesting part; let’s take a closer look at how it works. To start, here’s the method that handles the key in this case: def HandleKey(self, key): if key.Text == ‘\n’: self.DoInput(key.Ctrl) elif key.Text == ‘.’: self.DoCompletions() elif key.Name == ‘Up’ and self.Caret.line == 0: self.DoHistory() else: Editor.HandleKey(self, key) What about interacting with the canvas? Let’s take a look at the Application class’s StartConsole method: def StartConsole(self, console, canvas, langInfo): console.CurrentModule.SetVariable(“canvas”, canvas) console.CurrentModule.SetVariable(“wpf”, wpf) When DLRConsole initializes the console, it sets a couple of variables for you automatically, including a WPF helper module and a pointer to the canvas. Given these variables, you can write code like that shown in Figure 6, which implements the mouseover color-change effect. If you want to see this code in action, select the Python Code tab and press Ctrl+Enter. Running DLRConsole Locally You can download and run DLRConsole on your local machine, but getting set up is slightly more complicated because it requires a Web server. After setting up a Web server, you can extract DLRConsole.zip, place the folder in your Web server folder, and navigate to /DLRConsole/index.htm on your Web server. (Setting up a Web server on your machine is out of the scope of this column, but you can find some pointers for Windows Vista at iis.net/articles/view.aspx/IIS7/Deploy-an-IIS7-Server/Installing-IIS7/ Install-IIS7-on-Vista, and for Mac OS 10.4 at docs.info.apple.com/article.html?path=Mac/10.4/en/mh1921.html.) DoInput, in this case, calls DoSingleLine, which tries to parse the expression. If successful, it evaluates the expression, writes the output to the console, and sets the “_” convenience variable (see Figure 5). Just in case you’re interested, you can look in the source code to see how TryExpression parses an expression and how GetEngine creates the LanguageEngine. So, that’s what it takes to calculate 2+2 in DLRConsole. In fact, this covers all the one-liners you can do. With a little variation you can handle multiple liners, too. The most interesting parts are where the DLRConsole calls into the DLR to execute the code. The only caveat when running on your own Web server is that you need to set up the server’s configuration for supported DLR languages. You’ll know this is an issue if you get this error: Silverlight error message ErrorCode: 2024 ErrorType: ParserError Message: Invalid attribute value text/python. XamlFile: DLRConsole.xaml Line: 6 Position: 53 But Wait, There’s More You will need to add the following MIME type mappings to your server configuration: .py == text/python .jsx == text/jscript 2 + 2 is a pretty simple example. What about something more complicated? OK, how about .NET Framework integration? Take a look at this: 22 msdnmagazine CLR Inside Out At this point, pat yourself on the back; you’re running a DLR Silverlight app! http://msdn.microsoft.com/msdnmag/issues/06/10/CLRInsideOut http://msdn.microsoft.com/msdnmag/issues/06/10/CLRInsideOut http://blogs.msdn.com/ironpython http://iis.net/articles/view.aspx/IIS7/Deploy-an-IIS7-Server/Installing-IIS7/Install-IIS7-on-Vista http://docs.info.apple.com/article.html?path=Mac/10.4/en/mh1921.html http://iis.net/articles/view.aspx/IIS7/Deploy-an-IIS7-Server/Installing-IIS7/Install-IIS7-on-Vista http://docs.info.apple.com/article.html?path=Mac/10.4/en/mh1921.html
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.