Showing posts with label App Engine. Show all posts
Showing posts with label App Engine. Show all posts

Saturday, October 1, 2011

Teeny URL


Google has many services like plus, gmail, orkut, blogger which is familiar to the youth. But many of them are unaware of Google’s App Engine service. App engine allows the developers to host their applications online. Google gives free domain names to us. Python and Java are the current supporting platforms.

When I heard about this service, I eagerly started a url shortening application in python. Many of our urls, especially blog links and articles are large and bulky. It is difficult to use them on twitter and SMS where character limit is a big problem. So I think about a url shortening application for reducing the length of the large urls. The main advantage of shortened url is, it can be used anywhere on the web instead of the original url. Another import fact is every large url will get a unique shortened url. So it will not let the user into any problem.

Here I used python as the back end. Webapp framework is used and the database is Datastore. Datastore is a database by Google and it is different from other traditional databases. It uses an object oriented database management system. HTML and CSS is used for the interface.

url for Teeny URL application is,


Some screenshots of the Teeny URL application is given below,


































You can download the source code also. Click on the tab given below.


If you have any doubts with the codes, you can also read my posts regarding the source code of the Teeny URL. Links for my Teeny URL posts are,


Google App Engine - An Introduction
Creating application in Google App Engine
Two main classes in Teeny URL
Implementing application on App Engine

Thanks

AJAY

Implementing application on App Engine server



I have explained you about the source code in my previous posts. Now I will explain you how to run this in local machine and how to upload to appengine.

We need to keep the google_appengine folder and our teenyurl folder in same directory. After that we need to run the following command in order to run the application in our localhost.

google_appengine/dev_appserver.py teenyurl/

We can now open the browser and type the localhost address to view the application. If you want to make the application online, you need to first register for a free available domain name. Then you can upload the project by typing the code given below,

google_appengine/appcfg.py update teenyurl/

This will ask your gmail address to which the Google appengine domain name is associated. You need to enter your Google account password also. After that it will be deployed on appengine server. Now you can type the url of your application in the browser to access the application. My application’s url is 


If you want to upload an updated version, then change the version number in app.yaml file from 1 to 2. Then 2 versions will be available in the appengine dashboard. You need to change the default version to 2 in order to see the updated version. Versions tab will help you to manage the various versions. You can also see the various versions by prefixing version number on appspot url. Like below,


Dashboard will show you the daily quotas. Datastore viewer will help you to view the data by querying.

I feel nice to work with Google’s apengine service and thinking about developing other appengine applications also. You can host upto 10 applications using your one Google account. Explore the world of appspot projects and get inspired from those applications.
Let’s do an appengine project dude!!!

Thanks

AJAY

Two main classes in Teeny URL


I have explained that I have 2 main classes- Home and TeenyURL – in my application. Lets start with Home class.

Home class

This class has two basic functions, get() and post(). In the get(), we need to render the index.html file. When our application is executed, index file will be loaded first. We need to pass some parameters/variables to the index.html file. For that we use the following code.

template_values = {
            'title': "TeenyURL",
}

Now the template_values hold the string value “TeenyURL” which will be replaced by the variable ‘title’ in the index.html file.

<html>
<head>
<title> {{title}} </title>

This is the code in the index.html file. The ‘title’ variable is written inside {{}} so  that the compiler will understand it as a variable. The ‘title’ is replaced with “TeenyURL”. To render the index.html, we need to specify the path to the index.html and its parameters (ie., template_values). Look at the below code,

path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

Thus our index.html file is loaded and the user will see interface to enter the long url. When the user inputs some url and then submits, the post() of the Home class works.

url = self.request.get('url')

This code is used to get the value from the form where the user has given his input. Think about a situation where a user inputs his url and captures his short url and the again inputs the same large url. In that situation we need to provide the same short url which is provided earlier. So we need to first check, whether the submitted url is already in the datastore.  The following code is used for that.

x = db.GqlQuery("SELECT * FROM Data WHERE url=:1",url)
count = x.fetch(1)
z = len(count)
if (z>0):
                turl = count[0].turl

If the large url is a new one to datastore, we need to create a new short url for it.

while y>0 :
                word = "".join(random.choice(char_array) for i in range(4))
                x = db.GqlQuery("SELECT * FROM Data WHERE code=:1",word)
                count = x.fetch(1)
                y = len(count)

Here also checks whether the random generated code is already generated before for other large urls. I stored the large url and the corresponding short url along with the 4 letter code in the datastore. We need to create an object of the Data class. The below code is used to enter the values to the datastore,

data = Data()
data.url = url
data.code = word
data.turl = turl
data.put()

The below code is used to render the index.html file with original url and its corresponding short url.

template_values = {
            'title': title,
            'url' : url,
            'teenyurl' : turl,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

TeenyURL class

When anyone uses this short url, I need to redirect it to its original url. That is the purpose of TeenyURL class. The following code is used for that,

x = Data.all()
x = db.GqlQuery("SELECT * FROM Data WHERE code=:1" , path)
count = x.fetch(1)
ourl = count[0].url
self.redirect(ourl)

In order to efficiently call the corresponding classes, I defined the templates paths as below,

application = webapp.WSGIApplication( [('/',Home),
                              ('/(.+)',Teenyurl)],
                              debug=True)

Now I have explained about all the code. In my next post I will explain you how to run the application in local machine and how to upload it to the appengine.

Thanks

AJAY

Creating an application in Google App Engine


I have explained you about the Google appengine. Now I’m going to show you how I developed my Google appengine project. I have implemented an url shortening application in appengine. We always see long and bulky urls. For example look at the url of this post. If we paste this url in twitter or try to send this link to someone using SMS, it will be difficult to convey the link and message. So you can use my application to shorten your bulky large url.

In appengine, I used python as the back end. Webapp framework is used here. The database used here is Google’s datastore. It is little bit different from the traditional databases. It serves an object oriented database management system.

First I downloaded Google appengine SDK for python. I extracted it to home folder in my Ubuntu system. Then created a folder with name, “teenyurl”, which is the name of my application. Then created a app.yaml file in that folder. Then added following lines to it,

application: teenyurl
version: 1
runtime: python
api_version: 1
handlers:
- url: /static
  static_dir: static
- url: /.*
  script: teenyurl.py

The application name is set to teenyurl and it is the first version. Python is used as the back end. Static folder is defined so that we can add the css and image files to that folder. Then I created teenyurl.py file. We have to add the python code in that file.
Different from others, we need to create the table by defining a class.

class Data(db.Model):
      url = db.StringProperty()
      code = db.StringProperty()
      turl = db.StringProperty()

The above code creates a table with name Data and attributes url, code and turl with varchar type. If we want to insert values, we must create an object of class Data.

data = Data()
data.url = url
data.code = word
data.turl = turl
data.put()

The above code creates an object ‘data’ of class ‘Data’ and the inserts the value. put() is used to commit the values.


x = db.GqlQuery("SELECT * FROM Data WHERE code=:1" , path)


The above code returns the list of matching fields in the SELECT query. you may noticed the word code=:1",path). :1 is replaced by the first argument, here it is path.

Here in my application I have only 2 major classes. One is to create the short url (Home class) and the other is to redirect to the original url (TeenyURL class), when the short url is used. In the get() of Home class we are rendering the home page of the application. We have the home page file “index.html”.

I will explain more about these 2 classes in the next post. You can download the source code also. Click here.

Thanks

AJAY 

Google App Engine – An introduction


Many of us already handled a web application for our academic projects. But only a few of them are dare to buy a domain name and host it, so that anybody can access the application through Internet. I have completed my master of computer application project last week. It is a git based wiki implementation, and I will post some posts about that project soon. I also thought about buying a domain name, so that I can put it in my resume and the companies can view it online at the time of interview. I thought it will help me getting better jobs.

Then I come to know about the Google’s App Engine service. Like blogger, orkut, gmail, docs etc Google is also availing better services for students like Google Code, App Engine.

What is a Google App Engine? It is a service of Google where programmers can host their web applications at no cost. This free service gives us a domain name and we can host the code so that everyone can see your application by typing that url. Now Google App Engine supports only Python and Java. It gives a version control for every version of our code that are deployed. It gives many information like CPU time used, No. of requests, memory used etc.

How App Engine works? Google gives this service using a cloud computing technology. Google has server farms where thousands of server machines are working 24x7. When we are hosting an application, it is stored somewhere in these servers. Our application gets some amount of RAM and it is processed by some CPU.

Is it completely free? It is free to common applications. But there is a daily quota for each measures like average CPU time. I think normal applications will not reach that quota easily. If your application is viewed by a million people per day, then the quota may be crossed.

I think many of us are not aware of App Engine service of Google. If you have knowledge in Python or Java, try for an app engine project. I completed an app engine project and I will post about it soon. I think that will be helpful to the beginners.

Thanks

AJAY