EasilyBaffled
EasilyBaffled

Reputation: 3882

Google App Engine properties of write

I was looking at the code from this site, for a basic google app engine calculator. I am just as inexperienced with GAE as I am with HTML, so when I saw the code bellow I was a little confused. Mostly with the last line </html>""" % (result, buttons)). What is the % for and how does it relate result and buttons to the html code?

        result = ""
        try:
            result = f[operator](x, y)
        except ValueError:
            result = "Error: Incorrect Number"
        except ZeroDivisionError:
            result = "Error: Division by zero"
        except KeyError:
            pass
        # build HTML response
        buttons = "".join(["<input type='submit' name='operator' value='"
                           + o + "'>" for o in sorted(f.keys())])
        self.response.out.write("""<html>
            <body>
            <form action='/' method='get' autocomplete='off'> 
            <input type='text' name='x' value='%s'/><br/>
            <input type='text' name='y'/><br/> 
            %s 
            </form>
            </body>
            </html>""" % (result, buttons))

Upvotes: 1

Views: 100

Answers (1)

Ezra
Ezra

Reputation: 7702

The % is for formatting strings in Python. See a good explanation at Dive Into Python. In your example they are used to replace the '%s' characters with the values from variables.

Modifying your example:

A modified version your example, hardcoding values of result and buttons.

result = "THIS IS MY RESULT"
buttons = "AND MY BUTTON"
output = """
<html>
    <body>
        <form action='/' method='get' autocomplete='off'> 
            <input type='text' name='x' value='%s'/><br/>
            <input type='text' name='y'/><br/> 
            %s 
        </form>
    </body>
</html>
""" % (result, buttons)

print output

would yield:

<html>
    <body>
        <form action='/' method='get' autocomplete='off'> 
            <input type='text' name='x' value='THIS IS MY RESULT'/><br/>
            <input type='text' name='y'/><br/> 
            AND MY BUTTON 
        </form>
    </body>
</html>

In your example buttons holds more Html, and format strings make more sense in a context where the values would actually change, but the above should illustrate the basic principle.

A simpler example:

The code below:

result = "THIS IS MY RESULT"
buttons = "AND MY BUTTON"
print "%s ... %s!" % (result, buttons)

Would yield:

THIS IS MY RESULT ... AND MY BUTTON!

How it relates to App Engine:

Both examples above say print: this prints the output to "stdout"—your console.

In your original example, it says self.response.out.write, which is how you tell App Engine to write the text (which is Html) to your browser.

Concretely, if you change:

result = "THIS IS MY RESULT"
buttons = "AND MY BUTTON"
print "%s ... %s!" % (result, buttons)

to:

result = "THIS IS MY RESULT"
buttons = "AND MY BUTTON"
self.response.out.write("%s ... %s!" % (result, buttons))

the text will appear in your browser when you visit the page instead of on the console.

References:

Dive Into Python, also linked above is a great resource for learning Python. The whole book is good if you're new to Python. As are the Udacity courses.

The Python documentation on format strings is a good reference for format strings specifically.

The book "Using Google App Engine" is a great resource for learning Python, Html, and App Engine all at once. I can honestly recommend it, having read it myself. It's very accessible, but it is a few years old now.

Have fun!

Upvotes: 3

Related Questions