[PYTHON] Getting Started with Heroku, Deploying Flask App

Introduction

What is heroku?

PaaS (Platform as a Service) provided by heroku, which was founded in the United States in 2007.

PaaS is a service that allows you to publish an application without initializing the server. This allows developers to focus on application development.

image.png

What are the benefits of using heroku?

Rich in extensions

Scale-out is one of the methods to increase the processing power of the system and enhance the computer environment by increasing the number of servers that make up the system. After developing and publishing the application, it can be operated smoothly. "Maintaining an optimal environment" is indispensable for this. As the number of users who use the app increases, it will be necessary to enhance the environment so that the app can run smoothly. In order to provide a stable service to many users, it is important to scale out to improve the performance of the hardware. ** Also, on heroku, you can change the scale out immediately by operating on the dashboard screen or executing a simple command. ** **

Easy to scale out

Scale-out is one of the methods to increase the processing power of the system and enhance the computer environment by increasing the number of servers that make up the system. After developing and publishing the application, it can be operated smoothly. "Maintaining an optimal environment" is indispensable for this. As the number of users who use the app increases, it will be necessary to enhance the environment so that the app can run smoothly. In order to provide a stable service to many users, it is important to scale out to improve the performance of the hardware. ** Also, on heroku, you can change the scale out immediately by operating on the dashboard screen or executing a simple command. ** **

Heroku

Install Heroku command environment

brew tap heroku/brew && brew install heroku

Create an account on Heroku and make a note of your login email address and password. Type the login command in Terminal.

heroku login

The state of executing the above command is as follows. ezgif-4-139b33230118.gif

Deploy Flask app on Heroku

Create the required resources

Create a directory to put the project files and change to the directory. Execute the following command in the terminal.

mkdir flaskonheroku
cd flaskonheroku

Create the required files.

touch app.py Procfile

Edit app.py as follows.

app.py


# -*- coding: utf-8 -*-
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'hello, world'

if __name__ == '__main__':
    app.run()

Edit the Procfile as follows.

web: gunicorn app:app --log-file=-

Install the required libraries this time with pip install. Do the following in your terminal:

pip3 install Flask
pip3 install gunicorn

Create the files needed for deployment

Create requirements.txt.

pip freeze > requirements.txt

Edit requirements.txt as follows.

Flask==1.1.2
click==7.1.2
gunicorn==20.0.4
Jinja2==2.11.2

Check if it works in the local environment

$ 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://127.0.0.1:5000/ (Press CTRL+C to quit)

It is OK if you access the address and the following screen appears. スクリーンショット 2020-10-25 17.30.19.png

Preparing the git repository

Execute the following command in the terminal to initialize it as a git repository

git init
git add .
git commit -m "first commit"

Creating a Heroku app

Create an app on Heroku with the heroku create command.

$ heroku create
Creating app... done, ⬢ secret-refuge-44790
https://secret-refuge-●●●●●●●●.herokuapp.com/ | https://git.heroku.com/secret-refuge-●●●●●●●●.git

You can also specify the app name with heroku create app name. If you do not specify an app name, the app name will be assigned automatically. (Can be changed later)

Deploy the app to Heroku

A remote repository has been created, so push it.

$ git remote
heroku
git push heroku master

The state of executing the command and deploying is as follows. ezgif-4-9c7ffb1c8e6f.gif

Check the app

When you execute the following command, the browser will automatically start and the deployed Flask application will start.

heroku open

The state of the browser screen where the following screen actually started up. スクリーンショット 2020-10-25 18.00.04.png

Articles that I used as a reference

What is heroku? Introducing the basics and features that even beginners can understand in 5 minutes | Samurai Blog --Site for beginners in programmingHeroku deploy on mac --QiitaActive engineers explain how to deploy Python applications using heroku [for beginners] | TechAcademy Magazine -Introduction and initial settings of Heroku CLIHeroku operation CLI --Qiita

Recommended Posts

Getting Started with Heroku, Deploying Flask App
Run the app with Flask + Heroku
Easy web app with Python + Flask + Heroku
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Numpy
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Getting Started with Go Assembly
Touch Flask + run with Heroku
Getting Started with PKI with Golang ―― 4
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
How to deploy a web app made with Flask to Heroku
Getting Started with Flask # 2: Displaying Data Frames in Style Sheets
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
Creating a simple app with flask
Deploy Flask app on heroku (bitterly)
Deploy the Flask app on Heroku
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Deploy the Flask app on heroku
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with CPU Steal Time
Azure table storage with PTVS Flask app
Getting Started with python3 # 1 Learn Basic Knowledge
Deploy flask app with mod_wsgi (using pipenv)
I tried linebot with flask (anaconda) + heroku
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Launch Flask application with Docker on Heroku
Getting Started with TDD with Cyber-dojo at MobPro
A story about deploying a Twitter-linked app created using Flask + gunicorn on Heroku
Getting started with Python with 100 knocks on language processing
How to upload with Heroku, Flask, Python, Git (4)