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.
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
Build a Python environment and raise Flask to Heroku to do Hello World
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
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!
Flask Put the Python framework used this time
$ pip install flask
Gunicorn Also include the web server used this time
$ pip install gunicorn
$ 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/ ...
You can exit by pressing control and C at the same time as written.
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.
I will finally give what I made to Heroku!
$ 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.
$ git init
#Set git in the folder
$ git add .
#Add everything
$ git commit -m "helloworld"
#Commit with helloworld as the commit name
$ 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!
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.
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! !!'
$ 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
If you have any improvements or mistakes, please let us know in the comments.
Twitter:@ymgn_ll
Recommended Posts