Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Python on Google App Engine: Creating blog engine

runtime

  • Please log in to reply
No replies to this topic

#1 Vladimir

Vladimir

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 79 posts

Posted 21 November 2010 - 07:00 AM

I am going to write series of articles describing process of creating a blog engine for Google App Engine. I suppose that you have some experience with Python and web frameworks around it. I will try to cover non-trivial things that you will meet on GAE (emulating joins on Datastore, using blobstore, queues, map reduce etc), but I will also describe some basic things that are specific to GAE.

I use this environment in my daily development:
1. Windows 7
2. Eclipse + PyDev
3. GAE latest Python SDK: Downloads - Google App Engine - Google Code

You can setup your own environment, but this tutorial will cover mine :)

There are a couple of frameworks you can use on GAE:
1. Flask (get started) - extremely simple micro framework built on top of Werkzeug and Jinja2. You can directly use GAE Datastore, but your views can be easily ported to another platform.
2. Tipfy (get started) has a lot of extensions that are specific to GAE (like auth, sharded counters, appstats/blobstore utilities), but you can not use this framework outside GAE.
3. Django-nonrel (get started) - you will get most of the power of Django, but you will miss GAE specific features. You should use it only if you want to port existing Django app to GAE.
4. webapp, webapp2, kay, web2py and many others - less popular and less documented.

I will use Flask because I want directly use GAE APIs to show more features, but tipfy is probably better choice if you are not going to move from GAE. First, download and copy flask, werzeug and jinja2 to your project root. Then you have to create app.yaml file:

application: flaskapp # application name, http://flaskapp.appspot.com/
version: part1 # this version will be available at http://part1.latest.flaskapp.appspot.com/
runtime: python
api_version: 1

default_expiration: '365d' # expiration for static files

builtins: # enables various  builtin GAE apps
- admin_redirect: on
- datastore_admin: on
- appstats: on
- remote_api: on

handlers: # handlers for urls
- url: /static # we can access our statics at this url, for example http://flaskapp.appspot.com/static/img/logo.png
  static_dir: static # path to folder that conatins static files, static/img/logo.png

- url: /.* # all other requests will be served by main.py script
  script: main.py

You may notice that app.yaml contains reference to file "main.py" and dir "static". Create them. We will also need a separate dir to store templates and file to store our config. You project structure should look like this:

flaskblog/
    werzeug/
    jinja2/
    flask/
    static/
    templates/
    app.yaml
    config.py
    main.py

We created basic structure of our project. Now we can start programming. Very basic main.py file can look like this:

from google.appengine.ext.webapp.util import run_wsgi_app

from flask import *


app = Flask(__name__)
app.config.from_pyfile('config.py') # config.py is empty now, but we will add some lines later


@app.route('/')
def index():
    return render_template('index.html') # index.html will be searched in templates/ dir


def main():
    run_wsgi_app(app)


if __name__ == '__main__':
    main()

Code is very short and self-explaining. index.html is very simple:

<html>

<head>
<title>Flask on GAE</title>
</head>

<body>

<h1>Flask on GAE</h1>

</body>

</html>

To run app execute following command in Command Prompt:

dev_appserver.py .

Site should be accessible at http://localhost:8080/ . There is nothing specific to GAE in the code. If you don't understand anything, please refer to the official Flask docs: Welcome to Flask — Flask v0.6.1dev documentation . To deploy app to GAE use this command:

appcfg.py update .

Site should be accessible at Flask on GAE . Source code is available at bitbucket: vladimir_webdev / flaskapp / source . In next part I am going to cover models (Post, Tag, Category) and views (creating new posts, categories, tagging).
  • 0





Also tagged with one or more of these keywords: runtime

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download