Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What The Flask? and how to use it with some Google APIs

2,953 views

Published on

Tech Talk for Google Developer Bus Brasil 2013

Published in: Technology, News & Politics
  • Be the first to comment

What The Flask? and how to use it with some Google APIs

  1. 1. + Google
  2. 2. Who? CADEMY Bruno Rocha bruno@rocha.com http://github.com/rochacbruno http://twitter.com.rochacbruno http://brunorocha.org http://pythonhub.com www.quokkaproject.org
  3. 3. Full Stack Framework Faz as escolhas por você e oferece uma plataforma completa Micro Framework Define apenas o básico (WSGI, request, response, session etc) (ORM, Templates, Organização de arquivos, arquivos de configurações, etc) + Fácil - controle Crescimento gradativo + trabalhoso + controle
  4. 4. What The Flask? Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.
  5. 5. Werkzeug - WSGI library from werkzeug.serving import run_simple from werkzeug.wrappers import Request, Response @Request.application def application(request): return Response('Hello World!') run_simple('localhost', 4000, application)
  6. 6. Jinja - Template engine {% macro render_user(user) %} <li><a href="{{ user.url }}">{{ user.username }}</a></li> {% endmacro %} {% from _above_file.html import render_user %} <title>{% block title %}{% endblock %}</title> <ul> {% for user in users %} {{ render_user(user) }} {% endfor %} </ul>
  7. 7. Good intentions. # thread locals from flask import request, session, g # extensions from flask.ext import AmazingExtension # blueprints from flask import Blueprint blog = Blueprint(“blog_blueprint”) blog.template_folder = “path/to/folder” blog.static_folder = “path/to/static” # Application factory app = create_app(**kwargs) app.register_blueprint(blog) AmazingExtension(app)
  8. 8. It is not a framework, it is a pattern! Good intentions flask.ext.* your_app.py
  9. 9. your_app.py $ pip install flask, flask-security, flask-admin, xpto-orm from from from from from flask import Flask flask.ext.security import Security flask.ext.admin import Admin somewhere.db.models import UserDatastore somewhere.views import indexpage def create_app(**config): app = Flask(“myapp”) app.config_from_object(config) Admin(app) Security(app, UserDatastore) app.add_url_rule(“/index/<something>”, view_func=indexpage) return app if __name__ == “__main__”: app = create_app(SECRET_KEY=”XYZ”) app.run()
  10. 10. Blueprints Um Blueprint funciona de forma similar a um objeto Flask, mas na verdade não é uma aplicação, mas sim um projeto de como construir ou extender uma aplicação from flask import Blueprint, render_template blog_extension = Blueprint(“my_blog_extension”) blog_extension.endpoint = “/blog” blog_extension.template_folder = “path/to/blog_templates” blog_extension.static_folder = “path/to/blog_static” @blog_extension.route(“/index”) def blog(): posts = some_db_or_orm.posts.query() return render_template(“blog.html”, posts=posts) blog_blueprint.py from blog_blueprint import blog_extension your_app.py def create_app(**config): app = Flask(“myapp”) ... app.register_blueprint(blog_extension) return app
  11. 11. ● ● Flask subclass ○ class MyOwnFlask(Flask): pass application factory ○ app = create_app(**config) ○ evitar import circular ● Blueprints ○ Mesmo que seja uma one-page-app ● Flask-Admin ○ Modular, insira qualquer view no admin, crud completo, actions, filters ● Flask-Security ○ Login, Logout, Lembrar senha, Register, Access control, permissions ● Flask-script ○ python manage.py faça_me_um_sanduiche ● app.config_from_envvar ○ Settings desacoplado da app ○ export APP_SETTINGS=”/path/to/settings.cfg” ○ app.config_from_envvar(“APP_SETTINGS”)
  12. 12. and Google ?
  13. 13. http://bit.ly/flask-gae # app.yaml application : nome_do_app version : 1 runtime : python27 api_version : 1 threadsafe: true libraries: - name: jinja2 version: "2.6" - name: markupsafe version: "0.15" handlers: - url: /static static_dir : your_app_folder/static - url: .* script : main.app # your_app.py from flask import Flask def create_app(): app = Flask(__name__) return app # main.py from werkzeug import DebuggedApplication from your_app import create_app app = DebuggedApplication(create_app())
  14. 14. https://github.com/sashka/flask-googleauth # your_app.py from flask import Flask from flask.ext.googleauth import GoogleFederated from .configurator import register_views def create_app(): app = Flask(__name__) app.secret_key = “XYZ” auth = GoogleFederated("dominio.com", app) register_views(app, auth) return app # configurator.py from .views import myview, …, ... def register_views(app, auth) secret = auth.required(myview) app.add_url_rule(“/secret/”, view_func=secret) … ... # views.py from flask import g def myview() return "Logged user, %s (%s)" % (g.user.name, g.user.email) … ...
  15. 15. github.com/rochacbruno/Flask-GoogleMaps pip install flask-googlemaps from flask import Flask from flask.ext.googlemaps import GoogleMaps app = Flask(__name__) GoogleMaps(app) <div> {{googlemap("my_awesome_map", 0.23234234, -0.234234234, markers=[(0.12, -0.45345), ...])}} </div>
  16. 16. Thank you! Bruno Rocha http://brunorocha.org bruno@rocha.com http://github.com/rochacbruno http://twitter.com.rochacbruno http://pythonhub.com www.quokkaproject.org

×