What is Heroku?
Heroku is a cloud platform that helps in developing and deploying apps written in several programming languages. What I like in Heroku is that it creates a Virtual Environment for every app you publish. So, you can have your own packages and frameworks from the language of your choice.
What is webapp2?
webapp2 is a lightweight web development framework for Python by Google. It is one of the easiest frameworks to work with in Python. In fact, I started by journey of web development by learning this framework via the Google App Engine.
1) Set up Heroku on your system
I am not going to write much about this, as Heroku has given an awesome Getting Started tutorial for Python here. Follow that link and you will get the basics of the platform and be up and running with a Hello World application in Heroku in about 15 minutes. Yes, just 15 minutes. The tutorial will also talk about something called a Procfile, its a very important part of this project so, just have an idea about what it exactly does. I highly recommend you to follow that tutorial now, if you are new to Heroku. This tutorial follows a unix shell command line interface.
2) Now, comes the fun part
By this time, you must have installed the Heroku tool belt.
- Create a new folder called hellowebapp. This will be the project folder. All files must reside here.
- Now login to heroku.
$ heroku login
- cd to the project directory
$ cd hellowebapp
- Create a virtual environment. If you don't know what this is, follow this link.
$ virtualenv venv
- Activate the virtual environment
- Install WebOp, Paste and webapp2 using python's setup tools. We are going to use pip for this tutorial.
- Now, create a python file which defines your app. For the sake of the tutorial, we are going to run a simple hello world! example.:
import webapp2 class HelloWebapp2(webapp2.RequestHandler): def get(self): self.response.write('Hello, webapp2!') app = webapp2.WSGIApplication([ ('/', HelloWebapp2), ], debug=True) def main(): from paste import httpserver httpserver.serve(app, host='127.0.0.1', port='8080') if __name__ == '__main__': main()
- Create a file called Procfile.txt
touch Procfile.txt nano Procfile.txt
- Add the following contents to that file and Save it
web: gunicorn hello:app --log-file=-
- Test your app, locally. After the execution of this command, the app will mostly run at http://localhost:5000
$ foreman start
- Copy the list of packages to a file called requirements.txt so that when you deploy, the web server installs the packages automatically. The pip freeze command will come to help.
$ pip freeze > requirements.txt
- Create a git repository and save changes
- Create a Heroku app
$ heroku create
- Push contents to cloud
$ git push heroku master
- Lets just run one dyano of the app
heroku ps:scale web=1
$ source venv/bin/activate
$ pip install WebOp $ pip install Paste $ pip install webapp2
$ git init $ git add . $ git commit -m "First!"
Ta-da! Your webapp2 powered app is now running on heroku! Now start exploring the framework and continue with the work. Any problems? Just comment below. I will reply asap. If you have problems in setting up Heroku, you can ask that too.
Happy web developing!
Happy web developing!