homecontrol-dash: use f-strings

This commit is contained in:
Konstantin Koslowski 2020-02-10 16:05:18 +01:00
parent 9c0b659b5f
commit d021b20ada

View file

@ -13,11 +13,12 @@ URL_BASE="http://innocence:5000"
def get_sensors():
ret = {}
try:
url = "%s/sensor/get" % URL_BASE
url = f"{URL_BASE}/sensor/get"
res = requests.get(url)
ret = res.json()
except Exception as ex:
print('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
print(f"Exception Type: {type(ex).__name__}, args:\n{ex.args}")
return ret
@ -25,15 +26,20 @@ def get_values(sensorId, min_ts, max_ts, limit):
ret = {}
if sensorId:
try:
url = "%s/sensor/get_values/%s?min_ts=%s&max_ts=%s&limit=%s" % (URL_BASE,
sensorId, min_ts, max_ts, limit)
url = f"{URL_BASE}/sensor/get_values/{sensorId}?min_ts={min_ts}&max_ts={max_ts}&limit={limit}"
# print(url)
res = requests.get(url)
ret = res.json()[sensorId]
except Exception as ex:
print('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
print(f"Exception Type: {type(ex).__name__}, args:\n{ex.args}")
return ret
def pTime(value, fmt="%Y/%m/%d %H:%M"):
if value > 0:
return time.strftime(fmt, time.localtime(float(value)))
else:
return 0
sensors = get_sensors()
sensor = next(iter(sensors), None)
@ -108,7 +114,7 @@ def update_slider_min(sensorId):
min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"]
# print("min: [%f [%f] %f]" % (min_ts, min_ts, max_ts))
# print(f"min: [{min_ts} [{cur_ts}] {max_ts}]"
return min_ts, cur_ts, max_ts
@ -116,8 +122,7 @@ def update_slider_min(sensorId):
Output('slider-min-txt', 'children'),
[Input('slider-min', 'value')])
def update_slider_min_txt(value):
return "From: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))) if
value > 0 else 0)
return f"From: {pTime(value)}"
@app.callback(
[Output('slider-max', 'min'), Output('slider-max', 'value'),
@ -132,7 +137,7 @@ def update_slider_max(sensorId):
min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"]
# print("max: [%f [%f] %f]" % (min_ts, max_ts, max_ts))
# print(f"max: [{min_ts} [{max_ts}] {max_ts}]"
return min_ts, max_ts, max_ts
@ -140,14 +145,14 @@ def update_slider_max(sensorId):
Output('slider-max-txt', 'children'),
[Input('slider-max', 'value')])
def update_slider_max_txt(value):
return "To: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))))
return f"To: {pTime(value)}"
@app.callback(
Output('slider-limit-txt', 'children'),
[Input('slider-limit', 'value')])
def update_slider_limit_txt(value):
return "Limit: %s" % (value if value > 0 else None)
return f"Limit: {value if value > 0 else None}"
@app.callback(
@ -161,7 +166,7 @@ def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
if "values" in res:
v = res["values"]
s = res["sensorType"]
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))]
x = [pTime(v[i]["ts"], fmt="%m%d-%H%M") for i in range(len(v))]
if s == "luminance":
y = [log10(v[i]["value"]/5+1) for i in range(len(v))]
else:
@ -171,8 +176,8 @@ def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
x = [0]
y = [0]
return {
'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': "%s" % (s)} ],
'layout': { 'title': 'Data for sensor: %s (%d elements)' % (s, len(x)) }
'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': f'{s}'} ],
'layout': { 'title': f'Data for sensor: {s} ({len(x)} elements)' }
}
if __name__ == '__main__':