I am working to deploy a Flask application (containing a multi-page Dash application) using nginx and gunicorn using Dash 2.5.1. I’ve run into a wall where the app works as expected when running on the Flask development server, but not when running gunicorn on an Amazon Linux server; I get the following error:
A folder called {pages_folder} does not exist.
If a folder for pages is not required in your application, set `pages_folder=""`
For example, `app = Dash(__name__, pages_folder="")`
where {page_folder} is the path supplied to the dash constructor:
dash_app = Dash(
server=False,
use_pages=True,
routes_pathname_prefix='/',
pages_folder="app/dash_app/pages",
title="TITLE",
)
I’ve tried many different values for the pages_folder param, from absolute paths, to relative paths, yet none seem to work.
Here is a simplified example of my project stucture:
The application is created by calling a function create_app()
in the application.py
file, which creates a Flask instance, links the Flask instance as the Dash server, and returns the Flask instance (which is used by gunicorn).
When configuring gunicorn, I’m running synchronous workers, have tried preloading the app, and I’ve messed around with all kinds of different values for chdir and pythonpath, yet nothing seems to work, and Dash continues to throw the same error, defined by this function in the Dash _configs.py file:
def pages_folder_config(name, pages_folder, use_pages):
if not use_pages:
return None
pages_folder = pages_folder.lstrip("\\").lstrip("/")
pages_folder = None if pages_folder == "" else pages_folder
pages_folder_path = None
error_msg = f"""
A folder called {pages_folder} does not exist.
If a folder for pages is not required in your application, set `pages_folder=""`
For example, `app = Dash(__name__, pages_folder="")`
"""
if pages_folder:
pages_folder_path = os.path.join(
flask.helpers.get_root_path(name), pages_folder
)
if pages_folder and not os.path.isdir(pages_folder_path):
raise Exception(error_msg)
return pages_folder_path
I’ve debugged the path for the working directory using pathlib, directly before the Dash declaration, and it lines up exactly with what I’d expect, yet running through gunicorn still doesn’t work. I suspect the issue is more related to how gunicorn forks workers, but I’d appreciate any guidance.