flash
flash

Reputation: 275

HTML button on client to run python script on server then send results to webpage on client

I have seen some previous questions, that were similar but I couldn't find anything like this. I have a webpage (on a server) and I would like the user to click a button which will execute a python script. I want this python script to run on the server and then send the results back to the webpage and display it.

When the user clicks the button, the data that will be sent to the server would be an XML file.

I just don't know where to start with all of this. What can I use to accomplish this?

Thanks for your time.

EDIT: I actually have the webpage all done and setup, and it produces the XML. I just need to run the python script when a user clicks on a button on the webpage. Not sure if that helps, but I'm posting it. Thanks

I WOULD LIKE A HIGH-LEVEL EXPLANATION FOR THIS PLEASE AND THANK YOU, since I don't know about what has been suggested to me already.

Upvotes: 1

Views: 3483

Answers (1)

cox
cox

Reputation: 721

There is a lot of web libs for python. You may try bottle (work without installing, one-file, just put the „bottle.py” file in your work folder. A simple example:

    from bottle import route, run, static_file, post, request
    @route('/js/<filename>')
    def js(filename):
        return static_file(filename, root='js')

    @route('/')
    def index():return static_file('tst.html', root='./')

    @post('/xml')
    def xml():
        for x in request.forms:
            print(x)
        return {'return': 'accepted'}

    run(host='0.0.0.0', port=8000)

And html:

<!DOCTYPE html>
<html lang="ro">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>TTL</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<button onclick="test()">Test</button>
<script type="text/javascript">
    function test() {
        $.ajax({
            url: 'xml',
            type: 'POST',
            data: '<my><xml>string</xml></my>',
            dataType: 'json',
            success: function (ret) {
                alert(ret['return']);
            }
        });
    }
</script>
</body>
</html>

Sorry for JQuery, to lazy to write plain js xhr. Bottle is well documented, but cherrypy, pyramid, django, tornado also.

Upvotes: 3

Related Questions