[PYTHON] Re: Heroku life begin with Flask from zero - Environment and Hello world -

Introduction

At the club activity hackathon, I made a simple API by putting Python Flask on Heroku for the first time, so I would like to write it.

(Added on 2016/10/28) The API code for running PhantomJS with Selenium and scraping with Beautiful soup and its explanation are in the second part. Re: Life in Heroku starting from scratch with Flask ~ Selenium & PhantomJS & Beautifulsoup ~ At It's the second part until you deploy the program to Heroku Re: Life in Heroku starting from scratch with Flask ~ PhantomJS to Heroku ~ Since it is written in, please have a look.

What was used

Python environment

final goals

Put what you made with Python Flask on Heroku and make an API that returns json when you get it By the way, Chrome measures

This goal

Build a Python environment and raise Flask to Heroku to do Hello World

Next goal

I scraped a web page with Python, processed it into json format and spit it out, and pushed it to Heroku. From Heroku to the place where you get json

Preparation

Python environment

First of all, while looking at the article of the person who summarizes in an easy-to-understand manner on Qiita etc. Let's put ** pyenv ** and ** pyenv-virtualenv ** in homebrew etc.! reference:

Here, instead of pyenv-virtualenv, try to put a normal virtualenv, If you put it in, it can be troublesome, so be sure to put in the virtualenv of the pyenv plugin when you put it in. Reference: http://qiita.com/who_you_me/items/09f572c842b1c3fea015

After installing pyenv and pyenv-virtualenv, move to the folder you plan to upload to Heroku and prepare your environment.

$ mkdir hello-flask
#Create a project folder

$ cd hello-flask
#Move to folder


$ pyenv virtualenv 3.5.1 v3.5.1-flask 
# v3.5.1-Create an environment named flask

$ pyenv versions
# v3.5.1-Confirm that flask is added


$ pyenv shell v3.5.1-flask
#Apply this environment below the working folder

$ pyenv rehash
#Rehash just in case

$ pyenv versions
# v3.5.1-Confirm that it is moved to flask

By doing this, even if you put various packages in this folder, other environments will not be polluted. If you apply Flask to other folders, you can use the same environment! Yay!

Until Hello World in Flask

Flask Put the Python framework used this time

$ pip install flask

Gunicorn Also include the web server used this time

$ pip install gunicorn

Create Flask files

$ touch hello.py Procfile
#Create a file to write the flask file and settings

hello.py


# -*- coding: utf-8 -*-
#Absolutely necessary when using Japanese

#Import required libraries such as flask
import os
from flask import Flask

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

@app.route('/')
def index():
    return 'Hello World!'
    
#Determine if you hit with bash or put in with import
if __name__ == '__main__':
    app.run()

Procfile

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

If you prepare so far, the rest will work if you start it with Python! Convenient!

$ python hello.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

It will be like this, so be selfish as you are told If you access http://127.0.0.1:5000/ ... hello.png

You can exit by pressing control and C at the same time as written.

Departure to Heroku

Register with Heroku

https://www.heroku.com/ Create an account from sign up. It seems that the completion confirmation email will be returned within 15 minutes, so wait leisurely

If you can register a new one and see the dashboard, you are ready to go.

Actually raise to Heroku

I will finally give what I made to Heroku!

Create a file that exports the library you are using

$ pip freeze > requirements.txt

Heroku looks at this file and prepares libraries etc. If you upload this file to Heroku without updating it, you will get an error.

Heroku and git

Apply git and commit

$ git init
#Set git in the folder

$ git add .
#Add everything


$ git commit -m "helloworld"
#Commit with helloworld as the commit name

I'll give it to Heroku from here

$ heroku login
#Log in to heroku Enter your registered email address and pass

$ heroku create hello-flask
#Create a new project for heroku

If you create heroku on the web instead of on heroku, it will even set up a remote repository for git. However, this heroku project name, including other users, ** cannot use the same name in heroku **, so if you can't create it, you may add some numbers after it. If you don't write anything after heroku create, heroku will automatically give it a suitable name for you.

$ git push heroku master
#Push to heroku remote repository by making it heroku master
#With this one command, you can put libraries on heroku and do various things.

$ heroku open
#This command will open it in your browser without copying the URL

If you open the URL of heroku with heroku open, you can see Hello World.

At this point, if you can make this returned content text json, it should be a so-called json API! Yay!

If you get an error

When I see what I uploaded to heroku and I get an error, $ heroku logs Let's take a look at When you look at logs, you may not want to see it because there are many lines, but look for the file name you created from it

File "/app/hello.py", line 10

If you write something like, you know that that part or the part related to it is wrong.

Error check sheet

If you are using Japanese in a python file

At the top of the file

# -*- coding: utf-8 -*-

Did you forget to write? Required if you use Japanese even in the comment out. If you use Japanese in a place other than commented out, you may need to add u at the beginning.

hello.py


#When outputting in Japanese
def index():
    return u'Hello World! !!'

If it fails when you push to heroku

$ pip freeze > requirements.txt

Have you forgotten to create requirements.txt? Also, if you can push but get an error, after executing this command once, Check if you are using the newly added library in the file

reference

Afterword

If you have any improvements or mistakes, please let us know in the comments.

Twitter:@ymgn_ll

Recommended Posts

Re: Heroku life begin with Flask from zero - Environment and Hello world -
Re: Life in Heroku starting from scratch with Flask ~ PhantomJS to Heroku ~
Re: Life in Heroku starting from scratch with Flask ~ Selenium & PhantomJS & Beautifulsoup ~
Hello world with flask
Hello World with Flask + Hamlish
Hello World and face detection with opencv-python 4.2
Flask tutorial (from installation to hello world)
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Hello World and face detection with OpenCV 4.3 + Python
Hello World with gRPC / go in Docker environment
First python ① Environment construction with pythonbrew & Hello World !!
hello world with ctypes
Hello, World with Docker
[Tweepy] Re: Twitter Bot development life starting from zero # 1 [python]
From setting up a Rust environment to running Hello World
Re: Competitive Programming Life Starting from Zero Chapter 1.3 "Sedo Tea"
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
Touch Flask + run with Heroku
Draw hello world with mod_wsgi
Until hello world with zappa
Python starting with Hello world!
From building a Python environment for inexperienced people to Hello world
Re: Competitive Programming Life Starting from Zero Chapter 1.2 "Python of Tears"