I think you should add one more condition when you choose both of them. Something as below:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash
import dash_bootstrap_components as dbc
from dash import Input, Output, dcc, html
import time, os, glob, sys
import multiprocessing as mp
from dash.exceptions import PreventUpdate
app = dash.Dash(__name__, title='Network Monitoring Dashboard', external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div([
dcc.Interval(
id='interval-component',
interval=5*3000, # in milliseconds
n_intervals=0
),
dbc.Col([
html.Div([
html.Label(['Choose a graph:'],style={'font-weight': 'bold'}),
dcc.Dropdown(
id='dropdown',
options=[
{'label': 'Top 10 incoming traffic IP addresses', 'value': 'fig1'},
{'label': 'Top 10 outgoing traffic IP addresses', 'value': 'fig2'},
{'label': 'Top Active Ports', 'value': 'fig3'},
{'label': 'Top Active IP-Ports', 'value': 'fig4'},
],
multi=True,
value=['fig1'],
style={"width": "60%"}, clearable=False),
]),
html.Div(id='graph-container'),
])
])
@app.callback(Output('graph-container', 'children'),
[Input('dropdown', 'value'),
Input('interval-component','n_intervals')])
def update_graph(value, n):
data = [['192.168.0.105','192.168.0.103','TCP'],
['192.168.0.103','192.168.0.105','TCP'],
['192.168.0.105','192.168.0.103','UDP'],
['52.98.151.66','192.168.0.105','TCP'],
['192.168.0.105','52.98.151.66','ICMP'],
['52.98.228.226','192.168.0.105','ICMP'],
['52.98.228.226','192.168.0.105','UDP'],
['192.168.0.103','224.0.0.251','1UDP'],
['192.168.0.105','52.98.228.226','TCP']]
column_names = ['Source', 'Destination','Protocol']
for i in range(6):
df = pd.DataFrame (data)
df.columns = column_names
inbound_ip_count = df.groupby('Source').size().nlargest(n=10)
outbound_ip_count = df.groupby('Destination').size().nlargest(n=10)
port_count = df.groupby('Protocol').size().nlargest(n=10)
sources_grp = df.groupby(['Source', 'Protocol'], as_index=False)['Destination']\
.count().sort_values('Destination', ascending=False)
# Get the top 5 occurrences of Source
top_sources = df['Source'].value_counts()[:5].index.tolist()
# Filter the dataframe to only include the top 5 occurrences of Source
top_sources_df = sources_grp[sources_grp['Source'].isin(top_sources)]\
.reset_index(drop=True)
# Rename the count column to 'Protocol Count'
top_sources_df = top_sources_df.rename(columns={'Destination': 'Protocol Count'})
if value == ['fig1']:
fig1 = go.Figure(data = 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')
)
return dcc.Graph(id='live-graph2', figure = fig1)
# Ploting top 10 Outbound traffic IP addresses
elif value == ['fig2']:
fig2 = go.Figure(data = px.bar(df['Destination'],
x=outbound_ip_count.index, y=outbound_ip_count.values,
labels={
'x': 'IP Addresses',
'y': 'Packets',
},
title='Top 10 Outbound traffic IP addresses')
)
return dcc.Graph(id='live-graph2', figure = fig2)
elif value == ['fig1','fig2']:
df2 = df.melt(id_vars=['Protocol'], value_vars=['Source','Destination'])
df3= df2.groupby(['variable','value'])['value'].count().reset_index(name='counts')
fig3 = px.bar(df3,x='value', y='counts',facet_col='variable')
return dcc.Graph(id='live-graph2', figure = fig3)
else:
raise PreventUpdate
if __name__ == '__main__':
app.run_server(debug=False)