From 8d33fe713ce2b626dcc1ae75c1aec5e9f792bed4 Mon Sep 17 00:00:00 2001 From: 0xee Date: Mon, 21 Dec 2020 21:30:17 +0100 Subject: [PATCH] Add number input to select timerange more precisely --- tsplot | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/tsplot b/tsplot index f56b2b6..07f679f 100755 --- a/tsplot +++ b/tsplot @@ -60,25 +60,32 @@ plot_page = html.Div([ id='interval-component', interval=4*1000, # in milliseconds n_intervals=0), - dcc.Dropdown( - id='timerange', - options=[ - {'label': 'Last hour', 'value': 'h'}, - {'label': 'Last day', 'value': 'd'}, - {'label': 'Last week', 'value': 'w'}, - {'label': 'All', 'value': 'all'} - ], - value='d' - ), + html.Div([ + dcc.Input(id="range_count", + type="number", + value=1, + debounce=True, + min=1), + dcc.Dropdown( + id='timerange', + options=[ + {'label': 'hours', 'value': 'h'}, + {'label': 'days', 'value': 'd'}, + {'label': 'weeks', 'value': 'w'}, + {'label': 'all', 'value': 'all'} + ], + value='d' + ), + ]), dcc.Graph(id='graph', animate=False), dcc.Link('Back', href='/') ]) -def get_timedelta(range_str): +def get_timedelta(range_str, count): range_to_delta = { - 'h': datetime.timedelta(hours=1), - 'd': datetime.timedelta(days=1), - 'w': datetime.timedelta(weeks=1), + 'h': datetime.timedelta(hours=count), + 'd': datetime.timedelta(days=count), + 'w': datetime.timedelta(weeks=count), 'all': datetime.timedelta(weeks=10000), } return range_to_delta[range_str] @@ -87,11 +94,12 @@ def get_timedelta(range_str): @app.callback(Output('graph', 'figure'), [Input('interval-component', 'n_intervals'), Input('timerange', 'value'), + Input('range_count', 'value'), Input('url', 'pathname')]) -def update_graph(interval, timerange, pathname): +def update_graph(interval, timerange, range_count, pathname): topics = pathname.strip('/').split('+') if len(topics): - delta = get_timedelta(timerange) + delta = get_timedelta(timerange, range_count) now = int(time.time()) // update_interval_s def make_data(df, name): return {