Run Python script from AJAX or JQuery

前端 未结 3 673
無奈伤痛
無奈伤痛 2020-12-03 05:52

We have a requirement to run python scripts from Javascript. We have to pass an input String to the python script as an argument, and display the python output onto our web

相关标签:
3条回答
  • 2020-12-03 05:52

    Your Ajax call will be a web request (http) to the python script, so your python script needs to listen on the port for such requests and respond back.

    Above mentioned method is recommended, to use simple web server inside your sample ,py or you can use an existing ones such as Flask (or many others).

    FYI - for your simple task I would not recommend Django (its a big and heavy web framework with lots of bells you do not need).

    0 讨论(0)
  • 2020-12-03 06:09

    Getting data from the fields should be done as follows:

    # Get data from fields
    output = data.getvalue("param")
    

    Also, realized there are ^M characters issues while trying to execute, which I resolved using the vi editor.

    0 讨论(0)
  • 2020-12-03 06:14

    In full transparency I am a contributor to this GitHub repo. I was able to solve this problem using WayScript AJAX triggers:

    See GitHub Repo for instructions: https://github.com/wayscript/ajax-to-python-example

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