How to upload with Heroku, Flask, Python, Git (Part 1)

I learned about Heroku, Flask, and Python with Progate, dot install, Qiita, etc., so I will summarize them.

(1) Create a working directory (2) Set up a virtual environment (3) Install the necessary framework and WEB server (4) Create Flask file in Python (5) Try running the Flask file (6) Deploy to Heroku ① Register an account with Heroku ② Install Heroku with Homebrew ③ Prepare Git ④ Create an app on Heroku ⑤ Link the local app with the Heroku app ⑥ Create the files required for deployment to Heroku ⑦ Deploy to Heroku (7) Error handling ① Heroku ps: scale web = 1 is normal ② Heroku ps: scale web = 1 is not normal (8) Update the file

(1) Create a working directory

Since I always work on the desktop, I set up a directory on the desktop this time as well. First, start the Mac terminal and type cd desktop, then mkdir myproject, which will create a directory my-project on your desktop.

(2) Set up a virtual environment

Then in the terminal, do cd myproject and move into the directory my-project. Set up your development environment locally using Python's venv, a feature that provides a virtual environment.

python3 -m venv .

Then enter the following to activate the virtual environment.

source bin/activate

By the way, if you want to disable the virtual environment, enter as follows.

deactivate

(3) Install the necessary framework and WEB server

Next, install the framework.

pip install flask

Also, install the WEB server.

pip install gunicorn

(4) Create Flask file in Python

In the enabled virtual environment, enter the following to create an executable file in my-project. This time I created a file called hello.py.

touch hello.py

Next, open hello.py and enter the following. In addition, app.run () becomes app.run (debug = false) as it is, and the debug mode is off. In the production environment, setting debug = true is dangerous, so I left it as the default. For details, refer to the following. https://www.subarunari.com/entry/2018/03/10/いまさらながら_Flask_についてまとめる_〜Debugger〜

hello.py


# -*- coding: utf-8 -*-
#Required even if commented out when using Japanese

#Import required libraries such as flask
from flask import Flask

#Instantiate your name as app
app = Flask(__name__)

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

#app.run()Is set to false by default
if __name__ == '__main__':
    app.run()

(5) Try running the Flask file

Operate in the terminal again. Try in your local environment to see if the created hello.py works. Enter the following in the terminal.

python3 hello.py flask run

Then, the following is displayed.

* Serving Flask app "hello" (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)
127.0.0.1 - - 

If you access http://127.0.0.1:5000/ in the above part with a browser, the following will be displayed. Screenshot 2020-02-26 22.38.02.png

It was confirmed that it works normally in the local environment. Press control + c to return to terminal input mode.

How to upload with Heroku, Flask, Python, Git (Part 2)

Recommended Posts

How to upload with Heroku, Flask, Python, Git (Part 3)
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
How to upload with Heroku, Flask, Python, Git (4)
How to measure execution time with Python Part 1
How to measure execution time with Python Part 2
How to use python interactive mode with git bash
Python: How to use async with
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to deploy a web app made with Flask to Heroku
How to upload files to Cloud Storage with Firebase's python SDK
How to work with BigQuery in Python
How to do portmanteau test with python
How to display python Japanese with lolipop
Easy web app with Python + Flask + Heroku
[Part1] Scraping with Python → Organize to csv!
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to utilize Python with Jw_cad (Part 1 What is external transformation)
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Upload images to Google Drive with Python
How to install python3 with docker centos
How to crop the lower right part of the image with Python OpenCV
How to start Python (Flask) when EC2 starts
How to read a CSV file with Python 2/3
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
How to specify attributes with Mock of python
How to use tkinter with python in pyenv
Upload files to Google Drive with Lambda (Python)
[Python] How to handle Japanese characters with openCV
[Python] How to compare datetime with timezone added
Flow to complete Slack authentication with Flask (Python)
How to install Python
How to install python
Programming with Python Flask
How to convert / restore a string with [] in python
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Explain in detail how to make sounds with python
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
How to make a shooting game with toio (Part 1)
How to convert JSON file to CSV file with Python Pandas
[Python] How to deal with pandas read_html read error
How to get mouse wheel verdict with Python curses
[Python] How to rewrite the table style with python-pptx [python-pptx]
[Python] How to create a 2D histogram with Matplotlib
How to switch to smartphone mode with Python + Selenium + Chrome