Heroku x Flask (Python) is the fastest personally when making a trivial api, but I'll summarize it because it always takes time to mystery by default.
First, make an app on Heroku
mkdir "Folder name"
cd "Folder name"
git init
heroku create -a "app name"
Three files are required in the created folder
First, from requirements.txt (ver after == is appropriate)
requirements.txt
Flask==1.1.0
gunicorn==19.9.0
psycopg2==2.7.6.1
psycopg2 is a library for tampering with postgresql, so it is unnecessary when you do not mess with postgresql Others are required if you use Flask
Next is Procfile
Procfile
web: gunicorn hello:app --log-file -
最後はhello.py
hello.py
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------
#
# hello.py
#
# Mar/04/2020
# -------------------------------------------------------------------
import os
from flask import Flask,request
app = Flask(__name__)
@app.route("/")
def hello():
str_out = ""
str_out += "<h2>Hello from Python!</h2>"
str_out += "<blockquote>"
str_out += "Hello<p />"
str_out += "</blockquote>"
str_out += "Mar/04/2020 14:40<br />"
#
return str_out
#Only needed when testing locally, not on the server
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
# -------------------------------------------------------------------
git add .
git commit -m "First Commit"
git push heroku master
Originally you have to set python as buildpack, but it seems that heroku will recognize Python and download the latest version of python when you push it.
If it works, you should see "Hello From Python" with the command below.
heroku open
the end
Recommended Posts