I have two applications working together (Flask and Dash). The Flask application receives and processes data, calls the function that creates the Dash application, and sends a request with the id_session used to access the correct user data. The Dash application receives the request and updates the Dash layout. This integration works correctly in a localhost environment, but on PythonAnywhere, I am having issues with the correct updating of the graphs in the Dash layout. From what I can identify, it seems that the update signal reception in the callback is not working correctly. The request is received, and the data is accessed correctly within the “update-data” function, but the callback does not respond correctly every time. Sometimes the graphs are updated with incorrect data.
FLASK CODE:
flask_app = Flask(__name__)
flask_app.secret_key = 'supersecretkey'
import dash_app
dash_app.create_dash_app(flask_app)
@flask_app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
session_id = str(uuid.uuid4())
session['session_id'] = session_id
upload_folder = os.path.join('uploads', session_id)
os.makedirs(upload_folder, exist_ok=True)
database_file = request.files['database_file']
data = pd.read_csv(database_file)
# Data Manipulation Code
data_path = os.path.join(upload_folder, 'temp_data.csv')
data.to_csv(data_path, index=False)
requests.post('http://my_domain/update-data', data={'session_id': session_id})
return redirect("http://my_domain/dashapp/")
return render_template('index.html')
DASH CODE:
def create_dash_app(flask_app):
FONT_AWESOME = ["https://use.fontawesome.com/releases/v5.10.2/css/all.css"]
dash_app = Dash(server=flask_app, url_base_pathname='/dashapp/', external_stylesheets=FONT_AWESOME)
server = dash_app.server
dash_app.layout = dbc.Container(children=[
dcc.Store(id='data-update-signal', data=0),
... Rest of the Layout Code...
])
global data
data = pd.DataFrame({})
@server.route('/update-data', methods=['POST'])
def update_data():
session_id = request.form.get('session_id')
if not session_id:
return "Session ID missing", 400
download_folder = os.path.join('uploads', session_id)
global data
data_path = os.path.join(download_folder, 'temp_data.csv')
data = pd.read_csv(data_path)
current_signal = dash_app.layout.children[0].data
new_signal = current_signal + 1
dash_app.layout.children[0].data = new_signal
return "Data updated", 200
@dash_app.callback(
Output('graph1', 'figure'),
Output('graph3', 'figure'),
Output('graph4', 'figure'),
Input(ThemeSwitchAIO.ids.switch("theme"), "value"),
[Input('data-update-signal', 'data')]
)
def card1(toggle, data_update_signal):
template_theme1 = "flatly"
template_theme2 = "darkly"
template = template_theme1 if toggle else template_theme2
fig1 = go.Figure()
... Rest of the Fig Code ...
fig3 = go.Figure()
... Rest of the Fig Code ...
fig4 = go.Figure()
... Rest of the Fig Code ...
return fig1, fig3, fig4
return dash_app