Google App Engine ã® SDK ã§
ä½ãä½ã£ã¦ã¿ããï¼
ã¨ããããã§ã Tropy ã¿ãããªãã¤ãä½ã£ã¦ã¿ã
python 㧠20 è¡ä»¥ä¸ã®ããã°ã©ã ãæ¸ãã®ã¯ãã¶ãåãã¦ã
Tropy ã¨ã¯
以ä¸ã詳ããã§ãã
Tropyとは - はてなキーワード
ã¡ãªã¿ã«ã以ä¸ã®ã¹ã¯ãªã¼ã³ã·ã§ãã㯠id:naoya ãããä½ã£ã Tropy ã®ã¯ãã¼ã³ã® Haropy ã§ãã
ã§ãåããããªæãã®ãã®ãä½ã£ã¦ã¿ã
ã½ã¼ã¹ãæãã¦ããã¾ãã
ãã¡ã¤ã«æ§æ
PyGropy |-- app.yaml |-- edit.html |-- entry.html `-- pygropy.py
app.yaml
è¨å®ãã¡ã¤ã«
application: pygropy version: 1 runtime: python api_version: 1 handlers: - url: /.* script: pygropy.py
pygropy.py
ã³ã³ããã¼ã©ã¨ã¢ãã«ã®ããã°ã©ã
import os from random import random import wsgiref.handlers from google.appengine.ext import webapp from google.appengine.ext.webapp import template from google.appengine.ext import db from pprint import pprint # models class Entry(db.Model): title = db.StringProperty (required=True) body = db.StringProperty (required=True) timestamp = db.DateTimeProperty(required=True, auto_now=True) def to_hash(self): return { 'id': self.key().id(), 'title': self.title, 'body': self.body, 'timestamp': self.timestamp, } # controllers class MainPage(webapp.RequestHandler): def get(self): redirect_random_id(self) class EntryPage(webapp.RequestHandler): def get(self): id = self.request.get('id'); if id: entry = Entry.get_by_id(int(id)) path = os.path.join(os.path.dirname(__file__), 'entry.html') self.response.out.write(template.render(path, entry.to_hash())) else: redirect_random_id(self) class EditPage(webapp.RequestHandler): def post(self): id = self.request.get('id'); title = self.request.get('title'); body = self.request.get('body'); entry = False if id: entry = Entry.get_by_id(int(id)) if not(entry): entry = Entry(title=title, body=body) else: entry.title = title; entry.body = body; id = entry.put().id() self.redirect('/entry?id=' + str(id)) def get(self): id = self.request.get('id'); entry = False if id: entry = Entry.get_by_id(int(id)) if entry: vars = entry.to_hash() else: vars = {} path = os.path.join(os.path.dirname(__file__), 'edit.html') self.response.out.write(template.render(path, vars)) def redirect_random_id(handler): count = db.GqlQuery('SELECT * FROM Entry').count(); if count == 0: handler.redirect('/edit') else: id = int(random() * count) + 1 handler.redirect('/entry?id=' + str(id)) def main(): application = webapp.WSGIApplication([('/', MainPage), ('/edit', EditPage), ('/entry', EntryPage), ], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == "__main__": main()
edit.html
ç·¨éç¨ã® html ã®ãã³ãã¬ã¼ã
<html> <head> <title>PyGropy ver.0721</title> </head> <body> <form action="/edit" method="post"> <ul> <li><input type="text" name="id" value="{{id|escape}}" /></li> <li><input type="text" name="title" value="{{title|escape}}" /></li> <li><textarea name="body">{{body|escape}}</textarea></li> <li>{{timestamp|escape}}</li> <li><input type="submit" name="_submit" value="submit" /></li> <li><a href="/">random</a></li> </ul> </form> </body> </html>
entry.html
表示ç¨ã® html ã®ãã³ãã¬ã¼ã
<html> <head> <title>PyGropy ver.0721</title> </head> <body> <ul> <li>{{id|escape}}</li> <li>{{title|escape}}</li> <li>{{body|escape}}</li> <li>{{timestamp|escape}}</li> <li><a href="/edit?id={{id|escape}}">edit</a></li> <li><a href="/">random</a></li> </ul> </body> </html>
ã¨ããããã以ä¸ã®ã³ãã³ãã§ãã¹ããµã¼ããåãã¾ãã
$ dev_appserver.py ãã£ã¬ã¯ããªå INFO 2008-04-09 00:48:48,665 appcfg.py] Checking for updates to the SDK. INFO 2008-04-09 00:48:49,392 appcfg.py] The SDK is up to date. INFO 2008-04-09 00:48:49,401 dev_appserver_main.py] Running application pygropy on port 8080: http://localhost:8080
åããã¦ã¿ã
ãã¼ããã¼ãåããã¼
ãããè¤æ°è¡å
¥ããã¨ã¨ã©ã¼ã«ãªããªãããã¶ããã¢ãã«å®ç¾©ã®ã¨ãã«åã®æå®ãééããã£ã½ãï¼ï¼
ã§ããã¾ããããã¯æ¬è³ªçãªä½æ¥ãããªãã®ã§ééããã¾ã¾ã«ãã¦ããã¾ãã
ãããã¿ã¤ã ã¹ã¿ã³ãããããããªãã¾ããããã