0

I want to call python command from my html page .

Code is :

<form class="form" action="" method="post" name="new-service" enctype=multipart/form-data>
      <div class="control-group">

          <div class="controls">
               <input id="Search" name="Search" type="text" placeholder="Search.. eg: moto g" class="form-control input-xlarge search-query center-display" required="">
   <!--  here i want to call python command  or function  -->

          </div>
     </div>

      </form>

and my python command is :

$ python main.py -s=bow    

here bow is get from input box .

2
  • 1
    I don't think it would be possible client-side (i.e. browser), but server-side.. that is what django, flask, etc. can be used for (i.e. executing python code based on parameters coming from a web-page) Commented Jul 27, 2015 at 12:31
  • You need to use a web-server to handle the incoming request from your web page. Commented Jul 27, 2015 at 12:52

2 Answers 2

2

Directly from the UI you cannot call a command. That is backend specific. However, in the view that handles your form you can do something like:

from django.core.management import call_command
call_command('your_command_name', args, kwargs)

Check the documents page here for reference: https://docs.djangoproject.com/en/1.8/ref/django-admin/#call-command

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

1 Comment

... only if he is talking about Django.
1

Use jQuery to send a request to your python script on server that will be handled as a CGI/Fast CGI script or through WSGI (mod_wsgi for Apache) or mod_python (for Apache too).

Your Python script will receive input using query strings.

Client side example:

<html><head><title>TEST</title>
<script type="text/javascript" src="jquery.js"></script>
<script>

pyurl = "/cgi-bin/main.py"; // Just for example. It can be anything. Doesn't need to be Python at all
function callpy (argdict) {
    $.post(pyurl, argdict, function (data) {    
    // Here comes whatever you'll do with the Python's output.
    // For example:
    document.write(data);
    });
}
</script>
</head><body>
<!-- Now use it: -->
<a href="#" onclick='javascript:callpy({"s": "bow", "someother": "argument"});'>CLICK HERE FOR TEST!!!</a>
</body></html>

On server side (CGI just for example):

#! /usr/bin/env python
import cgi, cgitb
cgitb.enable()

print "Content-Type: text/html\n"
i = cgi.FieldStorage()
if i.has_key("s"):
    if i["s"].value=="bow": print "Yeeeeey!!!! I got 'bow'!!!<br>"
    else: print "Woops! I got", i["s"].value, "instead of 'bow'<br>"
else: print "Argument 's' not present!<br>"
print "All received arguments:"
for x in i.keys(): print x, "<br>"

7 Comments

what is argdict ? and server side i need to add new file or edit in pre existing file ?
argdict is associative array that holds arguments for query string which will be passed to your server-side python script. It will be automatically transformed and passed using POST method. I called it argdict because it is defined same as in Python and it has the same purpose. See how I called callpy() function inside an onclick event.
Server-side Python code have to be executed by a web server. Execution can be performed in any way I mentioned in my answer. The example server-side script I provided is a CGI script. You may use WSGI instead and if you want, you can use django or flask or whatever you want. The important thing is, your script must receive your arguments from the web site, and produce required output.
You may copy-paste my client-side javascript code in your HTML, add what you need instead of 'document.write(data)', and call callpy() function where you need it. You can also modify my CGI script to do the processing you need and enable CGI in your web server to execute it. Other than that, my code shows how it is done, not how you should do it. This you will have to work out by yourself as I don't know what is your setup and what do you exactly need.
If you have main.py finished, you have to add an execution line at the beginning (if on Unix), add the line 'print "Content-Type: text/html\n', print all output of your script to stdout, make the script executable, put it in right directory, enable CGI in your web server for that directory and you have CGI script finished.
|

Your Answer

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