[PYTHON] Deploy Flask app on heroku (bitterly)

Deploy Flask app on heroku (bitterly)

It's quite difficult.

Error encountered in $ heroku logs --tail error code=H10 desc="App crashed" error code=h14 desc="no web processes running" Such. Even if each is treated with Stackoverflow etc., it will not be solved. However, it worked well in the following way.

Create a Python virtual environment

$ virtualenv flaskapp
$ cd flaskapp
Activate a virtual environment

$ source bin/activate
It looks like this.

$ (flaskapp) $
Here you can install various things with pip without affecting the machine environment.

For example


$ (flaskapp) $ pip install requests
$ (flaskapp) $ pip install Flask

Toka Toka.

Here, gunicorn and Flask are installed.

$ (flaskapp) $ pip3 install Flask gunicorn

[Gunicorn reference]: Gunicorn is a WSGI (Web Server Gateway Interface) HTTP Server of python and is a derivative of Unicorn of Ruby. Gunicorn is a lightweight server with a very simple implementation compared to various frameworks. Reference: https://qiita.com/arata-honda/items/09e1c350b1bd8a186e9c [How to use] Install gunicorn with pip install gunicorn, Start with gunicorn hello: app.

Preparing for Heroku

Assuming you've signed in on Heroku

Heroku CLI installation

$ (flaskapp) $ wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
Log in to Heroku

$ (flaskapp) $ heroku login
Preparation file
$ echo "web: gunicorn app:app --log-file -" > Procfile

$ (flaskapp) $ pip3 freeze > requirements.txt

Click==7.0
Flask==1.1.1
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

--runtime.txt (optional)

$ (flaskapp) $ python -V | sed -n "s/Python /python-/p" > runtime.txt
from flask import Flask
import os

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))

Miso is not just app.run () here.

At this point, check if it works locally.

$ (flaskapp) $ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

$ curl http://127.0.0.1:5000
Hello World!

Deploy to heroku


$ (flaskapp) $ git init
$ (flaskapp) $ echo "*.DS_Store" > .gitignore
$ (flaskapp) $ git add .
$ (flaskapp) $ git commit -m 'first commit'
$ (flaskapp) $ heroku create
$ (flaskapp) $ git push heroku master

Operation check

Click the Open app button at the top right of the Heroku console screen

image.png

Another screen opened and Hello World was displayed safely. image.png


Postscript

If you want to add a folder to Heroku

Even if I add a static folder that is often used in Flask to the app and deploy it on Heroku, it cannot be created on heroku. The reason is that git ignores empty folders. If you put the files in the static folder and then deploy it, a static folder will be created.


Reference: [Complete version] Procedure for deploying API created with Flask to Heroku (Note) Reference: [Error: No such file or directory:'/ app / {myappname} / static'”](https://stackoverflow.com/questions/19323513/heroku-django-oserror-no-such-file-or- directory-app-myappname-static)

Recommended Posts

Deploy Flask app on heroku (bitterly)
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Deploy masonite app on Heroku 2020
(Failure) Deploy a web app made with Flask on heroku
Deploy the Django app on Heroku [Part 1]
Run the app with Flask + Heroku
Deploy your Django application on Heroku
How to deploy a Django app on heroku in just 5 minutes
Deploy flask app with mod_wsgi (using pipenv)
Deploy Django api on heroku (personal note)
[Python] Run Flask on Google App Engine
Easy web app with Python + Flask + Heroku
Getting Started with Heroku, Deploying Flask App
Launch Flask application with Docker on Heroku
Django Heroku Deploy 1
Redis on Heroku
Notes on Flask
shimehari on heroku
Django Heroku Deploy 2
A story about deploying a Twitter-linked app created using Flask + gunicorn on Heroku
Vienna with Python + Flask web app on Jenkins
A memorandum for touching python Flask on heroku
Run the flask app on Cloud9 and Apache Httpd
jsonschema validation on flask
Deploy a Django app made with PTVS on Azure
Miscellaneous notes about deploying the django app on Heroku
Deploy a web app created with Streamlit to Heroku
A memorandum of stumbling on my personal HEROKU & Python (Flask)
Sample to put Python Flask web app on Azure App Service (Web App)
How to deploy the easiest python textbook pybot on Heroku
Deploy your Go app on Google App Engine with GitHub Actions
Looking back on iOS'Healthcare App' 2019
Deploy Flask with ZEIT Now
Touch Flask + run with Heroku
[First personal development] The story of deploying the Flask app and Twitter's automatic reply bot on Heroku
Deploy django project to heroku
Deploy Flask servers on virtual machines such as Azure and AWS