Over the holiday period, Sarah and I went visiting at her mum’s in Ipswich.  Now, I’m not saying that they lack technology down there or anything, but there was no internet connection.  None whatsoever.  Zilch.  Nada.  Eeek!

What’s a techie guy meant to do in that situation?  I didn’t have a laptop or notebook with me, so I had to make do with my Android phone as the only source of technological entertainment and sole connection into the real world 🙂

The Python editor may be a little small on the Android, but it means I can program on the move!Now, I’d heard about the Damon Kohler‘s ASE (Android Scripting Environment) quite a while back, but never really got round to looking at it or taking it for a spin.  I’ve developed a few apps using the Android SDK, and even though Java isn’t my primary language, the whole platform has been designed to make it easy for developers to get their apps working really quickly.

However, day-to-day at Linden Lab, I work primarily in Python, so this looked like the perfect opportunity to have a delve into Python scripting use ASE, and it suited my circumstances perfectly — after all, you can type directly into the Android terminal and edit your Python scripts there and then without having to be tethered to a computer.

Installing ASE was a breeze directly from the web-site, and the supported environments were easy to install from within ASE itself — you can develop in Lua, JavaScript, Ruby, BeanShell, or Perl;  Python was my choice though, obviously.  So, following the instructions, getting started was really quick.  There are some example scripts to get you going, including a test script which quickly runs through some of the features.

And then I saw it:
import gdata.docs.service

Oooh.  Nice.  I was going to install the Google Data API Python libraries and have a play with those, and fortunately for me all the Google Data API Python modules were included in the standard ASE install.  Yay! In fact, you have everything you need to talk to all of Google’s APIs — Google Docs, Base, Blogger, Calendar, Contacts, YouTube, Webmaster Tools, Maps … they’re all there!

I was thinking of having a script that could check the stats on my web-site via Google Analytics.  I know I could have just browsed to the stats pages using Android’s built in browser, but where’s the fun in that 🙂  Now I can use the Google Analytics API to directly query the stats via a Python script running on my Android.

I’ve done quite a bit of work with Google Adwords APIs, but nothing in Python, but reading through the Analytics API documentation gave enough direction.  Essentially you need to authenticate yourself with the service you’re after, then make a query against the data with the parameters you want to filter on.  Seriously, it’s that simple.

Of course, it being a small soft keyboard, it took a day and an age to type in the Python code.  Fortunately for me, I had a lot of time to kill, and this was the closest I was going to get to any kind of computing power for a few days!  Get those fat fingers typing on that diddy screen …

Google Android Python Development example, using the Android Scripting Language (ASE).
This is an example of how to communicate with the Google Analytics Data API via Python.
Author: Howard Sandford, December 2009
Released under CC BY-SA-NC 3.0

import gdata.analytics.service
import datetime
import android

# For use with the ASE
droid = android.Android()

# The Google Analytics account login details
USERNAME = 'your_email_account@gmail'
PASSWORD = 'your_password'

# We want data for the last 7 days
end_date = datetime.datetime.today().strftime('%Y-%m-%d')
start_date = (datetime.datetime.today() - datetime.timedelta(7)).strftime('%Y-%m-%d')

# Authenticate ourselves against the Analytics Data API
client = gdata.analytics.service.AnalyticsDataService()
client.ClientLogin(USERNAME, PASSWORD)

# Grab the data for a particular profile_id -- in this case the
# number of visits for the keywords that people have searched for us
data = client.GetData(

# print out the results
total_visits = 0
analytics_results = []
for entry in data.entry:
    analytics_results.append('%s visits for %s\n' % (entry.metric[0].value, entry.dimension[0].value))
    total_visits = total_visits + int(entry.metric[0].value)

analytics_results.append('Total of %s visits between %s and %s\n' % (total_visits, start_date, end_date))

droid.getInput('Analytics Data', ''.join(analytics_results))

Seriously, that’s how easy it is to work with the Analytics API.

The USERNAME and PASSWORD are the same as the email address and password that you would use to log into your Adwords/Analytics dashboard on the web.  And no, strangely enough, what you see in the above code is not my username/password 🙂

In the code, the following snippet warrants a little more explanation:

data = client.GetData(

The ids, here set to ga:123456, is the profile id(s) of the web-site(s) you want to retrieve details of (and no, ga:123456 is not my profile id either!).  Profile id is also referred to as tableId in the Google Analytics API. You can find your profile id easily enough by logging into your Adwords/Analytics account and then selecting edit on the profile.  As you can see in the screenshot below, the profile id is clearly displayed.  The profile id is a number, and you just need to add the ‘ga:’ in front of it.

Grab your profile id directly from your Google Adwords/Analytics dashboard

The dimensions parameter describes how you want your data categorised; the metrics parameter describes what details you actually want to see.  In my script, I wanted to see the number of visits to this web-site split by what keyword the visitor used to find the site.  The sort parameter above shows that I wanted the results displayed in descending order of visits, i.e. the highest number of visits first.

The start_date and end_date parameters speak for themselves.

The values for dimensions and metrics could contain more than one item, separated by commas, and the full list of available dimensions and metrics can be found in the Google Analytics Dimensions and Metrics reference.

I originally used droid.makeToast() to pop-up the results on the screen, but the pop-up didn’t stay on the screen long enough for me to read all the returned information!  The original Java implementation allows you to change the time it shows on the screen, but ASE’s makeToast() doesn’t have that parameter available.  So instead I’ve used droid.getInput() to display the Google Analytics data.

Google Analytics data via Python scripting on the Android

This method, as the name suggests, is usually used to get input from the user, however I found that using it provides a convenient way to display the data in a modal dialog, complete with OK button and scroll-bars if necessary.  ASE doesn’t yet provide any other dialog capabilities — or in Android SDK speak, intents — and no other GUI design is available.  If I were to extend this mini-app, I would prefer a more esoteric display, but if I’m going to do that it would be easier to develop the whole thing in Java from scratch … hrm, perhaps an idea for another project …

… and remember, the main reason for starting this scripting endeavour was because I didn’t have any computer available to develop on, and so Python via ASE was the perfect way to achieve my goal.

So there you have it, a simple Google Analytics Python script on the Google Android using ASE (Android Scripting Environment).  If you have questsions, just let me know!

[Edit: I’ve added a follow-on update to improve the user interface using the latest ASE Python Android Scripting Environment here]

[All files are released under Creative Commons BY-SA-NC 3.0]

13 thoughts on “Google Android python development with ASE (Android Scripting Environment)

  1. What did you use to edit your code on the Droid? I see vi is available but no ESC key makes that problematic. How did you create the script? Thanks

  2. Hi Matthew, when you install the ASE you simply hit the “Add” option on the menu which prompts you for the type of script you want to create (in my case I selected “Python 2.6.2”). Then ASE provides a handy text editing window for you to type into. It’s not the kind of environment to use for *lots* of coding, but it’s certainly good enough for the small script.

    The other way to do it is to use whatever editor you want on your computer (Emacs, Vi, etc) and then start up the Android emulator, push the script to the /sdcard/ASE/scripts directory with the command:

    adb -e push your_script_name.py /sdcard/ASE/scripts

    or, connect your actual Android up to the computer via USB and copy the Python script to the same /ASE/scripts directory. You’ll then find the script is ready to run from the ASE environment.

    For me, I *had* to use ASE’s built in editor as it was all I had available to me 🙂

  3. Hey,nice work man
    my problem is,somehow i put android on HTC touch
    which is still in alpha,but it works
    anyways ,now i have a working ase.
    but i dont have a working internet connection
    so i go to interpreter and ask it python 2.6.2
    it tries downloading ,but fails as i dont have internet connection
    now i found out that the python interpreter and scripts are downloaded as zip and extracted to a path called /data/data/com.google.ase/xxx, /sdcard/ase/extras/xxx, and /sdcard/ase/scripts respectively.
    now my softkeyboard’s enter key doesnt work.
    and it andexplorer doest allow me to unzip even in memory card
    so i cant put python interpreter hence i cant run any .py
    pls help

  4. Hi Gaurav

    You’re better off asking your question on the official forum for ASE which can be found here:

    –> Android Scripting Environment (ASE)

    Anyway, I’m sure you can still install ASE via USB connection, even without an internet connection on the Android. It will be similar to installing ASE in the Android Emulator, but replacing your HTC Touch instead of the emulator, as detailed here:

    –> Installing the Android Scripting Environment on the Android Emulator

    Let me know how you get on!

  5. thanx man,i put the zip files in root of storage card
    ie just outside,
    now ase could find the python interpreter in SD card
    so it tries installing.
    But i aint a super user , so its installation fails.
    to make myself super user i dont think i need to open shell and type su
    and press enter,but my enter key aint functioning,so somehow im stuck.
    Do you know any website or place where there’s show off of python programming.

    1. Seriously, the links I gave you above will help you tons!

      Connect your phone up to your computer and put it in debug mode. Use the computer terminal to install the ase package with an adb push. You’ll be able to install everything following that second link in my previous reply, but instead of talking to the emulator, you’ll be talking to your real phone.

      If that doesn’t work, then I have no idea what you’ve done to your HTC Touch 🙁

    1. Yeah, I’ve been keeping my eye on the ASE developments, but of course there’s always the matter of finding time to not only play with the new and shiny, but to then *blog* about the new and shiny! 🙂

      I guess, watch this space for updates!

  6. hi there;
    thank you for the post. am new in this subject as python and android development. what i would like to ask is, is there any way to use eclipse for python to develop python scripts and test them, because i really need intellisense otherwise its very hard to develop in the small keyboard.

    1. Hi Muhanad

      I know what you mean about typing out a full program on the Android without a full keyboard. You’ll find that the SL4A interface allows you to select from a list of available commands, so it’s not totally impossible.

      However, integrating with a full screen editor on your computer shouldn’t be so difficult. I don’t know about Python in Eclipse — personally I’m an Emacs guy — as I’ve only used Eclipse for the occasional Java development.

      Either way, once you have your code written, you can use the Android console to push the Python file to the device:

      $ adb -e push yourscript.py /sdcard/path_to_your_python_scripts/

      Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *