✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

How to avoid QFileDialog opening twice when used alongside Dash

I have files stored in subfolders inside an “Archives” folder that I am using to reload Dash visualizations that I previously created. Namely those files are plot_infos, plot1.pickle and plot2.pickle. To use those files, I am browsing to the subfolder where they are located with QFileDialog.getExistingDirectory

In the first part of my code I am using:

import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_table

import pandas as pd
import pickle

from PyQt5.QtWidgets import QFileDialog, QApplication

from dash.dependencies import Input, Output

import plotly.graph_objs as go

import os
import sys

ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

app = QApplication(sys.argv)

Archive_path = str(QFileDialog.getExistingDirectory(None, 'Select Archive', os.path.join(ROOT_DIR, 'Archives')))

with open('{}/plot_infos'.format(Archive_path), 'rb') as fp:
    infos = pickle.load(fp) 

to load the first file.

But then later, I am using:

@app.callback(
   Output('comparison-graph', 'figure'),
   [Input('period-selector', 'value'),
    Input('special-selector', 'value')])
def update_graph(period_value, special_condition):
       dff = pd.read_pickle('{0}/plot1.pickle'.format(Archive_path))
       dff2 = pd.read_pickle('{0}/plot2.pickle'.format(Archive_path))

to load the two other files.

It is working as I intended except for the fact that the QFileDialog opens twice so I have to select the folder twice otherwise the Dash visualization won’t reload.

I know that I am supposed to store the path I get from QFileDialog as a string but the code seems to invoke QFileDialog a second time when Archive_path is used in update_graph despite it being defined with Archive_path = str(QFileDialog.getExistingDirectory(None, 'Select Archive', os.path.join(ROOT_DIR, 'Archives'))).

What should I do so that I can avoid this behaviour that seems to only happen when I am using QFileDialog alongside Dash?