Feeds:
Posts
Comments

Posts Tagged ‘Python’

Django is an excellent web-framework written in Python.

Here is a quick setup guide for installing Django and MySQL-python on Mac OS X Lion.

Setup Django

– Download latest Django here

– At this point I am assuming that you are familiar with the Terminal, so simply execute the following commands on your Downloads folder:

tar -xzvf Django-1.3.1.tar.gz
cd Django-1.3.1
sudo python setup.py install

Test Django installation

Type on the Terminal:

python
import django
print django.get_version()

Setup MySQL-python package

Download the latest MySQL (64-bit, tar archive) here

– Extract tar contents
– Rename folder to “mysql”
– Copy folder to /usr/local/
– You should now have mysql installed on /usr/local/mysql

Download the latest MySQL-python package here

Type on the Terminal:

tar -xvzf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3

Edit site.cfg on your favorite text editor and change the mysql path as below: mysql_config = /usr/local/mysql/bin/mysql_config

You are now ready to build and install MySQL-python:

sudo python setup.py build
sudo python setup.py install

Edit your ~/.bash_profile, add the following line:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

If you are already using MAMP you still have to follow the above MySQL installation process, as MAMP by default doesn’t include the required header files to build MySQL-python.
Moreover if you prefer MAMP, you don’t have to run mysql from /usr/local/mysql.
On your settings.py remember to set “127.0.0.1” as your preferred db host in case that “localhost” doesn’t work.

Test MySQL-python installation

Restart Terminal and type:

python
import MySQLdb

If you are not getting any errors at this point, then you are all good to go!
I’d suggest starting from the Django’s excellent introductory tutorial.

Read Full Post »

Python 3.0 – Final

We are pleased to announce the release of Python 3.0 (final), a new production-ready release, on December 3rd, 2008.

Python 3.0 (a.k.a. “Python 3000” or “Py3k”) is a new version of the language that is incompatible with the 2.x line of releases. The language is mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Also, the standard library has been reorganized in a few prominent places.

What’s New in Python 3.0
Download

Read Full Post »

Python 2.6 – Final

We are pleased to announce the release of Python 2.6 (final), a new production-ready release, on October 1st, 2008.

What’s new in Python 2.6
Download

Read Full Post »

This is my second article about the Google App Engine.
On the previous article, I have gone through the installation/setup process, and a “Hello World” project.

Introduction

While developing web applications it is always a good practice to separate the presentation layer from the application logic. This approach is recommended for all modern web frameworks, whether that is PHP, ASP.NET, Google App Engine etc.

During the design time, different people/teams may work on each layer, i.e. web designers on the presentation layer, and programmers on the code-behind. Maintenance will also be easier, as it could only involve the update of a single layer.

In our case, the presentation layer is going to be a simple HTML file, and the application logic a Python script file.

Coding – Part 1

Create a new folder on your hard disk called “template”.

Create the “template.py” file with the following code:

import wsgiref.handlers, os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class MainPage(webapp.RequestHandler):
    def get(self):
        html_title = 'Google App Engine - Template'
        html_body_text = 'This is a simple demo of the templating system.'

        template_values = {
            'html_title': html_title,
            'html_body_text': html_body_text,
            }

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

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

if __name__ == "__main__":
    main()

On the above code we are declaring two variables, named html_title and html_body_text.
The html_title will form the HTML title content, while the html_body_text is going to be printed on the HTML body.

Now, let’s include the variables on the HTML file, named “index.html”:

<html>
<head>
    <title>{{ html_title }}</title>
</head>
<body>
    
{{ html_body_text }}
</body>
</html>

We also need to create the configuration app.yaml file with the following code:

application: template
version: 1
runtime: python
api_version: 1

handlers:
- url: /
  script: template.py

Now let’s start the development web server by typing in the command prompt:
“dev_appserver.py D:\GoogleAppEngine\template”
Please replace the “D:\GoogleAppEngine\template” with the path of your “template” folder.

Point your web browser to http://localhost:8080/ , and you should see something like the following output:

Coding – Part 2

Now we are going to further improve our presentation layer by adding a CSS file.

Create a subfolder named “stylesheets” inside the “template” folder.

Create the “template.css” stylesheet file with the following code:

p {
    color: #FF0000;
}

Edit the “index.html” in order to include the CSS file:


<html>
<head>
	<title>{{ html_title }}</title>
	<link type="text/css" rel="stylesheet" href="/stylesheets/template.css" />
</head>
<body>
	
{{ html_body_text }}
</body>
</html>

Add the following handler on the app.yaml file:

- url: /stylesheets
  static_dir: stylesheets

Save all files, refresh your browser, and you should see the following output:

Conclusion

Google App Engine template system is based on Django’s template engine, and as demonstrated above, it is quite easy to be implemented.
You can find a more complicated example on Google App Engine’s template help page, using class properties and if/else logic on the presentation layer.

Read Full Post »

Introduction

Lately I have been reading quite a lot of articles about Google App Engine, and I just couldn’t resist trying it out myself.

Google App Engine enables you to build web applications on the same scalable systems that power Google applications.

If this is the first time you are hearing about the Google App Engine, it would be a good idea to read this introductory document.

The following post is based on the official “Getting Started” guide.

Setup

Setup is quite easy, all you need is the Google App Engine SDK, and the Python Runtime software.
I am using Windows XP, so I have downloaded the “GoogleAppEngine_1.0.1.msi” and “python-2.5.2.msi” installers from the above links.
Both of them are available for Windows/Mac/Linux, so you can download the required files according to your Operating System.
Installation is also straight-forward, I have installed both of them with the default options. There was no need to edit config files or do something else.
Google App Engine even sets a “Path” Environment Variable in Windows to make the development web server more accessible.

Coding – Part 1

Here comes the fun part, let’s find out how to build the “Hello World” application.

Create a new folder on your hard disk called “helloworld”.

Fire up your favorite text editor, mine is Notepad++, and paste the following code:

print 'Content-Type: text/plain'
print ''
print 'Hello World!'

Save the file as “helloworld.py” inside the folder “helloworld”.

Create a new text file called “app.yaml” in the same folder with the following contents:

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

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

The “app.yaml” is the configuration file, you can find more information about it here.

Now let’s start the development web server by typing in the command prompt:
“dev_appserver.py D:\GoogleAppEngine\helloworld”
Please replace the “D:\GoogleAppEngine\helloworld” with the path of your “helloworld” folder.

Point your web browser to http://localhost:8080/ , and you should see something like the following output:

Now change the “helloworld.py” a bit:

print 'Content-Type: text/plain'
print ''
print 'Hello World! - Google App Engine rocks!'

Save it, refresh your browser, and you should see:

The development web server is monitoring the project files, thus you can automatically view the changes!

Coding – Part 2

On this second part we are going to use the included, with the SDK, “webapp” framework.

Let’s create a new Python script file called “helloworld2.py” with the following contents:

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('Hello World from the webapp framework!' )

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

if __name__ == "__main__":
  main()

We also need to modify the configuration file (app.yaml), in order to specify the url that is going to handle the second script:

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

handlers:
- url: /
  script: helloworld.py

- url: /helloworld2
  script: helloworld2.py

As you can see, we changed the first handler (/.* -> /) to respond only to the root directory.
Also we added a second handler to respond on the /helloworld2 path.

Navigate your browser to http://localhost:8080/helloworld2 and you should see:

You can find here more information about the “webapp” framework and how to use it.

Conclusion

Well that was just the beginning to an exciting new way of developing web applications.
The API is under heavy development, and definitely you have to spend some time in order to get familiar with it, and build a real-world application.

Read Full Post »