Esempio n. 1
0
def edit_database(city_id):
    """Views for editing city specific data"""
    month_temperature = []
    month_humidity = []
    city_record = data.get(city_id)
    meteo = [get_city_temperature(city_record), get_city_humidity(city_record)]
    try:
        if request.method == "POST":
            # Get data from the form
            for i in range(12):
                # In a production application we ought to validate the input data
                month_temperature.append(float(request.form[f'temperature{i}']))
                month_humidity.append(int(request.form[f'humidity{i}']))

            # Database update
            for i, month in enumerate(city_record.city_meteo_data):
                month.average_temperature = month_temperature[i]
                month.average_humidity = month_humidity[i]

            db_session.commit()
            return redirect(url_for('main', city_id=city_id))
        else:
            return render_template('edit.html', city_name=city_record.city_name, city_id=city_id, months=MONTHS,
                                                          meteo=meteo)
    except Exception as error:
        return render_template('edit.html', city_name=city_record.city_name, city_id=city_id, months=MONTHS,
                      meteo=meteo, error=error)
Esempio n. 2
0
def get_city_image(city_id):
    """Rendering line charts with city specific data"""
    city = data.get(city_id)
    city_temp = get_city_temperature(city)
    city_hum = get_city_humidity(city)

    plt.clf()
    plt.plot(MONTHS, city_temp, color='blue', linewidth=2.5, linestyle='-')
    plt.ylabel('Mean Daily Temperature', color='blue')
    plt.yticks(color='blue')
    plt.twinx()
    plt.plot(MONTHS, city_hum, color='red', linewidth=2.5, linestyle='-')
    plt.ylabel('Average Relative Humidity', color='red')
    plt.yticks(color='red')
    plt.title(city.city_name)

    img = BytesIO()
    plt.savefig(img)
    img.seek(0)
    return img
Esempio n. 3
0
def get_main_image():
    """Rendering the scatter chart"""
    yearly_temp = []
    yearly_hum = []

    for city in data:
        yearly_temp.append(sum(get_city_temperature(city)) / 12)
        yearly_hum.append(sum(get_city_humidity(city)) / 12)

    plt.clf()
    plt.scatter(yearly_hum, yearly_temp, alpha=0.5)
    plt.title('Yearly Average Temperature/Humidity')
    plt.xlim(70, 95)
    plt.ylabel('Yearly Average Temperature')
    plt.xlabel('Yearly Average Relative Humidity')
    for i, txt in enumerate(CITIES):
        plt.annotate(txt, (yearly_hum[i], yearly_temp[i]))

    img = BytesIO()
    plt.savefig(img)
    img.seek(0)
    return img