Oke das obere alles mit asktype und asklength habe ich alles behoben.
Was soll ich dann statt fetchall machen kann ich mit einer for schleife direkt durch einen Cursor gehen?
Ja ich weiß was `plot_data['werttypone'] == row[2] in row` bedeutet. Was meinst du ist daran falsch soll ich dem row einen anderen Namen geben oder was ist das Problem.
commit hab ich entfernt.
Was genau meinst du mache ich zu viel bzw. falsch bei dem Datum? Und was ist unklar bei dem Rest?
Code: Alles auswählen
@app.route('/dashboard/<id>', methods=['GET', 'POST'])
def dashboard(id):
if 'username' in session:
start = "2018-01-01_00:00"
end = "2019-01-01_00:00"
data_one = ""
datatype = ""
ask_length = 0
plot_luft = plot_temp = plot_erde = plot_lux = plot_luft_z = plot_temp_z = plot_erde_z = plot_lux_z = []
start_src = 0
start_src_ask = 0
mess_id = "1"
if request.method == 'GET':
ask_length = 96
start_src_ask = 1
elif request.method == 'POST':
if 'mess_id' in request.form:
mess_id = request.form['mess_id']
ask_length = 96
start_src_ask = 1
elif 'last' in request.form:
last = request.form['last']
if last == 'tag':
ask_length = 96
start_src_ask = 1
elif last == 'woche':
ask_length = 672
start_src_ask = 1
else:
ask_length = 2880
start_src_ask = 1
elif 'date' in request.form:
ask_length = 1
start_date = request.form['start_date']
start = start_date + "_00:00"
end_date = request.form['end_date']
end = end_date + "_00:00"
data_one = 2
start_src = 1
#letzter tag
datatype = ""
start_src_ask = 1
if start_src_ask == 1:
plot_luft = [make_plot(mess_id, 'luftfeuchtigkeit', ask_length, None, None)]
plot_temp = [make_plot(mess_id, 'temperatur', ask_length, None, None)]
plot_erde = [make_plot(mess_id, 'erdfeuchtigkeit', ask_length, None, None)]
plot_lux = [make_plot(mess_id, 'lux', ask_length, None, None)]
if start_src == 1:
plot_luft_z = [make_plot(mess_id, 'luftfeuchtigkeit', 96, start, end)]
plot_temp_z = [make_plot(mess_id, 'temperatur', 96, start, end)]
plot_erde_z = [make_plot(mess_id, 'erdfeuchtigkeit', 96, start, end)]
plot_lux_z = [make_plot(mess_id, 'lux', 96, start, end)]
plot_luft = [make_plot(mess_id, 'luftfeuchtigkeit', 96, None, None)]
plot_temp = [make_plot(mess_id, 'temperatur', 96, None, None)]
plot_erde = [make_plot(mess_id, 'erdfeuchtigkeit', 96, None, None)]
plot_lux = [make_plot(mess_id, 'lux', 96, None, None)]
return render_template('dashboard.html', mess_id=mess_id, plot_luft=plot_luft, plot_temp=plot_temp, plot_erde=plot_erde, plot_lux=plot_lux, data_one=data_one, plot_luft_z=plot_luft_z, plot_temp_z=plot_temp_z, plot_erde_z=plot_erde_z, plot_lux_z=plot_lux_z)
else:
return redirect(url_for('index'))
def make_plot(mess_id, datatype, ask_length, start, end):
time = []
wertone = []
werttwo = []
plot_data = PLOTS[datatype]
conn = sqlite3.connect('data.db')
print ("Opened database " + datatype + " successfully")
cur = conn.cursor()
if plot_data['werttyptwo'] != None:
length = ask_length * 2
else:
length = ask_length
if start == None or end == None :
cur.execute("SELECT id, messstation_id, werttyp_id, timestamp, wert from messung WHERE messstation_id = ? AND werttyp_id = ? OR werttyp_id = ?ORDER BY timestamp DESC, werttyp_id ASC LIMIT ?;", [mess_id, plot_data['werttypone'], plot_data['werttyptwo'], length])
else:
cur.execute("SELECT id, messstation_id, werttyp_id, timestamp, wert from messung WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp DESC, werttyp_id ASC;", (start, end))
rows = cur.fetchall()
for row in rows:
if plot_data['werttypone'] == row[2] in row:
time.append(row[3])
wertone.append(row[4])
if plot_data['werttyptwo'] == row[2] in row:
werttwo.append(row[4])
conn.close()
time.reverse()
wertone.reverse()
if 'werttyptwo' in plot_data:
werttwo.reverse()
datetimes = [datetime.datetime.strptime(d, '%Y-%m-%d %H:%M') for d in time]
df = pd.DataFrame(time, columns=['date'])
df.loc[:, 'date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M')
plot = figure(plot_height=300, x_axis_type='datetime', sizing_mode='scale_width', tools="save", toolbar_location=None)
source = ColumnDataSource(data=dict( date=df['date'], wertone=wertone))
plot1 = plot.line('date', 'wertone', line_width=2, source=source, legend=plot_data['label1'])
if datatype == 'luftfeuchtigkeit' or datatype == 'temperatur':
source = ColumnDataSource(data=dict( date=df['date'], wertone=wertone, werttwo=werttwo))
plot2 = plot.line('date', 'werttwo', line_width=2, source=source, legend=plot_data['label2'], color="firebrick")
hover = HoverTool(
tooltips=[('Uhrzeit:', '@date{%d.%m.%Y %H:%M}'), (plot_data['label1'], '@wertone{0.00}') ],
formatters={'date' : 'datetime',},
mode='vline'
)
if datatype == 'luftfeuchtigkeit' or datatype == 'temperatur':
hover.renderers=[plot2]
hover.tooltips=[('Uhrzeit:', '@date{%d.%m.%Y %H:%M}'), (plot_data['label1'], '@wertone{0.00}'), (plot_data['label2'], '@werttwo{0.00}') ]
else:
hover.renderers=[plot1]
plot.add_tools(hover)
plot.xaxis.formatter=DatetimeTickFormatter( hours=["%H:%M"], days=["%d.%m.%Y %H:%M"], months=["%d.%m.%Y %H:%M"], years=["%d.%m.%Y %H:%M"])
plot.xaxis.major_label_orientation = pi/5
plot.yaxis.axis_label = plot_data['ylabel']
plot.legend.location ="top_left"
plot.legend.click_policy="hide"
script, div = components(plot)
return script, div