Guest User

Untitled

a guest
Oct 14th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import dash
  4. from dash import Dash, html, dcc, Input, Output
  5. import dash_daq as daq
  6. import psutil
  7. import time
  8. import sys
  9.  
  10. UPDATE_DELAY = 1 # in second
  11.  
  12. app = Dash(__name__)
  13.  
  14. # ---------------------------------------------------------------
  15. # get_size(bytes)
  16. # ----------------------------------------------------------------
  17.  
  18. def get_size(bytes):
  19. """
  20. Returns size of bytes in a nice format
  21. """
  22. for unit in ['', 'K', 'M', 'G', 'T', 'P']:
  23. if bytes < 1024:
  24. return f"{bytes:.2f}{unit}B"
  25. bytes /= 1024
  26.  
  27.  
  28. io = psutil.net_io_counters()
  29. bytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv
  30. # sleep for `UPDATE_DELAY` seconds
  31. time.sleep(UPDATE_DELAY)
  32. # get the stats again
  33. io_2 = psutil.net_io_counters()
  34. # new - old stats gets us the speed
  35. us, ds = io_2.bytes_sent - bytes_sent, io_2.bytes_recv - bytes_recv
  36. # print the total download/upload along with current speeds
  37. uploadSpeed = get_size(us / UPDATE_DELAY)
  38. downloadSpeed = get_size(ds / UPDATE_DELAY)
  39. dbytes = get_size(io_2.bytes_recv)
  40. ubytes = get_size(io_2.bytes_sent)
  41.  
  42. # print(float(downloadSpeed[:-2]))
  43. # sys.exit()
  44.  
  45. app = dash.Dash(__name__, assets_folder = 'assets', include_assets_files = True)
  46. def serve_layout():
  47. io = psutil.net_io_counters()
  48. bytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv
  49. # sleep for `UPDATE_DELAY` seconds
  50. time.sleep(UPDATE_DELAY)
  51. # get the stats again
  52. io_2 = psutil.net_io_counters()
  53. # new - old stats gets us the speed
  54. us, ds = io_2.bytes_sent - bytes_sent, io_2.bytes_recv - bytes_recv
  55. return html.Div(
  56. html.Div([
  57. daq.Gauge(
  58. showCurrentValue=True,
  59. color={"gradient":True,"ranges":{"green":[0,200],"blue":[200,500],"yellow":[500,800],"red":[800,1000]}},
  60. label=f'Inspired Download Bandwidth',
  61. labelPosition='top',
  62. id='download-gauge',
  63. # label="Default",
  64. max = 1000,
  65. size=300,
  66. units='KB/s',
  67. style={'display': 'block' },
  68. value=float(get_size(ds / UPDATE_DELAY)[:-2])
  69. ),
  70. daq.Gauge(
  71. showCurrentValue=True,
  72. color={"gradient":True,"ranges":{"green":[0,200],"blue":[200,500],"yellow":[500,800],"red":[800,1000]}},
  73. label=f'Inspired Upload Bandwidth',
  74. labelPosition='top',
  75. id='upload-gauge',
  76. # label="Default",
  77. max = 1000,
  78. size=300,
  79. units='KB/s',
  80. style={'display': 'block' },
  81. value=float(get_size(us / UPDATE_DELAY)[:-2])
  82. ),
  83. ]))
  84.  
  85. app.layout = serve_layout
  86.  
  87. # @app.callback(Output('download-gauge', 'value'), Input('download-gauge', 'value'))
  88. # @app.callback(Output('upload-gauge', 'value'), Input('upload-gauge', 'value'))
  89.  
  90. # ---------------------------------------------------------------
  91. # update_output()
  92. # ----------------------------------------------------------------
  93.  
  94. def update_output(value):
  95. return value
  96.  
  97. if __name__ == '__main__':
  98. app.run_server(debug=True)
  99.  
Advertisement
Add Comment
Please, Sign In to add comment