How can I shut the app, when another software is opens or is opened and I have open one tab

hi

I have an app, and I whant that when tab-6 is opened and another program starts, the app should close …

I can’t manage to make it with chatgpt.

How could I do this?

# Nombre del proceso asociado a PokerStars
nombre_del_proceso_pokerstars = 'PokerStars.exe'

# Diseño del sidebar
sidebar = html.Div(
    [
        html.H2("Poker Utils", className="display-4", style=SIDEBAR_STYLE),
        html.Hr(),
        html.P("The edge", className="lead", style=SIDEBAR_STYLE),
        dbc.Nav(
            [
                dbc.NavLink("Dropdown", id='navlink-tab1', href="/", active="exact"),
                dbc.NavLink("All players report", id='navlink-tab2', href="/page-2", active="exact"),
                dbc.NavLink("1 player report", id='navlink-tab3', href="/page-6", active="exact"),
                dbc.NavLink("Utils", id='navlink-tab4', href="/page-1", active="exact"),
                dbc.NavLink("EV winnings", id='navlink-tab5', href="/page-3", active="exact"),
                dbc.NavLink("Ranges", id='navlink-tab6', href="/page-7", active="exact"),
                # dbc.NavLink("Solved Preflop", id='navlink-tab7', href="/page-4", active="exact"),
                dbc.NavLink("Contact", id='navlink-tab8', href="/page-5", active="exact"),
            ],
            vertical=True,
            pills=True,
            style=SIDEBAR_STYLE,
        ),
    ],
    style=SIDEBAR_STYLE,
)

# Diseño del contenido principal
content = html.Div(id='page-content', style=SIDEBAR_STYLE)

# Diseño de la aplicación completa
app.layout = html.Div([dcc.Location(id='url'), sidebar, content])

# Callback para ocultar la pestaña específica si PokerStars está en ejecución y cerrar la aplicación si la pestaña ya está abierta
@app.callback(
    [Output('navlink-tab6', 'hidden'),
     Output('url', 'pathname')],
    Input('url', 'pathname'),
    prevent_initial_call=True
)
def update_navlink_tab6(pathname):
    # Obtén el estado de PokerStars
    pokerstars_abierto = check_pokerstars()

    # Redirecciona a la página de cierre si "Ranges" está abierta y PokerStars se inicia
    if pathname == '/page-7' and pokerstars_abierto:
        sys.modules[__name__].__dict__['_exit_code'] = 1
        return True, '/shutdown'
    
    # Oculta la pestaña "Ranges" si PokerStars está en ejecución
    hidden = pokerstars_abierto
    new_pathname = pathname
    if pathname == '/page-7' and pokerstars_abierto:
        # Redirecciona a la página de inicio si "Ranges" está abierta y PokerStars se inicia
        new_pathname = '/'
    return hidden, new_pathname

# Página de cierre
shutdown_page = html.Div([
    html.H2("La aplicación se cerrará debido a la ejecución de PokerStars."),
    html.P("Por favor, cierre la pestaña del navegador."),
])

# Función para verificar si PokerStars está abierto
def check_pokerstars():
    if platform.system() == 'Windows':
        # En Windows, verifica si el proceso PokerStars.exe está en ejecución
        for process in psutil.process_iter(['pid', 'name']):
            if nombre_del_proceso_pokerstars.lower() in process.info['name'].lower():
                return True
    else:
        # Puedes ajustar la lógica para otros sistemas operativos si es necesario
        return False

Hello @topotaman,

This is actually quite difficult to achieve as it border’s on the line of what you should do as a developer.

I guess potentially you could just stop responding on one page and only keep the most recent session from an IP, but this could potentially cause some issues.

I’m just brainstorming here. :stuck_out_tongue:

dont really know how to make this work … this is what if tried … but nothing works when the app is opened and the tab selected, and I run the other software …

this is what I have of code:


def programa_esta_abierto(nombre_programa):
    for proceso in psutil.process_iter(['pid', 'name']):
        if nombre_programa.lower() in proceso.info['name'].lower():
            return True
    return False

# Ejemplo de uso
nombre_programa = 'PokerStars.exe'  # Reemplaza con el nombre del programa que deseas verificar
if programa_esta_abierto(nombre_programa):
    print(f"El programa {nombre_programa} está abierto.")
   
else:
    print(f"El programa {nombre_programa} no está abierto.")
  
##################################################################################

# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "width": "10rem",
    "padding": "2rem 1rem",
    "background-color": "#000000",
    "height": 'auto',
}

# the styles for the main content position it to the right of the sidebar and
# add some padding.
CONTENT_STYLE = {
    "position":"flex",
    "margin-left": "10rem",
    "margin-right": "0rem",
    "margin-bottom":"0rem",
    "padding": "2rem 1rem",
    "background-color": "#000000",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "height": '100%',
    "width":"auto",
}



# Nombre del proceso asociado a PokerStars
nombre_del_proceso_pokerstars = 'PokerStars.exe'




# Diseño del sidebar
sidebar = html.Div(
    [
        html.H2("Poker Utils", className="display-4", style=SIDEBAR_STYLE),
        html.Hr(),
        html.P("The edge", className="lead", style=SIDEBAR_STYLE),
        dbc.Nav(
            [
                dbc.NavLink("Dropdown", id='navlink-tab1', href="/", active="exact"),
                dbc.NavLink("All players report", id='navlink-tab2', href="/page-2", active="exact"),
                dbc.NavLink("1 player report", id='navlink-tab3', href="/page-6", active="exact"),
                dbc.NavLink("Utils", id='navlink-tab4', href="/page-1", active="exact"),
                dbc.NavLink("EV winnings", id='navlink-tab5', href="/page-3", active="exact"),
                dbc.NavLink("Ranges", id='navlink-tab6', href="/page-7", active="exact"),
                # dbc.NavLink("Solved Preflop", id='navlink-tab7', href="/page-4", active="exact"),
                dbc.NavLink("Contact", id='navlink-tab8', href="/page-5", active="exact"),
            ],
            vertical=True,
            pills=True,
            style=SIDEBAR_STYLE,
        ),
    ],
    style=SIDEBAR_STYLE,
)

# Callback para ocultar la pestaña específica si PokerStars está en ejecución
@app.callback(
    [Output('navlink-tab6', 'hidden'),
     Output('url', 'pathname')],
    Input('url', 'pathname'),
    prevent_initial_call=False
)
def update_navlink_tab2(pathname):
    # Obtén el estado de PokerStars
    pokerstars_abierto = check_pokerstars()

    # Oculta la pestaña "All players report" si PokerStars está en ejecución
    hidden = pokerstars_abierto
    new_pathname = pathname
    if pathname == '/page-7' and pokerstars_abierto:
        # Redirecciona a la página de inicio si "All players report" está abierta y PokerStars se inicia
        new_pathname = '/'
        sys.exit()
        
    return hidden, new_pathname

# Función para verificar si PokerStars está abierto
def check_pokerstars():
    if platform.system() == 'Windows':
        # En Windows, verifica si el proceso PokerStars.exe está en ejecución
        for process in psutil.process_iter(['pid', 'name']):
            if nombre_del_proceso_pokerstars.lower() in process.info['name'].lower():
                
                return True
    else:
        # Puedes ajustar la lógica para otros sistemas operativos si es necesario
        return False

# Diseño del contenido principal
content = html.Div(id='page-content', style=SIDEBAR_STYLE)

# Diseño de la aplicación completa
app.layout = html.Div([dcc.Location(id='url'), sidebar, content])


and with this:


    elif pathname == "/page-7":
        return dbc.Container(
    [
        dcc.Store(id="store"),
        html.H1("Ranges",style={
    "color":"#f0f8ff",
    "background-color": "#000000",
},),
        html.Hr(),
        dbc.Tabs(
            [
################################################### ranges

                dbc.Tab(label="Spins ranges",tab_id="spins_ranges",style={
                        "color":"#f0f8ff",
                        "background-color": "#000000",
                        "opacity": 0.0 if check_pokerstars() == True else 1.0
                    }, active_label_style={"color":"#f0f8ff","background-color": "#000000"},children=[
                    dbc.Container(
                    [
                        html.H1("Spin Ranges"),
                        html.Hr(),
                        dbc.Row(
                            html.Div(children=[

                                html.Iframe(width="100%", height="800", src="/static/Ranges white viewer/spins.html", style={"opacity": 0.0 if check_pokerstars() == True else 1.0}),
                                
                            ],),
                            align="center", 
                        ),
                        
                    ],
                    fluid=True,
                    )]),

The opacity trick … doesn’t seem to work … I don’t see something happening …

what else could I try?

Is it supposed to open a new browser tab or an application tab?

You could always kill the tab upon click of a specific tab via a clientside callback.

Hey,
maybe you could try wrapping the tabs in a div and hiding the div based on an Interval. Just an idea.


from dash import Input, Output, State,  dcc, html, Dash
import psutil
import platform
import dash_bootstrap_components as dbc


nombre_del_proceso_pokerstars = 'PokerStars.exe'


app = Dash(__name__)


app.layout = html.Div([
    dcc.Interval(interval=5000, id = 'pokerstars_checker'), 
    html.Div(dbc.Tabs([dbc.Tab("Tab1"), dbc.Tab("Tab2")], id = 'tabs'), id = 'tab_div')
])


def check_pokerstars():
    if platform.system() == 'Windows':
        # En Windows, verifica si el proceso PokerStars.exe está en ejecución
        for process in psutil.process_iter(['pid', 'name']):
            
            if nombre_del_proceso_pokerstars.lower() in process.info['name'].lower():
                return True
    else:
        # Puedes ajustar la lógica para otros sistemas operativos si es necesario
        return False


@app.callback(
    Output("tab_div", "hidden"),
    Input("pokerstars_checker", "n_intervals"),
    Input("tabs", 'active_tab'),
    prevent_initial_call = True
)
def update_result(n, aT):
    if aT == 'tab-1':
    
        
        result = check_pokerstars()
        
        if result:
            return True
        else:
            return False
        
    else:
        return False
    


if __name__ == '__main__':
    app.run_server(debug=True)

Hi,
I ended up doing this (it works):


@app.callback(
    [Output('navlink-tab6', 'disabled'),
     Output('url', 'new_pathname')],
    Input('url', 'pathname'),
    prevent_initial_call=False
)
def update_navlink_tab7(pathname):
    # Obtén el estado de PokerStars
    pokerstars_abierto = check_pokerstars()
    global disableds
    new_pathname = pathname
 
    if pathname == '/page-7' and  pokerstars_abierto == True:
            #cerrar_navegador()
            sys.exit()
    else:
        if pokerstars_abierto == True:
            disableds = True
            print("0k stars is opened")
            
        else:
            disableds = False   
            print("0k, PokerStars is not opened")      
    
            
    return   disableds ,new_pathname



# Diseño del contenido principal
content = html.Div(id='page-content', style={'margin-left': '250px', 'padding': '20px'})

# Diseño de la aplicación completa
app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='output-data-upload'),
    dcc.Store(id='intermediate-value'),
    dcc.Store(id='paquito'),
    html.Br(),
    sidebar,
    content,
    dcc.Interval(id='interval-component', interval=5*1000, n_intervals=0)
])
##########################################################################################


# Callback para cerrar la aplicación si PokerStars está abierto
@app.callback(
    [Output('navlink-tab6', 'children'),
     Output('interval-component', 'interval')],
    [Input('interval-component', 'n_intervals')],
    prevent_initial_call=True
)
def close_app_if_required(n_intervals):
    pokerstars_abierto = check_pokerstars()

    print(f"n_intervals: {n_intervals}, pokerstars_abierto: {pokerstars_abierto}")

    if n_intervals > 0 and pokerstars_abierto:
        print("Cerrando la aplicación en", datetime.now())
        cerrar_navegador()
        sys.exit()
        raise PreventUpdate("La aplicación se cerró debido a PokerStars.")
        
    else:
        print("Manteniendo el enlace habilitado")
        return html.Div([html.H5("Rango")]), 5*1000  # Vuelve a habilitar el enlace y muestra el nombre original, y reinicia el intervalo


def check_pokerstars():
    pokerstars_abierto = False
    if platform.system() == 'Windows':
        for process in psutil.process_iter(['pid', 'name']):
            if nombre_del_proceso_pokerstars.lower() in process.info['name'].lower():
                pokerstars_abierto = True
                print("PokerStars abierto")
                break
    return pokerstars_abierto
1 Like