In a flask app,database let us consider postgresql
Now we have to deploy Flask app in heroku which is success in local host.First,
In our app,database connection is like this
con = psycopg2.connect(database='firstdb')
cur = con.cursor()
Instead of this,use....
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
con=psycopg2.connect(database=url.path[1:],user=url.username,password=url.password,host=url.hostname,port=url.port)
also import some modules
import os
import psycopg2
import urlparse
wherever database to connect,we should connect like above in each function..
Next in routes.py or the routing python program.our app includes
app.run(debug=True)
Instead of this...
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
Then also add
app.config.from_object(__name__)
in the below of app = Flask(__name__)..so it should be like this..
app = Flask(__name__)
app.config.from_object(__name__)
Then,
Create account on heroku.com
open terminal and enter
Heroku login #to enter username and password
cd appfloder #appfolder includes route.py,templates etc...
heroku create websitename #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev #creating database
heroku config:set HEROKU=1
virtualenv venv --distribute #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt
requirements.txt should be like this
Flask==0.10.1
Jinja2==2.7.1
MarkupSafe==0.18
Werkzeug==0.9.4
argparse==1.2.1
distribute==0.6.34
gunicorn==18.0
itsdangerous==0.23
psycopg2==2.5.1
wsgiref==0.1.2
then,
pip install -r requirements.txt
git init
Create a Procfile using vi Procfile ('P' is capital) and add following line in it
web: python routeprogam.py #here route program is yours
Also add a .gitignore file using vi .gitignore and add following 3 lines in it.This is for avoiding the virtual env files to deploy.
venv
*.pyc
*.py~
Then,
git add .
git commit -m "hosting"
git push heroku master
After launching it,
heroku pg:promote HEROKU_POSTGRESQL_PINK #PINK is here only your's can be find using
heroku config | grep HEROKU_POSTGRESQL
Then,'
heroku ps:scale web=1
heroku restart --app websitename
heroku open
Then you can see it is working......!!!!!!
heroku logs #for further info
If u have any doubts just clone my simple microblog app using,
heroku git:clone -a latheefmicroblog
and verify the codes and structure of Flask app
Now we have to deploy Flask app in heroku which is success in local host.First,
In our app,database connection is like this
con = psycopg2.connect(database='firstdb')
cur = con.cursor()
Instead of this,use....
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
con=psycopg2.connect(database=url.path[1:],user=url.username,password=url.password,host=url.hostname,port=url.port)
also import some modules
import os
import psycopg2
import urlparse
wherever database to connect,we should connect like above in each function..
Next in routes.py or the routing python program.our app includes
app.run(debug=True)
Instead of this...
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
Then also add
app.config.from_object(__name__)
in the below of app = Flask(__name__)..so it should be like this..
app = Flask(__name__)
app.config.from_object(__name__)
Then,
Create account on heroku.com
open terminal and enter
Heroku login #to enter username and password
cd appfloder #appfolder includes route.py,templates etc...
heroku create websitename #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev #creating database
heroku config:set HEROKU=1
virtualenv venv --distribute #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt
requirements.txt should be like this
Flask==0.10.1
Jinja2==2.7.1
MarkupSafe==0.18
Werkzeug==0.9.4
argparse==1.2.1
distribute==0.6.34
gunicorn==18.0
itsdangerous==0.23
psycopg2==2.5.1
wsgiref==0.1.2
then,
git init
Create a Procfile using vi Procfile ('P' is capital) and add following line in it
web: python routeprogam.py #here route program is yours
Also add a .gitignore file using vi .gitignore and add following 3 lines in it.This is for avoiding the virtual env files to deploy.
venv
*.pyc
*.py~
Then,
git add .
git commit -m "hosting"
git push heroku master
After launching it,
heroku pg:promote HEROKU_POSTGRESQL_PINK #PINK is here only your's can be find using
heroku config | grep HEROKU_POSTGRESQL
Then,'
heroku ps:scale web=1
heroku restart --app websitename
heroku open
Then you can see it is working......!!!!!!
heroku logs #for further info
If u have any doubts just clone my simple microblog app using,
heroku git:clone -a latheefmicroblog
and verify the codes and structure of Flask app
No comments:
Post a Comment