1

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.

4
  • 1
    You may want to look into using Flask. It probably does more than what you need, but it can probably solve your problem. Commented Aug 16, 2014 at 21:21
  • Thanks, Alex. I'll look into Flask. Commented Aug 16, 2014 at 21:36
  • So I looked into flask, and it's a framework for web applications. I'm not sure if I want to go down that route. I already have the webpage ready and it produces the XML file I want to sent to the server. So I'm not sure how I could incorporate Flask into that? Commented Aug 17, 2014 at 0:05
  • 1
    +1 for Flask or Django. You would basically call your script from a Flask or Django view, which is routed to an API URL endpoint, and return the data as a response. You can then hit that API endpoint via an AJAX call when the button is pressed. Commented Aug 17, 2014 at 2:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this. I just tried understanding this, but I think it requires me running this on the local host? I am not sure, can you give me some explanation about how to go about this? Plus you are using ajax. Would I need this to pull something like this off?
the „0.0.0.0” adr run on your current ip (could be acces from outside, if firewall say so). Ajax was heare for „inline” passing,I'm to lazy to write a 3 pages html, but if you made a form with action='xml' method='post', submit go to same route. Also, if you use a form, every input is accesible as request.get('input name'). I use bottle because convert dict to json by default, but there are a lot of other web helpers that may be more adequate for your need. I think Django is one of the most used/documented, but you may need to learn a little about it.
Thanks for the reply. I am new to this, so I'm actually looking into a more high-level answer. This is pretty low-level for me, so I don't understand much of it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.