Showing posts with label development. Show all posts
Showing posts with label development. Show all posts

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!

Saturday, July 6, 2013

PHP Code Snippets: Generate a random string

Generating random strings might be very useful when developing a website. You can use random strings for various purposes:
2) Generating Coupon codes for a shopping website
And lots more! 
Soon, I will add tutorials to create all the examples that I have mentioned above. But for now, just take a look at the function I have given below:

The variable $length can be set to the length of string that you want to have.

How to use the function?

Just call it whenever you want the random string to interact with the PHP script you are designing.
Ex: $myrand = generatestring(8) // Generates 8 character random string

Explore!

Add more type of characters you wanna include in your PHP script to the variable in the function $charset

More coding at Let's Code.


Thursday, June 20, 2013

Best books to kickstart learning web development

A complete beginner to programming and web development? Need something to kick start your learning of the languages that will let you convert your ideas into full featured web apps? Here are three books I recommend that you should start with. Sure you can find PDFs for the same, but I highly recommend you to buy these books, as they have lot of pencil-driven exercises as a part of their course. Also, they incorporate funny methods to keep you interested all time long. In fact, all of us thought that learning programming was just about reading texts and texts, I sure did, but this is certainly something new! They have lots of graphics and hilarious methods of understanding the cores of any language they publish a book about.
Learning PHP:




Both links above have the lowest price that you are gonna ever find on the market. Enjoy!

Set up the best web development environment - Netbeans + Apache + PHP + MySQL + jQuery and lots more!


Well, PHP, MySQL, jQuery - the three most essential tools we need to create the next gen websites or develop interactive content in the world of HTML5.
jQuery will run client side, but PHP and MySQL require server side programming. Hence we need to set up a similar environment on PC, and also we need something to code our thoughts and ideas using our knowledge. Netbeans is one of the best IDE(Integrated Development Environment) that can let you do this with much ease, and basically its free. Everything that you will need to complete this tutorial will be free.

Step 1: Set up a server

There are various apps that let you create an Apache+PHP+MySQL environment of your Windows PC. My favorites are:
Software Download Link Extra notes
Zend Server Click here More secure, High level server administration, might not have the latest version of PHP, MySQL and other modules
EasyPHP Click here Less Secure, Easy administration and installation of modules. Fast updates to PHP and MySQL << Recommended 
WampServer Click here Less secure,option to convert to web server, might not have the latest version of technologies(Apache, PHP, MySQL)

Well, just install these apps like you install any other Windows Software and your server will be up and running in no time.
This was easy, wasn't it?
Now comes the coding part..

Step 2: Install Netbeans

Why Netbeans?
Well, its fully featured, easy to use and has support for lots of languages and is FREE! You will also need JDK(Java SE) latest version. Here is the link.

Step 3: To create projects using NetBeans

Well, creating projects is easy. Now what you need is a simple selection to make when you are kick starting your development. In the New Project Wizard, choose PHP>PHP Application.

If you are creating a PHP project for the first time, Netbeans will take few seconds to activate the PHP Module.
 When you are in this screen when going through the New Project wizard, make sure you check the box copy files to a folder option as mentioned below. 
Now, select the path according to the location of your public www folder(also called document root). If you have downloaded any of the above softwares that I have mentioned, here is a guide to those folders:
ZendServer - <path to Zend Server installation directory>\Apache2.2\htdocs
EasyPHP - <path to EasyPHP installation directory>\data\localweb
WampServer - <path to WampServer installation directory>\www

Thats it! You are done!

Additional Notes:

  1. I prefer EasyPHP as they have latest technologies, so less or no bugs will be encountered when developing.
  2. Do set up root password for your MySQL databases. This makes your server quite secure, in case you decide to make the same server live.
  3. Copy the jQuery script into the folder where you can use it properly. For less amount of code, I prefer to save the .js file into the same directory as the main web page is.
  4. It is always better to choose a proper web host, rather than hosting the scripts yourself. Sure this is a DIY package for creating a web server, but its not secure enough.
  5. WAMPServer comes with phpMyAdmin by default, to access it use localhost\phpmyadmin. For ZendServer and EasyPHP you will have to install the modules.For tutorial on Zend Server, visit this link and for EasyPHP, just activate the phpMyAdmin module in the server administration GUI.
Now that you have set up a complete web Development server, how about kickstarting to learn web development? Check out one of the best books I recommend for beginners to start learning web development by clicking here!