I am trying something new with flask, Python and the arduino. I want to measure things with the arduino, calculate and save some values through phyton and put it on display through flask.
Right now I can make sure that the arduino receives and gives information and the displaying to a localhost worked. However I want to calculate the average of a list that contain numbers (ints, floats and doubles)
However when I try to use statistics.mean(recorded_humidities)
or any other recorded_ list I get hit with a error which is:
app.py, line 63, in greenhouse_dashboard
average_humidity = statistics.mean(recorded_humidities)
AttributeError: 'dict' object has no attribute 'mean'`
I can make a list in a seperate file, fill in some random numbers and then I can use the statistics.mean(list_name)
just fine
I am confused what is happening here and I do not know what I can do or did wrong so I am hoping to find a answer here.
Here is the full code where values get calculated:
import statistics
import sensor
from flask import Flask
from flask import render_template
from datetime import datetime
statistics = {}
recorded_humidities = []
recorded_temperatures = []
recorded_klux = []
minimal_humidites = 0
minimal_temperatures = 0
minimal_klux = 0
max_humidites = 0
max_temperatures = 0
max_klux = 0
average_humidity = 0
average_temperatures = 0
average_klux = 0
def current_time():
rightNow = datetime.now()
time = rightNow.strftime("%d %B %Y, %H:%M:%S")
time = time.lstrip('0')
time = time.lower()
day = rightNow.strftime("%A")
return "It is " time " on a " day "."
def current_humidity():
humidity = sensor.humidity
return humidity
def current_temperature():
temperature = sensor.temperature
return temperature
def current_klux():
klux = sensor.klux
return klux
app = Flask(__name__)
@app.route('/')
def greenhouse_dashboard():
recorded_humidities.append(current_humidity())
recorded_temperatures.append(current_temperature())
recorded_klux.append(current_klux())
#current values
#statistics["current_time"] = current_time()
#statistics["current_klux"] = current_klux()
#statistics["current_temperature"] = current_temperature()
#statistics["current_humidity"] = current_humidity()
#average values
average_humidity = statistics.mean(recorded_humidities) #fake value
average_temperatures = 2 #goes wrong
average_klux = 3 #goes wrong
#max values
max_humidites = max(recorded_humidities)
max_temperatures = max(recorded_temperatures)
max_klux = max(recorded_klux)
#min values
minimal_humidites = min(recorded_humidities)
minimal_temperatures = min(recorded_temperatures)
minimal_klux = min(recorded_klux)
return render_template('general.html',
time = current_time(),
humidity = current_humidity(),
temperature = current_temperature(),
klux = current_klux(),
average_klux = average_klux,
average_temperature = average_temperatures,
average_humidity = average_humidity,
min_klux = minimal_klux,
min_temperature = minimal_temperatures,
min_humidity = minimal_humidites,
max_klux = max_klux,
max_temperature = max_temperatures,
max_humidity = max_humidites)
I barely use stackoverflow but I want to use it more. if I should do certain things because it is handy or I made a mistake in giving information or setting up this question then please tell me so since I do want to use this correctly and want to help other in the end as well.
thank you in advance.
CodePudding user response:
You need to either rename the statistics dictionary, or alias the statistics module import.
Solution One - Rename the statistics
dict
:
# statistics = {}
statistics_dict = {}
Solution Two - Alias the statistics
module (not recommended):
# import statistics
import statistics as stats