Deploying a flask application on Heroku (Free Hosting Server)
Pranesh Kulkarni
Step 1 : Creating Flask application :
Write a code for a flask application. Sample code is given below
import flask
import werkzeug
app = flask.Flask(__name__)
@app.route(‘/’,methods=[‘GET’])
def hello():
return “<h1> Deployed to Heroku</h1>”
if __name__==”__main__”:
app.run()
Save this file in working folder. e.g. flask_conn.py
· Step 2: Setting up git repository
Creating requirements.txt file : This file includes library requirements for your flask application. You can get it simply by command
pip freeze > requirements.txt
Then you will get a file containing requirements of all your libraries You can remove unnecessary one. Now create one more file named “Procfile” This file contains commands that we need to run while deploying our app Since we are using gunicorn server, we have to write one line in that file.
web: gunicorn — bind 0.0.0.0:$PORT flask_conn:app
You can replace “flask_conn” with your flask application file name. Now we have 3 files in working directory. Now we need to run some git commands.
git init
This command is used to initialize empty git repository.
git add .
This command is used to add all the files in directory
Note :
There’s a problem with the “git add .” command. Since we’re currently working in the root directory, “git add .” will only add files located in the root directory. But the root directory may contain many other directories with files. How can we add files from those other directories plus the files in the root directory to the staging area? Git offers the command below:
git add –all
Okay now you need to commit all the changes and updates in the repository:
git commit -am “make it better”
Now your repository is ready to push on main server.
· Step 3: Pushing Repository to heroku servers.
First make sure that herou cli is installed on your computer. Now we need to login in our Heroku account For that you can use this command:
heroku login
After login we need to clone the repository. So to connect heroku repository to local respository you need to run this command:
heroku git:remote -a sfm-app
In my case heroku app name is sfm-app. Now we have to push the changes to heroku server:
git push heroku master
Now your app is deployed go to app url you will notice that your app is running on that server.
Updating Repository
First you need to clone heroku repository to your local repository so use this command:
heroku git:remote -a sfm-app
git pull heroku master
or
heroku git:clone -a sfm-app
After that edit the file as per your need and again commit the changes and push it again to heroku.
Working with OpenCV module
To run opencv module in your app you need to add heroku-buildpack-apt to your buildpacks on Heroku platform
For that go to this site:
https://elements.heroku.com/buildpacks
Then you will find apt builtpack their. To install this builtpack on your server, run this command in your command prompt
heroku buildpacks:add — index 1 heroku-community/apt
Now create one extra file named as “Aptfile”
Write following lines in that file:
libsm6
libxrender1
libfontconfig1
libice6
Now save your file and commit the changes. And upload it to heroku server.
To learn more about git commands, visit this site:
https://rubygarage.org/blog/most-basic-git-commands-with-examples