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

Wednesday, July 30, 2008

Sending an email with Google App Engine

When I first started learning, my application aim was a 'Mass Mailer' with Google App Engine. To start (as a newbie in this domain), I should start with the simplest operation: 'Sending an email using Google App Engine'.

To do this I was lost, I don't know anything about Python. But, how? Take a look around you'll probably find something, return always to the Google App Engine Documentation. Yes, the Send Mail section is available. Let's see it.

Mail Api docs, give you the sample from the overview! Here's what I found

from google.appengine.api import mail

class ConfirmUserSignup(webapp.RequestHandler):
def post(self):
user_address = self.request.get("email_address")

if not mail.is_email_valid(user_address):
# prompt user to enter a valid address

else:
confirmation_url = createNewUserConfirmation(self.request)
sender_address = "support@example.com"
subject = "Confirm your registration"
body = """
Thank you for creating an account! Please confirm your email address by
clicking on the link below:

%s
""" % confirmation_url

mail.send_mail(sender_address, user_address, subject, body)


Before doing anything! You need to know, that the Send mail function isn't included by default so when running your server, you should add the following command
dev_appserver.py --enable_sendmail
Or your mail won't be sent.
Then create a new Google App Engine Application. If you don't know how, here's a quick startup to create your Google App Engine application in minutes.

Trouble in manipulating this code? No, don't do, you don't need. First we are going simply to test the send_mail function, second we are going to use the Django Frameword and not the Google one.

Here's the code, that I use, simple and clean

from google.appengine.api import mail

print 'Content-Type: text/plain'
print ''
print 'I think the email was sent or going to!'
mail.send_mail("omar.abid2006@gmail.com", "omar.abid2006@gmail.com", "Google Coder", "Googler Coder Welcome")


Here's the list, don't be stupid!
  • Your application won't work on your Localhost, so upload it
  • To upload it, seek this article
  • Run your application 'name.appspot.com'
  • Check your inbox
  • Don't Send more than 2000, see quotas here
Ok, here's the first goal and it was done, more to come in the future.

Sunday, July 27, 2008

Django Platform for Google App Engine

I was surfing those days Google App Engine websites and found that the most popular and used platform to work with Google App Engine is Django. Luckily, it's already included on Google App Engine SDK in the lib folder. But I always give a hit for the official website and so you can do and visit the Django website.

The website documentation is so cool, more than that I found free book, you can get it here.
But what's Django and how it works?
As I said Django is a python Framework, written in pure python, then the matter is to know python and then Django will only accelerate your work
At its core, Django is simply a collection of libraries written in the Python programming language. To develop a site using Django, you write Python code that uses these libraries. Learning Django, then, is a matter of learning how to program in Python and understanding how the Django libraries work.
From the Django book
Ok, so we have to come back to Python, oh yes, here's a small comparision between python and django.
The example (found it in the django book) displays the ten most recently published books from a database.
The sample written in Pure Python

#!/usr/bin/python

import MySQLdb

print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"

connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")
for row in cursor.fetchall():
print "<li>%s</li>" % row[0]

print "</ul>"
print "</body></html>"

connection.close()

The sample written in Django

# models.py (the database tables)

from django.db import models

class Book(models.Model):
name = models.CharField(maxlength=50)
pub_date = models.DateField()


# views.py (the business logic)

from django.shortcuts import render_to_response
from models import Book

def latest_books(request):
book_list = Book.objects.order_by('-pub_date')[:10]
return render_to_response('latest_books.html', {'book_list': book_list})


# urls.py (the URL configuration)

from django.conf.urls.defaults import *
import views

urlpatterns = patterns('',
(r'latest/$', views.latest_books),
)


# latest_books.html (the template)

<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
</body></html>


I know that most of you will see that the django code is harder. Yes, it's longer and harder but smarter to use. I will prefer to use long code, then easily type book.name to display the latest books. The framework aims to make easy long and big application development.

Saturday, July 26, 2008

The Development Console for Google App Engine

Ok; now we are going to work seriously! (I hope so). I'm back from a small vacation and I'm upping for something special now. A nice guy already posted a comment and seems to be interested on the Google App Engine Server. Ohh; I think I have to start serious work on it :)

Now let's focus on the Google App Engine development console. Ok there's no much to see on it. (if you dare to compare it to the ASP.net administrator tools :( )
But I already loved a tool on it and I'll introduce it.

Now run your server; choose any application that doesn't bother.
Then open the following link 'http://localhost:8080/_ah/admin' You should see the admin console. As I said there's no much to see. Forget about the 'Data Store Viewer' and have a look on the 'interactive console'

What's cool on this console
  • Run quickly your test code
  • Check a few lines and find the error easily
  • Good for newbie and learner!
Here's the default code on it.

# Say hello to the current user
user = users.get_current_user()
if user:
nickname = user.nickname()
else:
nickname = "guest"
print "Hello, " + nickname

now try to make a change like deleting the word users in users.get_current_ser()
here's the error message
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\admin\__init__.py", line 179, in post
compiled_code = compile(code, '', 'exec')
File "", line 2
user = .get_current_user()
^
SyntaxError: invalid syntax

Sounds cool no?
Now you may wonder what this sample does. Ok, simply, if you are logged to Google account then it'll display your name. If not then it'll display "guest"!
But where to login? Ohh here's the page http://localhost:8080/_ah/login

Your users are not obliged to redirect to that page to login; you can integrate it on your own site!
You may wonder why it don't asks for a password; that's simple; because you work on local, then Google won't query its' database if you are working on local.

Wednesday, July 16, 2008

Working with Google App Engine Framework

Every language or platform should have frameworks, those are library that just accelerate development speed and better your work.
For example, the Dot Net Framework, try just to compare it with the dot net assembly language. So here's a simple and easy to use example!

While typing huge lines of code to shut down a computer...
'calling windows api
'if..then..end if
and some lines of code
You replace the work with
Windows.shutdown

Framework are huge libraries that replace ALL the code, so you don't have to work with the simple python now. This is an important step if you are going to work with Google App Engine and build secure application and long lines of code.

Google App Engine accept FrameWorks, (it's better to say Python!).
I was in this page and noticed that Google SDK have already a framework included; however if you want to include another framework, you simply have to include its' code in a folder on your project. Like Pear with PHP.

Google App Engine include a simple Framework of its' own, called Webapp

The test application will become then...


import wsgiref.handlers

from google.appengine.ext import webapp

class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Test')

def main():
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
main()


A first look on the code.. that's too long! Yes but easy to understand, if you can't learn it, then you should find a code editor like the Visual Studio editor for .net

Tuesday, July 15, 2008

Google App Engine Webserver

How much will you continue to use Google App Engine with a command line; why not make it easier. Open when Windows opens, easily upload files and much more than that.
Ok, it's not that hard. I'm a Vb.net developer and we can make an application that solve this issue.

What will the application do?
  • Manage your Google App Engine webserver with a user friendly interface.
  • Run automatically when Windows starts
  • Upload your application
  • Create a config file
  • Other things?

Is this free?

Haha, this is Open Source. Ok, I generally don't give my applications source, but this time yes, as I'm going to program for an Open Source community; GOOGLE!

When, how and why?

First it's easy to do it and won't take more than 3 or 4 hours and most of the
work is on design.
I'll host the application on both Google Code and
Codeplex. Every Vb.net programmer will be able to spice the application if he
can. and then it'll grow.
It'll be ready after I complete my current project EntreCard Browser (for EntreCard
bloggers).
So it'll be ready by the next week.

For interested people

The application is very simple, instead of using the command line through the
MS-DOS console, it'll be used through a user friendly Window. This will help you
easily select a path or customize your server... If you have any idea please
post your comment here, because I'm stack with emails :(

How to create a simple Google App Engine Application

Creating a Google App Engine, isn't that hard. Google already provide you with the required details to create your Hello Word application. So let's create a very simple Google App Engine application.
One problem is that Google introduction and samples don't touch begineers, but experienced ones. And not all experts, but in the Linux and PHP related. For example if you have knowledge on Visual Basic .net, then you can't easily make a Google App Engine application. However if you are a python or Linux coder, then Google App Engine is familiar for you.

This is normal, but Google don't provide very detailed information. I sometimes found problem on understanding what it talks about.

Google App Engine isn't flexible:
Google App Engine is cool, only if you look to the application but it's not flexible at all. You need to manage it through a command line tool, which is very annoying.

Simple Start-up:
Here's what a Google App Engine Application is (without the loosing Google tutorial).
  • A simple webpage on a server like PHP that run with Google App Engine (instead of apache)
  • Instead of uploading with FileZilla, you upload with the command line.
  • Instead of running your webserver through a program you run it with a command line.
  • Instead of using .php you use .py
  • The configuration file (like in ASP.net) is app.yaml

How to create your App Engine Application in 5 minutes:

  • Already have python and Google SDK Installed
  • Create a folder in c:/ and name it test
  • Create an app.yaml file
  • Create a test.py file

Put the following on the app.yaml file

application: helloworld
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
script: helloworld.py

Put the following on the test.py file

print 'Content-Type: text/plain'
print ''
print 'Hello, world!'

run your App Engine server:
To run your server, open the Ms-Dos Command line and type
google_appengine/dev_appserver.py c:/test/

Now you should be able to see the application in localhost(8080)

Like ASP.net in stone age!
Yes, while having an easy to use Server, editor and uploader, you have a very hard environement to work. But it's not a matter, Google and me will arrange those things together!

Sunday, July 13, 2008

Geo Chat App Engine... to work!

I liked the idea of Geo chat and decided to just customize it, then spread it up in the net. The application should be successful if I caught 5000 visitor daily, at least when someone enter he find 30 or 40 others to speak with. This will be a chating area! Nice and Wonderful, but when I try it using the appspot for this blog, it told me that I need a Google Map API Key for the following application although it did run correctly on the localhost.

Ok, this is not a big problem, I registered talkhub.appspot.com for my future chat application.

But this time I have to return to my Google tutorial or I'll be really burning steps, so I need to delete geacoder.appspot.com content but how to do that! Never mind we'll learn through the Google tutorial; hope that!

Uploading a Google App Engine application

I'm not burning steps after all, I just want to see the application alive in the internet, what matters if we derived a little bit from the Google tutorial.

So I decided to upload the Geochat sample!
But where to start? The application is complete, all files are in and very light (220 kb). So here the tutorial to be in the right track.

Upload may seems hard, if you are a beginner in programming, command lines...
So here's how to do for empty persons:
  • First you have to set an application, after you are done, remember the application ID (gaecoder is my ID and you can access my application http://gaecoder.appspot.com)
  • Go to your application root and open app.yml or app.yaml
  • Change the following line from...
    application: geochat
    to your application ID
    application: gaecoder
    If you don't do that.. you won't upload the files
  • Then run the command line and enter the following line
    appcfg.py update c:/geochat/
    And replace geochat with your path or folder name
  • You'll be asked for your email then you password (Google email and password)
  • Your upload should run and go if nothing goes wrong!
Visit your application dashboard, you should be able to see your application statics.
Check the application here, you should find Geo chat, but I may change it later...
Have a problem, let face it together, tell me what didn't work!

Running your App Engine Application

Finally running your Google App Engine application on your computer isn't that hard. I could do it on two easy steps.
First make sure your have downloaded and installed both Python and App Engine SDK.
Then download a sample, I chose Geo Chat.

Ok now after having those installed and downloaded a sample, extract the sample and put it on your Hard disk root. (Don't Ask me why, you can put it on another place, any place, but as you are using a command line and not windows then you should have easy to write path).

After extracting and putting the sample files, run the Ms-dos command line and type the following:
dev_appserver.py c:\geochat
Replace geochat with your sample folder name. Now visit your Localhost, you should see your application up and running on your computer.

As the sample use Google Maps and Google accounts, so an internet connection is needed and Google server must be up!

That was quite easy cool and interesting. It really put me on the right way! Now I'm going to try to upload the Geo chat to the App Engine Server, which may be another story...

Getting Started with Google App Engine

So I need to get started with Google App Engine, ehh?!
I follow Google steps, starting with the introduction. So, we are going to create a guest book, then we need a database, authentication and so more.. So that engine will take care of it all?

The thing that attracted me is
Users can post anonymously, or using their Google accounts.
So we are going to use Google accounts, great. I saw some application on the web (especially the big ones, that uses your Google or Hotmail account to import your contact from them.). It's really great, this is the real massive distribution.

Ok so let's go to the next step. It's simply describing the development environment. There's two important parts that you should know, the development web server and how to upload your application.

Google App Engine samples

I got bored from the app engine, I just learned that I need to use the command line to upload a file. And I hate using the command line so much, this don't impress me. However those few samples made me think back on it.

Flyapp is like I-Google. I had a friend having a project like Flyapp which is 6starts. But if you compare them, you'll notice that the App Google Engine one is more delicious than the PHP/Ruby on rail one!


Another application was Spy, but you could say it's the young brother of addictomatic; so nothing really interesting on that Google App Engine.

So my idea was to make an Email sender, other said mass mailer.

Here, at this point, I must break my mind and understand what Google App Engine is, or I'll do... nothing!

Saturday, July 12, 2008

Google App Engine created

Finally I got the Google App Engine created. The problem was that Google App Engine don't accept the following name "googlecoder" which make it not possible to create the app.
Now the application was created but I have some restriction so I should be careful how to use it.

I can create only 3 Google Application, plus other restriction that you find here.

From the first look, the Google App Engine seems like a hosting server, but while using PHP or ASP you are using Google App Engine.

A few examples would solve the problem and let me quickly start coding (I hope so).

Luckily it's Google who give you the samples! So let's start and see how we can upload them and run the application, after that we'll try to change something on the application, so we can create future application

Register your Google App Engine Application

Then we should run. I click the button and decided to create my application. The first step was to identify myself. This will help Google prevent SPAM! I didn't tried but I think 1 phone number can register you one time.

I'm in Tunisia and the process was ultra fast. The assistant told me that I should wait 10 minutes or more but it didn't take even 1 second. I was really shocked because if i was sending the sms to another number and of the same network, this will take longer than 2 minutes to wait. Any way, how you can explain it, I got the code and arrived to the creating page of the application.

I enter the "application name" and it said that it's available, but when I click on Save, that tell me that the ID is not available.

No way, I must try another way. So I started with the Getting Started documentation.

First step is to download Python and install it. Which will be another story!
Python wasn't that lite, it wieght more than 10 mb. One thing that I don't have answer, why should I install Python? Why Google need python? Is it just needed for developer?

I first started by reading the stupid Python introduction
Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code.
Here's what you could read on the main page of Python. But still not clear for me what this thing will do!
I found in the side bar, a widget that shows category where this Python can help and I got interested on the Web Programming one but really didn't find something attracting especially when I saw that the download completed.

Ok the installation with the msi package was too easy but using it... may not, so I got a look on the program menu. I found the documentation, so I opened it and it starts a real server! (like the dot net help).

The help is so big! And Python seems clearly now a group of library, that we'll use perhaps. So I returned to Google App engine.

The next step is to download the Google App engine SDK. I found also some buttons for websites powered by Google App Engine. I decided to use one in the blog when fixing a custom template to it.

The SDK was light, only 2.5 mb, and It was msi package also. That was an important point for me because the Google Desktop SDK for Windows was a zip file, with very stupid files inside.
But 2.5 mb wasn't that impressive for me, if I compare it to Visual Basic .net, this would be 30 times bigger. But bigger don't mean better, but so bigger may mean it!

I found it now boring and won't become interesting unless I see and react with some samples that I'm going to show later.

Google App Engine

I started seeking the API & Developer section and got attracted by the Google App engine.
What really caught my attention is creating Web application hosted by Google where I use Google Technology or API or something like that.

Another thing, I remember a time I see a Google service (that I don't remember well!) where in the bottom there's Built by Google App Engine, which means that this one is powerful and I hope so.

As the hosting of my project or web application was free and instantly I didn't thought and signed up. I also gave a look to the app engine blog to see what's going on, on it.

Quickly I arrived to the start page and hit "Create an application" to see how it works.

If it really was easy to use it then it'll be so cool, because:
  • No assembly required, like an old MS-Dos stand alone
  • Free hosting with 500mb and 5 million page view in a month
Could you understand this one, I read it on the home page:

It's easy to scale.
Google App Engine makes it easy to build scalable applications that grow from one user to millions of users without infrastructure headaches.
What does this really mean, from 1 to 1 million? Does it work like Facebook? Is their any massive distribution for my application?

We are going to know....
Interested on Microsoft Technologies? Read Visual Studio Dot Net