I have the following code that reads files and extract the input for the dropdown option list, then there is the layout and callback to plot some graphs, the options definition is set before the layout so the layout can find the variables needed. However the list is supposed to be updating every 10 seconds, but because I have created the options list outside the layout, it does not get updated, and any new information is not coming to the list.
The question is that what can I do to have it updated along with the update of the graph? I moved the lists creation inside the callback but it fails as the layout will not find these options. I also duplicated the same and put another copy inside the callback, but it did not help, the list is still based on the initial files reading.
files = glob.glob("C:/DataCapture/*.txt")
modified_files = list()
current_time = time.time()
for txt_file in files:
time_delta = current_time - os.path.getmtime(txt_file)
if time_delta < 90:
modified_files.append(txt_file)
dfs = [ ]
for file_name in modified_files:
df1 = pd.read_csv(file_name, on_bad_lines='skip').dropna()
df1.rename(
columns={'_ws.col.Time': 'Time', 'ip.src': 'Source', 'ip.dst': 'Destination', 'ip.proto': 'Protocol'},
inplace=True)
dfs.append(df1)
df = pd.concat(dfs)
df['Protocol'] = df['Protocol'].replace({1: 'ICMP', 2: 'IGMP', 6: 'TCP', 17: 'UDP', 80: 'Http', 443: 'Https'})
list_src_IP = list(df['Source'].unique())
list_dst_IP = list(df['Destination'].unique())
list_prt = list(df['Protocol'].unique())
layout = html.Div([
html.H1('You can enter your filtering options below', className='text-center fs-1 text-primary'),
dcc.Interval(
id = 'Interval',
interval = 5 *2000,
n_intervals = 0
),
dcc.Dropdown(
id='src_IP_options',
placeholder = 'Select The Source IP or leave empty',
options = list_src_IP,
style={"width": "60%"}),
dcc.Dropdown(
id='dst_IP_options',
placeholder = 'Select The Destination IP or leave empty',
options = list_dst_IP,
style={"width": "60%"}),
dcc.Dropdown(
id='prt_options',
placeholder = 'Select The Port or leave empty',
options = list_prt,
style={"width": "60%"}
),
html.Br(),
html.Label(['']),
html.Button(id='submit_btn', n_clicks=0, children='Submit'),
html.Div(id='graph_container'),
])
@callback(
Output('graph_container', 'children'),
[Input('submit_btn', 'n_clicks'),
Input('Interval', 'n_intervals'),
State('src_IP_options', 'value'),
State('dst_IP_options', 'value'),
State('prt_options', 'value'),
])
def Select_options(n_clicks, intervals, value1, value2, value3):
files = glob.glob("C:/DataCapture/*.txt")
modified_files = list()
current_time = time.time()
for txt_file in files:
time_delta = current_time - os.path.getmtime(txt_file)
if time_delta < 90:
modified_files.append(txt_file)
dfs = []
for file_name in modified_files:
df1 = pd.read_csv(file_name, on_bad_lines='skip').dropna()
df1.rename(
columns={'_ws.col.Time': 'Time', 'ip.src': 'Source', 'ip.dst': 'Destination', 'ip.proto': 'Protocol'},
inplace=True)
dfs.append(df1)
df = pd.concat(dfs)
list_src_IP = list(df['Source'].unique())
list_dst_IP = list(df['Destination'].unique())
list_prt = list(df['Protocol'].unique())