我可以从Python Flask应用程序调用JavaScript函数吗?

时间:2020-10-19 13:28:18

标签: javascript python html http

我一直在我的Flask网络应用中尝试为以下过程找到最佳解决方案。

  1. 从客户端调用Flask端点并传递变量
  2. 将变量传递给JavaScript函数
  3. JavaScript会更新现有的HTML代码块

我的Flask应用程序公开了这样的端点。

@app.route("/send_text", methods=["POST"])
def send_text():
    # Get parameter from client request
    request_content = request.get_json(force=True)
    bot_question_text = request_content.get('text')
return render_template("index.html", text=text)

我可以毫无问题地调用此端点,但是,我想将变量 text 传递给index.html中的JavaScript函数。

function UpdateMessage(text_from_flask) {
  const msgHTML = `<div class="msg-text">text_from_flask</div>`;
  msgerChat.insertAdjacentHTML("beforeend", msgHTML);
  msgerChat.scrollTop += 500;
}

此JavaScript函数需要将HTML代码块插入下面的现有HTML代码中。

<main class="msger-chat">
</main>

我尝试了render_template到index.html并将变量从Flask传递给HTML,但是没有 动态工作(每次调用Flask端点时,我想更新 msger-chat 块)

是否有最佳实践,或者有人知道这种用例吗?
我将不胜感激,因为我已经坚持了几天了。

0 个答案:

没有答案