vue.js和python脚本之间交互的最佳方式



我想知道什么是在vue应用程序中基于输入运行python脚本的最佳方法。我发现我可以用flask制作一个api,它可以运行我选择的python脚本,使用curl请求我可以从vue应用程序调用它。

你对如何做到这一点还有其他想法吗?

我能想到的最好的想法是在Flask API中设置HTTP端点,然后从那里直接从Vue应用程序调用API。

类似于以下内容:Python脚本

from flask import Flask, request
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return "You data here"
else:
body = request.body #access the body you sent on JS below
<put whatever you want to do here>

Javascript

const myRequest = async() =>{
const response = fetch("http://localhost:8000", {
method: "POST",
headers:{
"Content-Type": "Application/JSON"}
body:{<your stuff here>}
)
}

最新更新