I have the below code that suppose to be doing the below:
1- Capture traffic form a NIC, but this is a separate file which will run continuously outside this section
2- The main_process function will read the files captured by point 1 above and convert them into df, but I changed this to static df, which should be ok for testing. then the same function calculates something and then generates the px plotting, then pass the result outside the function using multiprocessing pipe()
3- The plotting function will receive the drawing details from the main_process function via the MPing pipe() then will start the layout section, it runs for 1 time within the if statement
Now, the MPing will control the starting of each function, I added delays to test controlling the sequence of function
I have few issues with this code:
1- When the server starts, it does not recognize the layout, even though I am controlling what starts first, but probably because it is inside the function
2- If I move the app.layout outside the function, I will not be able to get the data for plotting, as it was not created yet first and second it is inside a function
3- If I move the run_server outside the if block, again it will not recognize the layout
I think I need to use the callback, but not sure how to use it, none of the online examples made me understand how to use it in my case, to be honest, and the reason why I am thinking about the callback is because the data will change every 30 seconds, so the dashboard should be updated accordingly
Any help will be appreciated
import pandas as pd
from dash import html, Dash, dcc
import plotly.express as px
import os, glob, time, sys
from multiprocessing
import Pipe
import multiprocessing as mp
app = Dash(__name__)
def main_process(conn2):
print('main process started')
print('The process ID for the main_process is', os.getpid())
i = 0
while True:
time.sleep(5)
data = [['192.168.0.105','192.168.0.103','6'],
['192.168.0.103','192.168.0.105','6'],
['192.168.0.105','192.168.0.103','6'],
['52.98.151.66','192.168.0.105','6'],
['192.168.0.105','52.98.151.66','6'],
['52.98.228.226','192.168.0.105','6'],
['52.98.228.226','192.168.0.105','6'],
['192.168.0.103','224.0.0.251','17'],
['192.168.0.105','52.98.228.226','6']]
column_names = ['Source', 'Destination','Protocol']
for i in range(6):
df = pd.DataFrame (data)
df.columns = column_names
# Ploting top 10 Inbound traffic IP addresses
inbound_ip_count = df.groupby('Source').size().nlargest(n=10)
top_in_IP = px.bar(df['Source'],
x=inbound_ip_count.index, y= inbound_ip_count.values,
labels={
'x': 'IP Addresses',
'y': 'Packets',
},
title='Top 10 incoming traffic IP addresses')
data_out = top_in_IP
print('data_out generated')
conn2.send(data_out)
print('data_out sent')
def plotting(conn1):
n = 0
print('The process ID for the plotting is', os.getpid())
while True:
# time.sleep(2)
data_out = conn1.recv()
print('data_out received')
fig0 = data_out
n +=1
print('n is ', n)
if n == 1:
print('starting the layout')
app.layout = html.Div(className="row", children =[
html.H1('Network Traffic charts', style={'text-align':'center'}),
html.Div(children = [
dcc.Graph(figure=fig0, style={'width': '95vh', 'height': '90vh','display': 'inline-block'}),
])
])
print('app.layout loaded')
l = 0 if __name__ == "__main__":
conn1, conn2 = Pipe(duplex=False)
p1 = mp.Process(target=main_process, args=(conn2,))
p2 = mp.Process(target=plotting, args=(conn1,))
p1.start()
p2.start()
time.sleep(10)
l +=1
if l == 1:
print('Before server running', l)
print('The process ID for the server is', os.getpid())
time.sleep(18)
app.run_server(port=2020, debug=False, use_reloader=False)