hassaanm
hassaanm

Reputation: 411

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?

Thanks

Upvotes: 16

Views: 117203

Answers (5)

Blender
Blender

Reputation: 298106

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:

server.py:

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

@app.route('/')
def index():
  return render_template('template.html')

@app.route('/my-link/')
def my_link():
  print 'I got clicked!'

  return 'Click.'

if __name__ == '__main__':
  app.run(debug=True)

templates/template.html:

<!doctype html>

<title>Test</title> 
<meta charset=utf-8> 

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

Run it with python server.py and then navigate to http://localhost:5000/. The development server isn't secure, so for deploying your application, look at http://flask.pocoo.org/docs/0.10/quickstart/#deploying-to-a-web-server

Upvotes: 28

Anderson Green
Anderson Green

Reputation: 31800

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

Upvotes: 1

schurpf
schurpf

Reputation: 609

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!'
    index.exposed = True

    def showMessage(self):
        # Here's the important message!
        return "Hello world!"
    showMessage.exposed = 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)
else:
    # 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.

Upvotes: 2

David Grayson
David Grayson

Reputation: 87386

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.

Upvotes: 2

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95479

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.

Upvotes: 7

Related Questions