Sometimes I have to put text on a path

Sunday, July 3, 2011

Hello-guestbook with Google App Engine and webapp

This post is the second part of the post http://ex-ample.blogspot.com/2011/07/first-steps-google-appengine-mac.html (a tutorial for "dummies v0.0.0.1alpha").
    The  7 steps of this tutorial (D,E,F,G,H,I,J):
    • Creating a Simple Request Handler and the Configuration File
    • Using the webapp Framework
    • Using the Users Service
    • Handling Forms With webapp
    • Using the Datastore
    • Using Templates
    • Using Static Files
    corresponds to:
    1. http://code.google.com/appengine/docs/python/gettingstarted/helloworld.html
    2. http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html
    3. http://code.google.com/appengine/docs/python/gettingstarted/usingusers.html
    4. http://code.google.com/appengine/docs/python/gettingstarted/handlingforms.html
    5. http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html
      (Note: If you built your application using an earlier version of this Getting Started Guide, please note that the sample application has changed. You can still find the sample code for the original Guestbook application, which does not use ancestor queries, in the demos directory of the SDK)
    6. http://code.google.com/appengine/docs/python/gettingstarted/templates.html
    7. http://code.google.com/appengine/docs/python/gettingstarted/staticfiles.html
    --D----part D--- "Simple Request Handler and the Configuration File"
    http://code.google.com/appengine/docs/python/gettingstarted/helloworld.html
    your appspot.com=just a message.
    --E----part E--- webapp:
    In this above part-D, we have used the Common Gateway Interface (CGI) which is a standard (RFC 3875: CGI Version 1.1) that defines how web server software can delegate the generation of web pages to a stand-alone application or an executable file. Such applications, known as CGI scripts, can be written in any programming language, although scripting languages are often used.
    Now we will use the webapp Framework far better than the simple CGI.
    http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html

    The CGI standard is simple, but it would be cumbersome to write all of the code that uses it by hand. Web application frameworks handle these details for you, so you can focus your development efforts on your application's features. Google App Engine supports any framework written in pure Python that speaks CGI (and any WSGI-compliant framework using a CGI adaptor), including:
     DjangoCherryPyPylonsweb.py, and web2py.

    You can bundle a framework of your choosing with your application code by copying its code into your application directory.
    App Engine includes a simple web application framework of its own, called webapp. The webapp framework is already installed in the App Engine environment and in the SDK, so you do not need to bundle it with your application code to use it. We will use webapp for the rest of this tutorial.
    A webapp application has 3 parts:
    • one or more RequestHandler classes that process requests and build responses
    • a WSGIApplication instance that routes incoming requests to handlers based on the URL
    • a main routine that runs the WSGIApplication using a CGI adaptor.
    Let's rewrite our friendly greeting (see the guestbook.py) as a webapp application.
    Edit guestbook.py and replace its contents with some lines of code posted at : http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html:

    at this step, your appspot.com=just a message. See the source of the page:

    --F----part F---  the Users Service:
    the user gets a log-in window just an email.

    --G----part G--- Handling Forms With webapp:
    http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html
    --H----part H--- Datastore:

    This latest version mixes HTML content with the code for the MainPage handler. This will make it difficult to change the appearance of the application, especially as our application gets bigger and more complex. Let's use templates to manage the appearance, and introduce static files for a CSS stylesheet.
    Continue to Using Templates.

    --I----part I--- the Templates:
    HTML embedded in code is messy and difficult to maintain. It's better to use a templating system, where the HTML is kept in a separate file. This separate file has a special syntax to indicate where the data from the application appears. There are many templating systems for Python:
     EZTCheetahClearSilverQuixoteDjango...
    You can use your template engine of choice by bundling it with your application code.

    The webapp module includes Django's templating engine. Versions 1.2 and 0.96 are included with the SDK and are part of App Engine, so you do not need to bundle Django yourself to use it.
    See the Django section of Third-party libraries for information on using supported Django versions:
    http://code.google.com/appengine/docs/python/tools/libraries.html#Django

    For the guestbook.py file, you have to :
    http://code.google.com/appengine/docs/python/gettingstarted/templates.html
    1)Add 2 lines of import statements at the top,
    2)Replace the MainPage handler with 10lines and this 2 lines:

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


    Finally, create a new file in the guestbook directory named "index.html".

    template.render(path, template_values) takes a file path to the template file and a dictionary of values, and returns the rendered text.
    The template uses Django templating syntax to access and iterate over the values, and can refer to properties of those values. In many cases, you can pass datastore model objects directly as values, and access their properties from templates.
    Tip: An App Engine application has read-only access to all of the files uploaded with the project, the library modules, and no other files. The current working directory is the application root directory, so the path to index.html is simply "index.html".



    --J----part J---the Static Files, example CSS file:
    Every web application returns dynamically generated HTML from the application code, via templates or some other mechanism. Most web applications also need to serve static content, such as images, CSS stylesheets, or JavaScript files. For efficiency, App Engine treats static files differently from application source and data files. You can use App Engine's static files feature to serve a CSS stylesheet for this application.


    Unlike a traditional web hosting environment, Google App Engine does not serve files directly out of your application's source directory unless configured to do so. We named our template file index.html, but this does not automatically make the file available at the URL /index.html.
    But there are many cases where you want to serve static files directly to the web browser. Images, CSS stylesheets, JavaScript code, movies and Flash animations are all typically stored with a web application and served directly to the browser. You can tell App Engine to serve specific files directly without your having to code your own handler.


    http://code.google.com/appengine/docs/python/gettingstarted/staticfiles.html



    1) Edit app.yaml and add:

    handlers:
    - url: /stylesheets
      static_dir: stylesheets
    --------
    The new handlers section defines two handlers for URLs. When App Engine receives a request with a URL beginning with /stylesheets, it maps the remainder of the path to files in the stylesheets directory and, if an appropriate file is found, the contents of the file are returned to the client. All other URLs match the / path, and are handled by the helloworld.py script.
    By default, App Engine serves static files using a MIME type based on the filename extension (http://en.wikipedia.org/wiki/MIME). For example, a file with a name ending in .css will be served with a MIME type of text/css. You can configure explicit MIME types with additional options.

    URL handler path patterns are tested in the order they appear in app.yaml, from top to bottom. In this case, the /stylesheets pattern will match before the /.* pattern will for the appropriate paths. For more information on URL mapping and other options you can specify in app.yaml, see the app.yaml reference:

    2) Create the directory guestbook/stylesheets and in this new directory, create a new file named "main.css"


    3) Finally, edit guestbook/index.html and insert 3 lines : 2  head tags and a CSS line.

    No comments:

    Post a Comment