Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a python script on button click

I have an HTML page with one button, and I need to execute a python script when we click on the button and return to the same HTML page with the result.

So I need do some validation on return value and perform some action.

Here is my code:

HTML:

<input type="text" name="name" id="name">
<button type="button" id="home" onclick="validate()" value="checkvalue"></button>

JS:

function validate(){
    if (returnvalue=="test") alert(test)
    else alert ("unsuccessful")
}

What my python code is doing is some validation on the name entered in the text box and gives the return status.

But I need the result back on the same page, so I can do the form submission later with all the details. Any help will be appreciated

like image 502
user2058205 Avatar asked Mar 01 '13 05:03

user2058205


People also ask

How do you call a python function from a button in HTML?

The Best Answer is To run, open command prompt to the New folder directory, type python server.py to run the script, then go to browser type localhost:5000 , then you will see button. You can click and route to destination script file you created. Hope this helpful.


1 Answers

You can use Ajax, which is easier with jQuery

$.ajax({
   url: "/path/to/your/script",
   success: function(response) {
     // here you do whatever you want with the response variable
   }
});

and you should read the jQuery.ajax page since it has too many options.

like image 84
martriay Avatar answered Sep 18 '22 10:09

martriay