【问题标题】:How to use input function with flask in Python, to capture user input and then send response based on that response. Like dialogue-flow如何在 Python 中将输入函数与烧瓶一起使用,以捕获用户输入,然后根据该响应发送响应。像对话流
【发布时间】:2025-12-26 08:05:16
【问题描述】:

如何在 Python 中将输入函数与烧瓶一起使用,以获取用户输入,然后根据该响应发送响应。尝试不断地这样做,比如对话流方法。

试图公开此命令行终端以在 UI 中捕获用户输入捕获

#Below is the code
df_final = pandas_dedupe.dedupe_dataframe(df,['Company'])
#Below is the resultant output which get displayed in terminal which asks for user response. 
#I'm trying to expose this to user and get his response.
Company : reliance captal

Company : reliance captive

0/10 positive, 0/10 negative
Do these records refer to the same thing?
(y)es / (n)o / (u)nsure / (f)inished

【问题讨论】:

  • 您是否尝试过使用将 POST 请求与命令数据一起发送到服务器的表单?您可以使用 AJAX 来避免重新渲染。

标签: python python-3.x flask flask-wtforms python-webbrowser


【解决方案1】:

templates/contact.html

<form action="/contact" method="POST">
  <textarea type="text" name="msg"></textarea>
  <label>Messege</label>
  <button type="submit" class="button buttonBlue">
  Submit
  </button>
</form>

app.py
数据存储在msg 变量中,您可以根据该变量发送响应

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    if request.method == 'POST':
        msg = request.form.get('msg')
    return render_template("contact.html")

【讨论】:

  • 感谢 Gaurav,但问题是我正在修改一个库的源代码,该库使用输入函数在命令终端上获取用户输入。我正在尝试将该命令终端输入捕获到 UI 中。