6) APyaX Content Download

APyaX offers a method for enabling web pages to download dynamic content (files generated by a call to a python function). The following HTML page provides an example.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>APyaX file download</title>
	<script type="text/python" src="downloadit.py"></script>
	<script type="text/javascript">
	    function handle_download_img(imageHTML) {
		document.getElementById('result').innerHTML = imageHTML;
	    }

	    function show_error(error) {
		document.getElementById('error').innerHTML = error;
	    }
	</script>
	<link rel="stylesheet" type="text/css" href="../../css/velocikit.css"/>
</head>

<body>

	<h2>APyaX File Download</h2>

	<p>The image which should appear below has been generated by a python script</p>

	<a onclick="getDownloadIMGS(new apyax(handle_download_img,show_error))">
	<img src="apyax_download(downloadImage,download)"/>
	</a>

	Click on the image to download and display more python generated images

	<p><b>result=<div id="result"></div></b></p>
	<p><b>error=<span id="error"></span></b></p>

</body>
</html>

HTML page demonstrating APyaX markup for file download


The python script downloadit.py defines a function called downloadImage and is referenced in the (X)HTML.

When the APyaX generator is called, it scans src attributes in the (X)HTML document to search for calls to a function called apyax_download instead of the usual file path. This function takes the name of the python function to call (downloadImage) as its first argument, and a key value to pass to this function as its second argument.

apyax_download(<python-function>,<key-value>)

where:

<python-function>

All python scripts referenced from this page are searched a function with this name. If located, this function is invoked on the server to generate the content. The function is passed the usual apyax object as its first parameter and the <key-value> (see below) as its second parameter.

The python function invoked to return the file should return a tuple containing:

(1) A file like object containing the content to return to the client (in this example, a GIF image)

(2) A string containing the mime-type for the file (enabling the client to correctly handle the returned file).

<key-value>

This value is passed to the python function. It can be used to select which content should be generated.

The following python script provides an example of a python function which generates content:



import StringIO

def downloadImage(apyax,key):

    if images.has_key(key):
	filecontents = StringIO.StringIO(images[key][0])
	return (filecontents,"image/"+images[key][1])
    else:
	return ("","image/gif")

def getDownloadIMGS(apyax):

    downloadHTML='<table border="yes">'
    
    for key in images.keys():
	downloadURL=apyax.getDownloadURL('downloadImage',key)
	downloadHTML+='<tr><td>'+key+'</td><td><img src="'+downloadURL+'" /></td></tr>'

    downloadHTML += '</table>'
    return downloadHTML
    
image_download = "\
\x47\x49\x46\x38\x39\x61\x10\x00\x10\x00\xc4\x19\x00\x87\xcf\x9f\
\x0c\x9e\x3d\xa8\xdc\xb9\x96\xd5\xab\x75\xc8\x91\x06\x9b\x38\xe1\
\xf3\xe7\xba\xe3\xc8\xd2\xed\xdb\x57\xbc\x79\xc3\xe7\xcf\xdb\xf1\
\xe2\x42\xb3\x68\xb7\xe2\xc5\xd5\xee\xdd\x1e\xa5\x4b\xb4\xe1\xc3\
\x27\xa9\x52\x9c\xd7\xb0\xf9\xfd\xfa\xa5\xdb\xb7\x09\x9d\x3a\x66\
\xc2\x85\xff\xff\xff\x00\x99\x33\xff\xff\xff\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x21\xf9\x04\
\x01\x00\x00\x19\x00\x2c\x00\x00\x00\x00\x10\x00\x10\x00\x00\x05\
\x5a\x60\x26\x8e\x64\x69\x4e\x10\x61\x11\xd0\x64\x8a\xc2\x83\xcd\
\xf3\x23\x98\x00\xad\xcf\x00\xd9\xe8\x97\xa0\xae\x31\x8a\x00\x85\
\xb4\x88\x48\xb1\x0b\x5e\x76\x8a\xcc\xa0\x89\xa4\x0d\x32\xb9\x99\
\x73\x4b\xeb\x49\x8e\x4e\x9d\x24\x63\xa0\x3e\x75\x06\x11\xa3\xb9\
\x63\x8c\x1c\x81\x9d\xae\xe2\x20\x1d\xe2\xf2\xc0\xc1\xb4\x48\x14\
\x68\x05\x09\x0b\x2f\x23\x08\x14\x08\x26\x21\x00\x3b"

images = {
    "download":(image_download,'gif'),
}



Python script downloadit.py for generating dynamic content