On Github merqurio / flask-on-appengine
Gabi de Maeztu http://merqur.io
from flask import Flask app = Flask(__name__) @app.route("/") @app.route("/home/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
Used to render the HTML content of the page that the user ultimately sees
from flask import render_template @app.route('/hello/') @app.route('/hello/< name>') def hello(name=None): return render_template('hello.html', name=name)
Hello from Flask {% if name %}
$ pip install flask
Even nicer if we use virtualenv, so our libraries do not mess up
$ mkdir flask $ virtualenv flask/ New python executable in flask/bin/python3.4 Also creating executable in flask/bin/python Installing setuptools, pip, wheel...done. $ . flask/bin/activate (flask)$ cd flask/ (flask)$ pip install flask
# import the framework from flask import Flask # create an instance app = Flask(__name__) # define a route and a 'controller' @app.route("/") def hello(): return "Hello World!" # if runing this script, start up dev server if __name__ == "__main__": app.run()
$ python hello.py * Running on http://localhost:8000/
$ curl https://sdk.cloud.google.com | bash or $ docker pull google/cloud-sdk
$ git clone https://github.com/GoogleCloudPlatform/appengine-python-flask-skeleton $ cd appengine-python-flask-skeleton $ pip install -r requirements.txt -t lib
application: flask-pybcn #your app id here version: 1 runtime: python27 api_version: 1 threadsafe: yes - url: .* # This regex directs all routes to main.app script: main.app
# Import the Flask Framework from flask import Flask app = Flask(__name__) # We don't need to call run() since our application is embedded within # the App Engine WSGI application server. @app.route('/') def hello(): return 'Hello World!'
$ dev_appserver.py . # Run's a server at localhost:8080 $ appcfg.py -A project-id update . # Deploy to App Engine
Available at project-id.appspot.com
from google.appengine.ext import ndb class Publication(ndb.Model): """A publication object.""" date = ndb.DateTimeProperty(auto_now_add=True) title = ndb.StringProperty(required=True) pages = ndb.IntProperty() year = ndb.DateTimeProperty() is_book = ndb.BooleanProperty()
little_prince = Publication(title='The Little Prince') little_prince.pages = 112 little_prince.year = datetime.date(1943, 04, 06) little_prince.is_book = True little_prince.put()
@app.route('/') def index(): return render_template('index.html')
from models import Publication @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': little_prince = Publication() little_prince.title = request.form['title'] little_prince.put() return render_template('index.html')
@app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': little_prince = Publication() little_prince.title = request.form['title'] little_prince.put() all_pub = Publication.query().fetch() return render_template('index.html', pubs=all_pub)
{% for publication in pubs %}
Flask Google App Engine Explore Flask Follow me ! @gabimaeztu