Hi,
This is in addition to my other question that I am still waiting for a response to
The question is that in the app.py, the first line inside the while True
got executed once I open the page, so it creates the file, but the second line got executed only after pressing the submit button, which is what is needed, the point is that why there is the difference, I want both of them to be executed after the pressing the submit button.
App.py
import dash
from dash import html, dcc, Dash, callback, Input, Output
import multiprocessing as mp
import os, sys, time
app = dash.Dash(__name__, use_pages=True)
app.layout = html.Div(
[
dcc.Store(id="store", data={}),
html.Div("Network Traffic Dashboard", style={'fontSize':50, 'textAlign':'center'}),
html.Div([
dcc.Link(page['name']+" | ", href=page['path'])
for page in dash.page_registry.values()
]),
html.Hr(),
html.Div(id='selected_nic'),
dash.page_container
]
)
@callback(
Output('selected_nic', 'nic'),
[Input('store', 'data')]
)
def capturing_loop(nic):
while True:
file_name = (r'C:/DataCapture/traffic_{}.txt'.format(int(time.time())))
os.system(f'cmd /c D:/OneDrive/NetPredict/dumpcap/dumpcap -i {nic} -q -a duration:10 -w {file_name}.pcap')
os.system(f'cmd /c D:/OneDrive/NetPredict/dumpcap/tshark -r "{file_name}.pcap" -V -T fields -e frame.number -e _ws.col.Time -e ip.src -e ip.dst -e ip.proto -E header=y -E separator=, -E occurrence=a > "{file_name}"')
os.remove(file_name +'.pcap')
if __name__ == "__main__":
if not os.path.exists(r'c:/DataCapture'):
os.mkdir(r'c:\DataCapture')
p1 = mp.Process(target=capturing_loop)
p1.start()
app.run_server(debug=False)
home.py
import dash
from dash import dcc, html, Input, Output, State, callback
import pandas as pd
import subprocess, io
dash.register_page(__name__, path='/')
nics_list = subprocess.check_output(f'cmd /c D:/OneDrive/NetPredict/dumpcap/tshark -D')
nics_list = nics_list.decode()
nics_list = nics_list.replace('. ', ',').replace('} (', '},').replace(')\r','')
df = pd.read_csv(io.StringIO(nics_list), sep=",", header=None)
df.fillna(' (Loopback Link, DO NOT select)', inplace=True)
df[2] = '"' + df[2] + '"'
layout = html.Div([
html.H1(children='Welcome to the Network Traffic Monitoring Dashboard', style={
'text-align': 'center'}),
html.Div([
html.Label(['Please select a graph to start capturing Traffic, then move to page 2:'], style={
'font-weight': 'bold'}),
dcc.RadioItems(
id='nic_selection',
options=df[2], persistence=True),
html.Button(id='submit_btn', n_clicks=0, children='Submit'),
])])
@callback(
Output('store', 'data'),
[State('nic_selection', 'value')],
[Input('submit_btn', 'n_clicks')]
)
def update_output(value, n_clicks):
print('Value is', value)
if n_clicks is None:
raise PreventUpdate
store = value
return store
The other pages code is not relevant, and very long to post
Help is highly appreciated