烧瓶刷新按钮剂量重新加载页面正确

时间:2020-08-06 19:53:27

标签: javascript python html matplotlib flask

我正在尝试在HTML页面上显示图,我已经选择了单选按钮以选择用户所需的过滤类型,然后在单击“刷新”时,我希望页面重新加载以显示我的python代码创建的新图。我的问题是刷新按钮仅刷新页面而不刷新已绘制的新图像。我如何解决此问题?有没有一种方法可以在单击按钮时重新加载页面,或者在用户每次单击刷新时重新加载图像,或者甚至在python代码中每次创建新图时都重新加载页面。 这是我的烧瓶代码和一张图片,提供了一些上下文 enter image description here

@app.route('/device_statistics', methods=['POST', 'GET'])
def device_stats():
    if 'username' not in session:
        return redirect(url_for('auth'))

 
    error = None


  


    mydevices = ['Hall', 'Door101', 'Door102', 'Garage' , 'Fiat 500','Audi A3']
    Door1 = {'Mon': 46, 'Tues': 20, 'Wed': 90, 'Thu': 15, 'Fri': 100, 'Sat': 130, 'Sun': 120}
    Door1Month = {'Jan': 46, 'Feb': 20, 'Mar': 90, 'Apr': 15, 'May': 100, 'Jun': 130, 'Jul':  120, 'Aug': 98, 'Sept': 80, 'Oct': 112, 'Nov': 70, 'Dec': 50}
    Door1Season = {'Spring ': 799, 'Summer': 590, 'Autumn': 801, 'Winter': 899}



    if request.method == 'POST':
        chosendevice = request.form.get('device', 'not in form')
        print(chosendevice)
        timestamp = request.form['radio']
        print(str(timestamp))

        if timestamp == 'Days':
            #if os.path.exists('static/pictures/' + 'Device_statistics.png'):
                    #os.remove('static/pictures/' + 'Device_statistics.png')
            plt.plot(Door1.keys(), Door1.values(), linestyle='-', marker='o')

            plt.xlabel('Days of the week  ')
            plt.savefig('static/pictures/Device_statistics.png')
            return redirect(url_for('device_stats'))
          


        elif timestamp == 'Months':
            #if os.path.exists('static/pictures/' + 'Device_statistics.png'):
            #        os.remove('static/pictures/' + 'Device_statistics.png')
            plt.plot(Door1Month.keys(), Door1Month.values(), linestyle='-', marker='o')
            plt.xlabel('months of the year ')
            plt.savefig('static/pictures/Device_statistics.png')
            plt.clf()
            


        elif timestamp == 'Seasons':
            #if os.path.exists('static/pictures/' + 'Device_statistics.png'):
            #        os.remove('static/pictures/' + 'Device_statistics.png')
            plt.plot(Door1Season.keys(), Door1Season.values(), linestyle='-', marker='o')
            plt.xlabel('Seasons')
            plt.savefig('static/pictures/Device_statistics.png')
            plt.clf()
           
        else:
            if os.path.exists('static/pictures/' + 'Device_statistics.png'):
                os.remove('static/pictures/' + 'Device_statistics.png')
            plt.plot(Door1.keys(), Door1.values(), linestyle='-', marker='o')
            plt.savefig('static/pictures/Device_statistics.png')
            plt.clf()
            
  

    return render_template('device_statistics.html', mydevices=mydevices, error=error)



<div class="container">
      <form  action= {{redirect(url_for('index'))}} method='POST'>
    <input type='submit' value="REFRESH" >
</form>

0 个答案:

没有答案