Upload
eueung-mulyana
View
299
Download
1
Embed Size (px)
Citation preview
GAE - PythonEueung Mulyana
http://eueung.github.io/EL6240/gaePython 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
import webapp2#----------------------------------------class MainHandler(webapp2.RequestHandler): def get(self): self.response.write('Hello world!')#----------------------------------------app = webapp2.WSGIApplication([ ('/', MainHandler)], debug=True)
Generated Structure/Codes
application: helloversion: 1runtime: python27api_version: 1threadsafe: 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 Project7 / 22
https://console.cloud.google.com/home/dashboard8 / 22
GAE Launcher -> Dashboard9 / 22
Uploading
GAE Launcher -> Deploy
application: hello-gae-1156version: 1runtime: python27api_version: 1threadsafe: 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 -> Dashboard11 / 22
Live - http://hello-gae-1156.appspot.com12 / 22
Example - Guestbook
13 / 22
A (Minimal) Guestbook
source
<html><body>An anonymous person wrote:<blockquote>test dua dua dua dua dua
<form action="/sign" method="post"> <div><tag-textarea name="content" rows="3" cols="60"></tag-textarea <div><input type="submit" value="Sign Guestbook"></div></form></body></html>
Local
14 / 22
Admin | GAE Launcher -> SDK Console
15 / 22
Admin | GAE Launcher -> SDK Console
16 / 22
guestbook.py
import cgiimport datetimeimport webapp2#---------------------------------------from google.appengine.ext import ndbfrom google.appengine.api import users#---------------------------------------guestbook_key = ndb.Key('Guestbook', 'default_guestbook')#---------------------------------------class Greeting(ndb.Model): author = ndb.UserProperty() content = ndb.TextProperty() date = ndb.DateTimeProperty(auto_now_add=True)#---------------------------------------class Guestbook(webapp2.RequestHandler): def post(self): greeting = Greeting(parent=guestbook_key)
if users.get_current_user(): greeting.author = users.get_current_user()
greeting.content = self.request.get('content') greeting.put() self.redirect('/')
class MainPage(webapp2.RequestHandler): def get(self): self.response.out.write('<html><body>') #--------------------------------------- greetings = ndb.gql('SELECT * ' 'FROM Greeting ' 'WHERE ANCESTOR IS :1 ' 'ORDER BY date DESC LIMIT 10', guestbook_key) #--------------------------------------- for greeting in greetings: if greeting.author: self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname()) else: self.response.out.write('An anonymous person wrote:' self.response.out.write('<blockquote>%s</blockquote>' #--------------------------------------- self.response.out.write(""" <form action="/sign" method="post"> <div><tag-textarea name="content" rows="3" cols="60"></tag-textarea></div> <div><input type="submit" value="Sign Guestbook"></div> </form> </body></html>""")#---------------------------------------app = webapp2.WSGIApplication([ ('/', MainPage), ('/sign', Guestbook)], debug=True)
17 / 22
application: guestbook-test-1156version: 1runtime: python27api_version: 1threadsafe: yes
handlers:- url: .* script: guestbook.app
libraries:- name: webapp2 version: "2.5.2"
app.yaml
18 / 22
Live19 / 22
Dashboard20 / 22
References1. Hello, World! in 5 minutes - Python — Google Cloud Platform2. Introduction - Python — Google Cloud Platform
21 / 22
ENDEueung Mulyana
http://eueung.github.io/EL6240/gaePython CodeLabs | Attribution-ShareAlike CC BY-SA
22 / 22