Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import dash
- from dash import Dash, html, dcc, Input, Output
- import dash_daq as daq
- import psutil
- import time
- import sys
- UPDATE_DELAY = 1 # in second
- app = Dash(__name__)
- # ---------------------------------------------------------------
- # get_size(bytes)
- # ----------------------------------------------------------------
- def get_size(bytes):
- """
- Returns size of bytes in a nice format
- """
- for unit in ['', 'K', 'M', 'G', 'T', 'P']:
- if bytes < 1024:
- return f"{bytes:.2f}{unit}B"
- bytes /= 1024
- io = psutil.net_io_counters()
- bytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv
- # sleep for `UPDATE_DELAY` seconds
- time.sleep(UPDATE_DELAY)
- # get the stats again
- io_2 = psutil.net_io_counters()
- # new - old stats gets us the speed
- us, ds = io_2.bytes_sent - bytes_sent, io_2.bytes_recv - bytes_recv
- # print the total download/upload along with current speeds
- uploadSpeed = get_size(us / UPDATE_DELAY)
- downloadSpeed = get_size(ds / UPDATE_DELAY)
- dbytes = get_size(io_2.bytes_recv)
- ubytes = get_size(io_2.bytes_sent)
- # print(float(downloadSpeed[:-2]))
- # sys.exit()
- app = dash.Dash(__name__, assets_folder = 'assets', include_assets_files = True)
- def serve_layout():
- io = psutil.net_io_counters()
- bytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv
- # sleep for `UPDATE_DELAY` seconds
- time.sleep(UPDATE_DELAY)
- # get the stats again
- io_2 = psutil.net_io_counters()
- # new - old stats gets us the speed
- us, ds = io_2.bytes_sent - bytes_sent, io_2.bytes_recv - bytes_recv
- return html.Div(
- html.Div([
- daq.Gauge(
- showCurrentValue=True,
- color={"gradient":True,"ranges":{"green":[0,200],"blue":[200,500],"yellow":[500,800],"red":[800,1000]}},
- label=f'Inspired Download Bandwidth',
- labelPosition='top',
- id='download-gauge',
- # label="Default",
- max = 1000,
- size=300,
- units='KB/s',
- style={'display': 'block' },
- value=float(get_size(ds / UPDATE_DELAY)[:-2])
- ),
- daq.Gauge(
- showCurrentValue=True,
- color={"gradient":True,"ranges":{"green":[0,200],"blue":[200,500],"yellow":[500,800],"red":[800,1000]}},
- label=f'Inspired Upload Bandwidth',
- labelPosition='top',
- id='upload-gauge',
- # label="Default",
- max = 1000,
- size=300,
- units='KB/s',
- style={'display': 'block' },
- value=float(get_size(us / UPDATE_DELAY)[:-2])
- ),
- ]))
- app.layout = serve_layout
- # @app.callback(Output('download-gauge', 'value'), Input('download-gauge', 'value'))
- # @app.callback(Output('upload-gauge', 'value'), Input('upload-gauge', 'value'))
- # ---------------------------------------------------------------
- # update_output()
- # ----------------------------------------------------------------
- def update_output(value):
- return value
- if __name__ == '__main__':
- app.run_server(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment