Build web application with Flask on Heroku.
Table of content
Heroku deployment
-
Create an heroku instance with a predefined name.
$heroku create sentimentx -
Create a virtual environment with the
virtualenv venvwhich will generate the information as follows.New python executable in venv/bin/python Installing setuptools, pip, wheel...done. -
Install python packages with
pipfor the virtual environment we just created.$pip install tweepy $pip install flask $pip install numpy $pip install cPickle $pip install nltk $pip install scipy -
Still, we need to have a Python dependency file
requirements.txtforHerokuto understand these required packages. As all necessary packages are installed withpip, we can collect package information withfreezeshown as follows.$pip freeze > requirements.txt -
It seems that packages are in good shape. However, there are some problems with
Herokuto installscipyandnumpyand probablyscikit-learn. In practice, it means that a application with these packages can run nicely on local computer but won’t run onHerokuremote server. The solution is to use a third party pre-build package with the following command. It is worth noting that the following pre-build package at least supportnumpy==1.8.1andscipy==0.14.0. So if you have a higher version of these two packages, you might want to consider lower version alternatives in case they don’t work withHeroku.heroku buildpacks:set https://github.com/thenovices/heroku-buildpack-scipy -
Write the
Procfilewith the following content to tellHerokuserver which python script should be activated.web: python app.py -
Deploy the web application to
herokuwith the following command. The command will first copy all files toherokuserver and deploy them afterwords. Of course, you need to have them ready already in Github e.g., all related files have been committed to Github local/remote.$git push heroku master -
To check if the submitted web services is running on
Herokuremote service, you can use the following command.$heroku ps:scale web=1 -
Or open a browser window to the web service running in
Herokuremote server with the following command.$heroku open -
Or run
Herokuweb services on local machine with the following command.$heroku local -
In case of problems that the web application did not run on
herokuserver check log with the following command.$heroku logs -
It is also nice to know some limit of the Heroku server.