flask refre sh кнопка dosent перезагрузить страницу правильно - PullRequest
0 голосов
/ 06 августа 2020

Я пытаюсь показать график на странице HTML, у меня есть переключатель, чтобы выбрать тип фильтрации, который хочет пользователь, затем при нажатии refre sh я хочу, чтобы страница перезагрузилась, показывая новый график мой код python создан. проблема, с которой я столкнулся, заключается в том, что кнопка refre sh только обновляет страницу без перезагрузки нового изображения, которое было построено. как я могу решить эту проблему, есть ли способ перезагружать страницу при нажатии кнопки, или перезагружать изображение каждый раз, когда пользователь нажимает refre sh, или даже перезагружать страницу каждый раз, когда в python создается новый график код. это мой код flask и картинка, чтобы дать немного контекста 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)



 РЕФРЕ SH  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...