You are reading a MIX Online Opinion. In which we speak our minds. Hans Hugli Meet Hans Arrow

Lab Notes

2Comment Retweet

TwitterBadge 2.1

Apr 22, 2010 In Development By Hans Hugli

The other day, I noticed that the TwitterBadge object on the Gestalt page stopped working. The original was based off the Gestalt 0.6 codebase and was never updated to the new bits since the System.Json namespace was not available in the new version of the DLR. We did this to reduce the download size of the codebase.

In this LabNote, I’ll explain how I updated the TwitterBadge IronPython app to make a call to classes in an external DLL. My method employs the same technique as Ashish Ghoda’s IronRuby sample. I modified the sample to use external Python and XAML files and made it easy to change the twitter keyword you want to search for.

Here’s the content of the HTML page:

<script src="" type="text/javascript"></script>

<script type="application/x-zip-compressed" src=""></script>

<script type="text/python" class="twitterbadge" src="" defer="true"></script>

<script id="twitterbadge" type="application/xml+xaml" Width="580" Height="180" src="twitterbadge.xaml"></script>

<script type="text/python" class="twitterbadge">
import System
import twitterbadge

First, I added a reference to the Dynamic Language Runtime script and a reference to the System.Json.dll that’s zipped up in the file

Next, I added a reference to the Python script and XAML that defines the UI, and an inline Python script that bootstraps the application. The final script must be set to the same class name as the class name of the external Python file. The keyword used in this sample is "MIX10", but it can be modified directly in the HTML page to avoid making any edits in the external Python file.

Let’s take a look at the external Python file to see what needed to be done to load the System.Json into scope.

from System import *
from System.Windows import *
from System.Windows.Threading import *
from System.Windows.Media import *
from System.Net import *
from System.Text import *
from System.Collections import *
from System.Collections.Specialized import *
from System.Windows.Media.Imaging import *
from System.Text.RegularExpressions import *

me = Application.Current.RootVisual # This localizes the XAML UI elements

First, I imported the namespaces I needed for the application. Next, I localized the RootVisual. Normally, a Python script is aware of the XAML UI since the DLR automatically localizes the "me" variable with the RootVisual of the XAML. But in this case, I was loading the script up manually, so I had to set it manually.

Next,I imported clr,which has a method that allows adding a reference to a file. Note that the path to the file contains the name of the zip file "json". Once I added the reference, I could import the System.Json namespace.

import clr
clr.AddReferenceToFile("json/System.Json.dll") # This loads the JSON dll that does not ship with the DLR
from System.Json import *

I also needed to make a call to the System.Windows.Browser.HtmlPage namespace to launch the browser when a user clicks on a tweet. Again, normally HtmlPage is localized if the Python is loaded automatically, but in this case I needed to import the System namespace and manually call the method.

import System
System.Windows.Browser.HtmlPage.Window.Navigate(Uri(url), "_blank")

The last modification is to add a callable method that accepts a keyword term.

def load_keyword(kw):
	global twitter
        global keyword
        keyword = kw
	twitter = Twitter()
	twitter.add_presenter(BadgePresenter()), "?tag=" + kw)

load_keyword is called from the HTML page inline Python script. Click on the image below to try it. (You’ll need to have Silverlight installed.)

You can grab the source code and copy the contents into a directory on your web server or embed the HTML script above to existing web pages—as long as the xaml, py and zip files are local to your web page.

Because it’s not necessary to copy the DLR bits locally, this app is convenient. Note that on some web servers you may need to add mime-types to get the application to work. If this is the case, ensure you add the following:

  • .xap -> application/x-silverlight-app
  • .slvx -> application/octet-stream
  • .py -> text/plain (for Python)
  • .rb -> text/plain (for Ruby)
  • .xaml – application/xaml+xml

Follow the Conversation

2 comments so far. You should leave one, too.

Juicy Couture Watches said on May 2, 2012

This blog site is great. How did you come up witht he idea?

Valentino Dress said on May 24, 2012

Curious to determine what all you intellectuals need to say about this?-?-.