What's the very simplest way to run some Python from a button on a web page?

Tim Roberts timr at probo.com
Sat Jan 21 23:36:07 EST 2012

tinnews at isbd.co.uk wrote:
>I want to run a server side python script when a button on a web page
>is clicked.  This is on a LAMP server - apache2 on xubuntu 11.10.
>I know I *could* run it as a CGI script but I don't want to change the
>web page at all when the button is clicked (I'll see the effect
>elsewhere on the screen anyway) so normal CGI isn't ideal.

It seems what you're after is AJAX.  If you are using a Javascript
framework like jQuery, it's easy to fire off an asynchronous request back
to your server that leaves the existing page alone.  If you aren't, then I
think the easiest method is to use an invisible <iframe>.  From Javascript,
you can set the "src" property of the <iframe> to fire off a request while
leaving the rest of the page alone.

You could spend the rest of your career reading all of the good web
material on AJAX.
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

More information about the Python-list mailing list