22
GAE - Python Eueung Mulyana http://eueung.github.io/EL6240/gae Python CodeLabs | Attribution-ShareAlike CC BY-SA 1 / 22

Google app engine python

Embed Size (px)

Citation preview

Page 1: Google app engine   python

GAE - PythonEueung Mulyana

http://eueung.github.io/EL6240/gaePython CodeLabs | Attribution-ShareAlike CC BY-SA

1 / 22

Page 2: Google app engine   python

Agenda

GAE Basics

Example - Guestbook

2 / 22

Page 3: Google app engine   python

GAE Python Basics

3 / 22

Page 4: Google app engine   python

Getting Started 

GAE Launcher : Add New Application

GAE Launcher - Idle vs. Running App

4 / 22

Page 5: Google app engine   python

GAE Launcher -> Browse GAE Launcher -> SDK Console

5 / 22

Page 6: Google app engine   python

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

Page 7: Google app engine   python

https://console.cloud.google.com -> Create Project7 / 22

Page 8: Google app engine   python

https://console.cloud.google.com/home/dashboard8 / 22

Page 9: Google app engine   python

GAE Launcher -> Dashboard9 / 22

Page 10: Google app engine   python

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

Page 11: Google app engine   python

Deployed | GAE Launcher -> Dashboard11 / 22

Page 12: Google app engine   python

Live - http://hello-gae-1156.appspot.com12 / 22

Page 13: Google app engine   python

Example - Guestbook

13 / 22

Page 14: Google app engine   python

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

Page 15: Google app engine   python

Admin | GAE Launcher -> SDK Console

15 / 22

Page 16: Google app engine   python

Admin | GAE Launcher -> SDK Console

16 / 22

Page 17: Google app engine   python

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

Page 18: Google app engine   python

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

Page 19: Google app engine   python

Live19 / 22

Page 20: Google app engine   python

Dashboard20 / 22

Page 21: Google app engine   python

References1. Hello, World! in 5 minutes - Python — Google Cloud Platform2. Introduction - Python — Google Cloud Platform

21 / 22

Page 22: Google app engine   python

ENDEueung Mulyana

http://eueung.github.io/EL6240/gaePython CodeLabs | Attribution-ShareAlike CC BY-SA

22 / 22