Submit button creates a new instance of the process

Hi, I have a multipage Dash app, the first page asks the user to select an interface, then press submit, and the other page will pick up the selection and capture the traffic from that interface, it is running nicely, however my issue is that if the user presses the submit button again, it will create a new instance of the other process, so I will have two processes running, and it will be as many presses, even if the selection is the same, what it should do is to kill the first process and start a new one, here are the codes for the two pages

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

Just refreshing the question after the weekend :slight_smile: