Question

How to Deploy Flask App to App Platform

Posted June 28, 2021 164 views
PythonDigitalOcean App PlatformFlask

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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

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