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">
	<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;
	<link rel="stylesheet" type="text/css" href="../../css/velocikit.css"/>


	<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)"/>

	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>


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.




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).


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])
	return ("","image/gif")

def getDownloadIMGS(apyax):

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

    downloadHTML += '</table>'
    return downloadHTML
image_download = "\

images = {

Python script downloadit.py for generating dynamic content