Execute a python script on button click

后端 未结 2 384
忘掉有多难
忘掉有多难 2020-12-02 19:29

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

相关标签:
2条回答
  • 2020-12-02 19:55

    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.

    0 讨论(0)
  • 2020-12-02 20:10

    Make a page(or a service) in python, which can accept post or get request and process the info and return back a response. It is better if the response is in json format. Then you can use this code to make a call on the button click.

    <input type="text" name="name" id="name">
    <button type="button" id="home" onclick="validate()" value="checkvalue">
    <script>
    $('#id').click(function(){
    
     $.ajax({
          type:'get',
          url:<YOUR SERVERSIDE PAGE URL>,
          cache:false,
          data:<if any arguments>,
          async:asynchronous,
          dataType:json, //if you want json
          success: function(data) {
            <put your custom validation here using the response from data structure >
          },
          error: function(request, status, error) {
            <put your custom code here to handle the call failure>
          }
       });
    });
    </script>
    

    I hope this helps

    0 讨论(0)
提交回复
热议问题