Python API

From DevWiki

Revision as of 05:16, 17 December 2009 by Riegersn (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to Home


Contents


Python Reference

Module

Clases


Overview

When you combine the power and simplicity of python to your Boxee application. The possibilities can be endless. Any 'On-Event' tag can hold python code. Such as <onload> and <onclick>. Our python API enables developers to interact with just about every aspect of their skin/application as well as performing any standard python functions. If you want to query a website and return a list of results when the user click a button, you can do this easily with python.

For a good introduction to Python try Dive Into Python. There are many valuable examples here that are easy for beginers and offer a nice refresher for the intermediate python developer. For the official docs check out the Official Python Tutorial. It is important to note that current Boxee builds are running the python 2.4 environment.


Where & How

Your python code can reside either in the skin xml itself using cdata tags or in the root of your application directory. Multiple scripts can be used. If you choose to save your python script(s) in the root of your application folder you can call it from within your application by using the import function (examples below). When using python from directly within your skin xml files, set the lang property to python and use cdata for the code itself. There are a number of tags you can use to call your python code. For the most part, any property tag that starts with on can use python. Below is a full list of tags available.

  • onload
  • onunload
  • onleft
  • onup
  • onright
  • ondown
  • onclick
  • onfocus
<onclick lang="python"><![CDATA[
import myscript
myscript.login(user, pass)
]]></onclick>

Note that python tab/space rules still apply. So make sure your python code starts flush on the line (don't indent the entire script to keep it inline with your xml)


Examples

Below are some examples of using python within your application.

button onclick

<control type="button" id="7002">
   <width>115</width>
   <height>60</height>
   <label>Search</label>
   <onclick lang="python">
<![CDATA[
mc.ShowDialogNotification('You clicked the search button!')
]]>
   </onclick>
</control>

window onload

<window type="window" id="14000">
   <defaultcontrol always="true">1000</defaultcontrol>
   <allowoverlay>no</allowoverlay>
   <onload lang="python">
<![CDATA[
config = mc.GetApp().GetLocalConfig()
path = config.GetValue("feed"):
mc.GetActiveWindow().GetList(120).SetContentURL(path)
]]>
   </onload>

script in root dir

userdata/apps/{your_app_id}/myscript.py
import mc
 
def login():
   uname = mc.ShowDialogKeyboard("Enter Username", "")
   pword = mc.ShowDialogKeyboard("Enter Password", "", True)
   if uname and pword:
      http = mc.Http()
      params = "user=%s&pass=%s" % (uname, pword)
      http.Post("http://mysite.com/login/", params)
      responseCookie = str(http.GetHttpHeader("Set-cookie"))
      if 'PHPSESSID=' in responseCookie:
         return True
   return False
<control type="button" id="7002">
   <width>115</width>
   <height>60</height>
   <label>LOGIN</label>
   <onclick lang="python">
<![CDATA[
import myscript
result = myscript.login()
if result:
   mc.ShowDialogNotification("You are now authenticated!")
]]>
   </onclick>
</control>
Personal tools