See this for style: https://github.com/jimmybow/Flask_template_auth_with_Dash
Inspired by https://hackersandslackers.com/plotly-dash-with-flask/ https://mattupstate.com/blog/how-i-structure-my-flask-applications/
Any Python file is a module, its name being the file's base name/module's name property without the .py extension. A package is a collection of Python modules, i.e., a package is a directory of Python modules containing an additional init.py file. The init.py distinguishes a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own init.py file.
conda install -c conda-forge poetry
see Poetry CLI https://python-poetry.org/docs/cli/
poetry new [package-name] poetry new devdashapp Or run poetry init
DONT USE DASH IN FILE NAMES for py modules
poetry install
poetry add flask poetry add dash poetry add requests
Alternatively run poetry install after adding the dependencies into the pyproject.toml file
poetry update poetry self:update
/app ├── /application │ ├── init.py │ ├── auth.py │ ├── forms.py │ ├── models.py │ ├── routes.py │ ├── /static │ │ ├── /dist │ │ └── /src │ └── /templates ├── config.py ├── start.sh └── wsgi.py
The entirety of our app lives in the /application folder, with the creation of our app happening in init.py. The init file is where we actually create what's called the Application Factory.
The initialization of our app happens within the init_app() function following the Flask Application Factory pattern.
Create Config Class to model the environment variables FLASK_APP = environ.get("FLASK_APP") FLASK_ENV = environ.get("FLASK_ENV")
FLASK_APP=wsgi.py FLASK_ENV=development
Import Dash application using from .plotlydash.dashboard import init_dashboard app = init_dashboard(app)
importing a file called dashboard.py from a directory in our Flask app called /plotlydash. Inside dashboard.py is a single function which contains the entirety of a Plotly Dash app in itself:
We pass our top-level Flask app into Dash as server, hence dash_app = Dash(server=server). This effectively spins up a Dash instance using our Flask app at its core, as opposed to its own!
Note Dash has full control over anything we build beneath the hierarchy of our prefix
Because we create our own app i.e. app = Flask we cannot use the same structure used by the official Dash documentatiom i.e. app = dash.Dash
poetry run [script-name] executes a script defined in the [tool.poetry.scripts] section of pyproject.toml.
gunicorn --bind=0.0.0.0:5000 --timeout 600 wsgi:app or poetry run python wsgi.py
$ poetry run uvicorn main:app --reload --host 0.0.0.0 --port 3000
See Uvicorn settings https://www.uvicorn.org/settings/ See Gunicorn settings - https://docs.gunicorn.org/en/develop/configure.html
poetry build: Builds the source and wheels archives. poetry publish: Publishes the output of the previous build to the project's external repository (likely PyPi).
Add a requirements.txt file in the root of your project that specifies your direct dependencies. App Service then installs those dependencies automatically when you deploy your project. The requirements.txt file must be in the project root for dependencies to be installed. Otherwise, the build process reports the error: "Could not find setup.py or requirements.txt; Not running pip install." If you encounter this error, check the location of your requirements file.
App Service automatically defines an environment variable named WEBSITE_HOSTNAME with the web app's URL, such as msdocs-hello-world.azurewebsites.net. It also defines WEBSITE_SITE_NAME with the name of your app, such as msdocs-hello-world.
poetry export -f requirements.txt --output requirements.txt --without-hashes
For Flask, App Service looks for a file named application.py or app.py and starts Gunicorn as follows:
gunicorn --bind=0.0.0.0 --timeout 600 application:app
gunicorn --bind=0.0.0.0 --timeout 600 app:app
gunicorn --bind=0.0.0.0:5000 --timeout 600 wsgi:app
Add this to Azure startup.sh file and run the Azure CLI command or add it via Configuration settings in https://docs.microsoft.com/en-gb/azure/app-service/configure-common#configure-general-settings
az webapp config set --resource-group --name --startup-file "" az webapp config set --resource-group PortfolioApp --name devdashapp --startup-file startup.sh
git remote add origin https://github.com/emiliod90/flask_dash_app.git git remote set-url --add --push origin https://github.com/emiliod90/flask_dash_app.git
git remote set-url --add --push origin https://emydesouza@dev.azure.com/emydesouza/Flask%20Dash%20App/_git/Flask%20Dash%20App
git branch -M main git push -u origin main