Development

Upload photos from iPhone using PhoneGap, jQuery, and Python

PhoneGap

Setup an HTML button to click.

<a onclick="uploadPhoto(pictureSource.PHOTOLIBRARY);">Photo Upload</a>

the click handler

// A button will call this function
//
function uploadPhoto(source) { // Retrieve image file location from specified source navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 25, destinationType: destinationType.DATA_URL, sourceType: source, allowEdit: true });
}

The callback handlers.

function onPhotoDataSuccess(imageData) {
  // upload the captured photo
  $.post(‘http://path/to/ajaxUpload’, {data:imageData}, 
                 function(data){alert(‘success’);});
}
 
 
// Called if something bad happens.
// 
function onFail(mesage) {
  alert(‘Failed because: ‘ + message);
}

the server side handler (cherrypy)

    @cherrypy.expose
    def ajaxUpload(self, **kwargs):
        """
        Accept miscellaneous iphone picture upload.
        """
        import os
        import base64
  filename = "test.jpg" tmpFileName = os.path.join( "/tmp/", filename ) tmpFile = open(tmpFileName , ‘wb’) data = base64.b64decode(kwargs.get(‘data’)) tmpFile.write(data) tmpFile.close()
  cherrypy.response.headers[‘Content-Type’] = ‘text/javascript’ return "some type of return message for your AJAX if you like"

Reference Material:

Image Upload using jQuery and Python

PhoneGap Camera API

Convert Epoch time to Normal time with Python

I have a long integer time that I believe is actually epoch time. The Python methods to convert epoch time to/from “normal” time (human readable time).

Convert Epoch to Human-readable

import time 
 
# where epoch is your epoch time value
 
time.strftime("%a, %d %b %Y %H:%M:%S +0000", 
                        time.localtime(epoch))

http://www.epochconverter.com/

Setting up Python Pyjamas

I’m just playing around with the Python Pyjamas toolkit for no reason. The Pyjamas Book is slightly difficult to follow. Here are my notes.

Check out Pyjamas from Git and run bootstrap file.

$: git clone git://pyjs.org/git/pyjamas.git
$: cd pyjamas
$: python bootstrap.py 

Running bootstrap.py creates a bin directory. If you plan on playing with this a lot it may be worthwhile to link the files in bin to your environment path.

## (I have ~/bin referenced on my environment path. You might not!)
$: ln -s ../pyjamas/bin/pyjscompile ~/bin
$: ln -s ../pyjamas/bin/pyjsbuild ~/bin

Try an example

$: cd ../pyjamas/examples/helloworld
$: pyjsbuild Hello.py
= pyjamas Compiles some stuff into ./output =

Pyjamas dumps a bunch of files into an output subdirectory. Take a look at the Hello.html file that got generated.

Now you can follow the Pyjamas book.

For now: figure out how to create a UI with Pyjamas.

Next Steps: Use Pyjamas with Google App Engine.

Syndicate content