Call a python function within a html file

Posted on

Question :

Call a python function within a html file

Is there a way to call a python function when a certain link is clicked within a html page?


Asked By: hassaanm


Answer #1:

You’ll need to use a web framework to route the requests to Python, as you can’t do that with just HTML. Flask is one simple framework:

from flask import Flask, render_template
app = Flask(__name__)

def index():
  return render_template('template.html')

def my_link():
  print 'I got clicked!'

  return 'Click.'

if __name__ == '__main__':


<!doctype html>

<meta charset=utf-8> 

<a href="/my-link/">Click me</a>

Run it with python and then navigate to http://localhost:5000/. The development server isn’t secure, so for deploying your application, look at

Answered By: Blender

Answer #2:

Yes, but not directly; you can set the onclick handler to invoke a JavaScript function that will construct an XMLHttpRequest object and send a request to a page on your server. That page on your server can, in turn, be implemented using Python and do whatever it would need to do.

Answer #3:

Yes. If the link points to your web server, then you can set up your web server to run any kind of code when that link is clicked, and return the result of that code to the user’s browser. There are many ways to write a web server like this. For example, see Django. You might also want to use AJAX.

If you want to run code in the user’s browser, use Javascript.

Answered By: David Grayson

Answer #4:

There are several ways to do this, but the one that has worked best for me is to use CherryPy. CherryPy is a minimalist python web framework that allows you to run a small server on any computer. There is a very similiar question to yours on stackoverflow – Using the browser for desktop UI.

The code below will do what you want. Its example 2 from the CherryPy tutorial.

import cherrypy

class HelloWorld:

    def index(self):
        # Let's link to another method here.
        return 'We have an <a href="showMessage">important message</a> for you!' = True

    def showMessage(self):
        # Here's the important message!
        return "Hello world!" = True

import os.path
tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')

if __name__ == '__main__':
    # CherryPy always starts with app.root when trying to map request URIs
    # to objects, so we need to mount a request handler root. A request
    # to '/' will be mapped to HelloWorld().index().
    cherrypy.quickstart(HelloWorld(), config=tutconf)
    # This branch is for the test suite; you can ignore it.
    cherrypy.tree.mount(HelloWorld(), config=tutconf)

I personally use CherryPy in combination with several other modules and tools:

  • Mako (template library)
  • py2exe (convert into Windows executable)
  • GccWinBinaries (used in combination with py2exe)

I wrote an article about Browser as Desktop UI with CherryPy that introduces modules and tools used plus some further links that might help.

Answered By: schurpf

Answer #5:

In addition to running Python scripts on a server, you can run Python scripts on the client-side using Skulpt.

Answered By: Anderson Green

Leave a Reply

Your email address will not be published.