Saturday, December 20, 2014

Running the webapp2 framework on Heroku

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

Lets begin!

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. 
  1. Create a new folder called hellowebapp. This will be the project folder. All files must reside here.
  2. Now login to heroku.
    $ heroku login
  3. cd to the project directory
    $ cd hellowebapp
  4. Create a virtual environment. If you don't know what this is, follow this link.
    $ virtualenv venv
  5. Activate the virtual environment
  6. $ source venv/bin/activate
  7. Install WebOp, Paste and webapp2 using python's setup tools. We are going to use pip for this tutorial.
  8. $ pip install WebOp
    $ pip install Paste
    $ pip install webapp2
    
  9. 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()
    
  10. Create a file called Procfile.txt
    touch Procfile.txt
    nano Procfile.txt
    
  11. Add the following contents to that file and Save it
    web: gunicorn hello:app --log-file=-
  12. Test your app, locally. After the execution of this command, the app will mostly run at http://localhost:5000
    $ foreman start
  13. 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
  14. Create a git repository and save changes
  15. $ git init
    $ git add .
    $ git commit -m "First!"
    
  16. Create a Heroku app
    $ heroku create
  17. Push contents to cloud
    $ git push heroku master
  18. Lets just run one dyano of the app
     heroku ps:scale web=1 
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!