boxee :: forums  

Go Back   boxee :: forums > boxee developers > boxee applications

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old January 6th, 2010, 10:56 PM
joshuajonah joshuajonah is offline
Senior Member
 
Join Date: Jan 2009
Posts: 185
Default [Info] Persistent storage and global variables in apps

Devbobo posted an interesting question about how to keep data structures in memory for use throughout an app. So I figure we need a little rundown on persistance options in Boxee apps.

The Problem
When you define a variable in one event of an app XML, it is not accessible from other events in the same XML file. Each event is treated as a fresh Python script instance and therefore has no access to items defined in others.

Example:
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
test_variable = "Exists"
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
print test_variable                # <- gives you an error that it is undefined
]]></onclick>
        </control>
Eventually you're going to have to find a way to store values short term for the session, or permanently.

Some Solutions
We have a number of ways we can hold a variable throughout the XML starting with the simplest:

Storing variables in LocalConfig an app has a class instance called a LocalConfig, this is a place for settings in an app. It is quite limited, but is usually just enough for most apps. It is a simple key -> value storage that is limited to strings and is kinda slow. This data will only be available for the session. Example usage using our previous example:
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
mc.GetApp().GetLocalConfig().SetValue("test_variable", "Exists")
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
print mc.GetApp().GetLocalConfig().GetValue("test_variable") # <- prints "Exists" like you'd expect
]]></onclick>
        </control>
Relevant Boxee mc class docs

Using file storage This is by far the slowest, but permanently persistent, and relatively simple. Example code:
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
f = open('test_variable.txt', 'w')
f.write('Exists')
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
f = open('test_variable.txt', 'rb')
f.read()              # <- prints "Exists"
]]></onclick>
        </control>
Relevant Python docs

Using sqlite you can use sqlite as a DB in Boxee apps, boxee apps have the permission to create sqlite.db files in the app folders on the client machine. This is not only permanently persistent, but also very flexible as it is an entire relational database system. This comes at the cost of it being slightly heavy and complex. Also because of the separate scripts, you must open a new cursor for every script (not ideal but does the job):
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
from sqlite3 import *
conn = connect('appid.db')
curs = conn.cursor()
curs.execute("insert into sometable values ('test_variable', 'Exists')")
conn.commit()
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
from sqlite3 import *
curs = connect('appid.db').cursor()
curs.execute("select * from sometable")
for row in curs:
    print row
# prints "('test_variable', 'Exists')"
]]></onclick>
        </control>
Relevant Python docs

Anything You Want you could use memcached, CouchDB, MySQL, Postgres, Tokyo Cabinet, etc. But there's a catch. Your client will have to have this software, as well as the appropriate Python bindings installed to connect to these databases. Boxee uses sqlite for it's persistent storage, so you are safe to assume that sqlite is around. The others are not required and not installed with Boxee.

Hopefully this helps open your mind about what is possible with apps. Just try to remember that under all the complexity, it's just Python. Need to scrape a webpage? Make an FTP connection? Scan a barcode? Order a pizza? There's a Python package for just about anything you can think of, and they're all usable inside a Boxee App.

Considers starting a Dominoes pizza order and delivery app
__________________
A Guy Who Makes Boxee Apps Intermittently

Last edited by joshuajonah; January 7th, 2010 at 12:12 AM.
Reply With Quote
 

Bookmarks

Tags
database, persistence, pizza, sqlite, storage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:36 AM.


skin based on greenzero from vBSkins.com

 
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2011, vBulletin Solutions, Inc.