
GAE - Python
Eueung Mulyana
http://eueung.github.io/EL6240/gae
Python CodeLabs | Attribution-ShareAlike CC BY-SA
1 / 22
Agenda
GAE Basics
Example - Guestbook
2 / 22
 GAE Python Basics
3 / 22
Getting Started
 
GAE Launcher : Add New Application
GAE Launcher - Idle vs. Running App
4 / 22
GAE Launcher -> Browse GAE Launcher -> SDK Console
5 / 22
importwebapp2
#----------------------------------------
classMainHandler(webapp2.RequestHandler):
defget(self):
self.response.write('Helloworld!')
#----------------------------------------
app=webapp2.WSGIApplication([
('/',MainHandler)
],debug=True)
Generated Structure/Codes
application:hello
version:1
runtime:python27
api_version:1
threadsafe:yes
handlers:
-url:/favicon.ico
static_files:favicon.ico
upload:favicon.ico
-url:.*
script:main.app
libraries:
-name:webapp2
version:"2.5.2"
app.yaml
6 / 22
https://console.cloud.google.com -> Create Project
7 / 22
https://console.cloud.google.com/home/dashboard
8 / 22
GAE Launcher -> Dashboard
9 / 22
Uploading
 
GAE Launcher -> Deploy
application:hello-gae-1156
version:1
runtime:python27
api_version:1
threadsafe:yes
handlers:
-url:/favicon.ico
static_files:favicon.ico
upload:favicon.ico
-url:.*
script:main.app
libraries:
-name:webapp2
version:"2.5.2"
app.yaml
10 / 22
Deployed | GAE Launcher -> Dashboard
11 / 22
Live - http://hello-gae-1156.appspot.com
12 / 22
 Example - Guestbook
13 / 22
A (Minimal) Guestbook
source
<html><body>
Ananonymouspersonwrote:<blockquote>testduaduaduaduadua
<formaction="/sign"method="post">
<div><tag-textareaname="content"rows="3"cols="60"></tag-textarea
<div><inputtype="submit"value="SignGuestbook"></div>
</form>
</body></html>
 
Local
14 / 22
Admin | GAE Launcher -> SDK Console
15 / 22
Admin | GAE Launcher -> SDK Console
16 / 22
guestbook.py
importcgi
importdatetime
importwebapp2
#---------------------------------------
fromgoogle.appengine.extimportndb
fromgoogle.appengine.apiimportusers
#---------------------------------------
guestbook_key=ndb.Key('Guestbook','default_guestbook')
#---------------------------------------
classGreeting(ndb.Model):
author =ndb.UserProperty()
content=ndb.TextProperty()
date =ndb.DateTimeProperty(auto_now_add=True)
#---------------------------------------
classGuestbook(webapp2.RequestHandler):
defpost(self):
greeting=Greeting(parent=guestbook_key)
ifusers.get_current_user():
greeting.author=users.get_current_user()
greeting.content=self.request.get('content')
greeting.put()
self.redirect('/')
classMainPage(webapp2.RequestHandler):
defget(self):
self.response.out.write('<html><body>')
#---------------------------------------
greetings=ndb.gql('SELECT*'
'FROMGreeting'
'WHEREANCESTORIS:1'
'ORDERBYdateDESCLIMIT10',guestbo
#---------------------------------------
forgreetingingreetings:
ifgreeting.author:
self.response.out.write('<b>%s</b>wrote:'%greeting.
else:
self.response.out.write('Ananonymouspersonwrote:'
self.response.out.write('<blockquote>%s</blockquote>'
#---------------------------------------
self.response.out.write("""
<formaction="/sign"method="post">
<div><tag-textareaname="content"rows="3"cols="60"><
<div><inputtype="submit"value="SignGuestbook"></div
</form>
</body></html>""")
#---------------------------------------
app=webapp2.WSGIApplication([
('/',MainPage),
('/sign',Guestbook)
],debug=True)
17 / 22
application:guestbook-test-1156
version:1
runtime:python27
api_version:1
threadsafe:yes
handlers:
-url:.*
script:guestbook.app
libraries:
-name:webapp2
version:"2.5.2"
app.yaml
18 / 22
Live
19 / 22
Dashboard
20 / 22
References
1. Hello, World! in 5 minutes - Python — Google Cloud Platform
2. Introduction - Python — Google Cloud Platform
21 / 22

END
Eueung Mulyana
http://eueung.github.io/EL6240/gae
Python CodeLabs | Attribution-ShareAlike CC BY-SA
22 / 22

Google app engine python