By jonleff
Hi,
I am new to web app deployment, and am attempting to deploy a Flask web app to the DigitalOcean App Platform — but am having a hard time finding the right documentation.
I have a working Flask app on my local machine in the form of a Python package, where I can run the main.py file to launch the app. The code in the main.py file looks like this:
from website import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
The main.py file references code in the __init__.py file, which looks like this:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
db = SQLAlchemy()
DB_NAME = 'database.db'
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'XXXXXXXXXXXXXX'
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
db.init_app(app)
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
from .models import User, Security
create_database(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
return(User.query.get(int(id)))
return(app)
def create_database(app):
if not path.exists('website/' + DB_NAME):
db.create_all(app=app)
print('Created Database!')
The rest of the code (templates, models, etc.) is kept throughout the package in other folders or files. My file structure looks like this:
web-app
- gunicorn_config.py
- requirements.txt
- main.py
- website
- __init__.py
- auth.py (authorization routes)
- views.py (regular routes)
- models.py (defines SQL classes)
- database.db (SQL database)
- static
- index.js (holds JavaScript functions)
- styles
- styles.css (holds CSS)
- templates
- ALL SITE TEMPLATES (.html)
I have successfully followed the instructions in this link (https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-app-using-gunicorn-to-app-platform) to upload my app to a Github repository, but am unsure what to specify for the build commands and run commands on the App Platform setup, as well as if this structure will work at all on the App Platform.
Thank you in advance for any help.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello @jonleff
The flask app with this structure should work on the App Platform. There is no need to add the build command however you can add the following run command to deploy the app:
gunicorn --worker-tmp-dir /dev/shm --config gunicorn_config.py --bind 0.0.0.0:8080 main:app
If you are seeing any issue with deploying the app with above run command then please contact support with exact error message and we will be able to assist you.
Thank you!
Best, Dikshith
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.