Python 如何询问数据并在数据到达时绘制,与flask在同一页中 当前架构

Python 如何询问数据并在数据到达时绘制,与flask在同一页中 当前架构,python,flask,post,plot,live-streaming,Python,Flask,Post,Plot,Live Streaming,我在路由中有一个表单,用于请求一个新的测量,并为微控制器设置测量参数,微控制器每秒轮询一个文件,当用户请求时,它使用请求的参数执行测量 @app.route('/',方法=['GET','POST']) def set_new_measure(): form=MeasureParametersForm() if form.validate_on_submit(): 下一个度量值部分={ "量度":1,, “g_范围”:int(格式g_范围数据), “采样率”:浮动(形式采样率数据), “采集时间

我在路由中有一个表单,用于请求一个新的测量,并为微控制器设置测量参数,微控制器每秒轮询一个文件,当用户请求时,它使用请求的参数执行测量

@app.route('/',方法=['GET','POST'])
def set_new_measure():
form=MeasureParametersForm()
if form.validate_on_submit():
下一个度量值部分={
"量度":1,,
“g_范围”:int(格式g_范围数据),
“采样率”:浮动(形式采样率数据),
“采集时间”:form.acquisition\u time.data
}
将open(path.next_measure_参数,mode='w')作为f:
dump(next\u measure\u pars,f)
打印(f'使用{next_measure_pars}请求新度量)
如果form.errors:
打印(表单错误)
返回呈现模板('home.html',form=form)
当微控制器执行测量后,它会将结果发布到另一条路径上:目前显示结果图的唯一方法是保存结果,然后从文件系统中打开它

@app.route('/post_data',方法=['GET','post']
如果request.method==“POST”:
x、 y=f_数据。解析_请求(请求)
图=绘图。我的绘图(x,y)
#保存到文件
fig.savefig('figure.png'))
os.startfile('figure.png')
问题: 当收到数据时,我想在设置测量参数的表单所在的同一页面中显示结果图,就在它的下方

我试过的解决方案不起作用
  • 将用于打印的代码放在表单的同一路径上,并区分表单提交和来自传感器的post,但我得到“CSRF令牌丢失错误”
  • 覆盖文件系统中的图形,并在表单路由的HTML中打开它,但它仅在我手动刷新页面时起作用
下面是HTML的表单部分

<form method="POST" action="">
  {{ form.hidden_tag() }}
  <fieldset class="form-group">
    <legend class="border-bottom mb-4">new measure parameters</legend>
    <div class="form-group">
      {{ form.g_range.label(class="form-control-label") }}
      {% if form.g_range.errors %}
      {{ form.g_range(class="form-control form-control-lg is-invalid") }}
      <div class="invalid-feedback">
        {% for error in form.g_range.errors %}
        <span>{{ error }}</span>
        {% endfor %}
      </div>
      {% else %}
      {{ form.g_range(class="form-control form-control-lg") }}
      {% endif %}
    </div>
    <div class="form-group">
      {{ form.sampling_rate.label(class="form-control-label") }}
      {% if form.sampling_rate.errors %}
      {{ form.sampling_rate(class="form-control form-control-lg is-invalid") }}
      <div class="invalid-feedback">
        {% for error in form.sampling_rate.errors %}
        <span>{{ error }}</span>
        {% endfor %}
      </div>
      {% else %}
      {{ form.sampling_rate(class="form-control form-control-lg") }}
      {% endif %}
    </div>
    <div class="form-group">
      {{ form.acquisition_time.label(class="form-control-label") }}
      {% if form.acquisition_time.errors %}
      {{ form.acquisition_time(class="form-control form-control-lg is-invalid") }}
      <div class="invalid-feedback">
        {% for error in form.acquisition_time.errors %}
        <span>{{ error }}</span>
        {% endfor %}
      </div>
      {% else %}
      {{ form.acquisition_time(class="form-control form-control-lg") }}
      {% endif %}
    </div>
  </fieldset>
  <div class="form-group">
    {{ form.submit(class="btn btn-outline-info") }}
  </div>
</form>

{{form.hidden_tag()}}
新的测量参数
{{form.g_range.label(class=“form control label”)}
{%if form.g_range.errors%}
{{form.g_range(class=“form control form control lg is invalid”)}
{%for form.g_range.errors%}
{{error}}
{%endfor%}
{%else%}
{{form.g_range(class=“form control form control lg”)}
{%endif%}
{{form.sampling_rate.label(class=“form control label”)}
{%if form.sampling_rate.errors%}
{{form.sampling_rate(class=“form control form control lg is invalid”)}
{form.sampling_rate.errors%}中的错误为%
{{error}}
{%endfor%}
{%else%}
{{form.sampling_rate(class=“form control form control lg”)}
{%endif%}
{{form.acquisition_time.label(class=“form control label”)}
{%if form.acquisition_time.errors%}
{{form.acquisition_time(class=“form control form control lg is invalid”)}
{form.acquisition_time.errors%}中的错误为%
{{error}}
{%endfor%}
{%else%}
{{form.acquisition_time(class=“form control form control lg”)}
{%endif%}
{{form.submit(class=“btn btn outline info”)}