【问题标题】:restarting a flask application/ refreshing a template重新启动烧瓶应用程序/刷新模板
【发布时间】:2022-09-28 03:32:37
【问题描述】:

我想在 POST 请求发生时重新启动烧瓶应用程序。

我的想法是一个网站,它是一个 index.html 文件,服务器写入发布的数据。

一个粗略的路径是这样的:

  1. 向服务器发送数据 --> 2. 处理数据 --> 3. 向服务器写入数据 --> 4. 重启 Flask 实例/更新 html

    我已经到了第 3 部分。第 4 部分对我不起作用,我尝试任何方法。有人能帮我吗?

    我也使用了render_template,这可能是问题所在。

    当前代码:

    #Server-Side
    @app.route(\"/\", methods=[\"POST\"])
    def rnder():
      f = open(\"templates/index.html\", \"wt\")
      f.write(str(request.json))
      f.close()
      f = open(\"restart.py\", \"rt\")
      f = f.read()
      try:
        exec(f)
      except BrokenPipeError:
            sys.stdout = None
      quit()
      
    @app.route(\"/\", methods=[\"GET\"])
    def hello():
      return render_template(\'index.html\')
    
    #Sending Data
    data = {\"Hello World\": \"This is a test\"}
    requests.post(url=\"http://abcdefghijklmnopqrstuvw.xyz\", json=data)
    

    标签: python flask


    【解决方案1】:

    添加debug=True 做到了这一点。每次更新都会刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      相关资源
      最近更新 更多