Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Plugging into Flask

Plugging into Flask

Andy Dirnberger

December 04, 2013
Tweet

More Decks by Andy Dirnberger

Other Decks in Technology

Transcript

  1. class MyCrazyImportHook: def find_module(self, fullname, path=None): “““Return a loader if

    the module is found.””” ! def load_module(self, fullname): “““Load the module or raise ImportError.”””
  2. from flask.ext.myextension import MyExtension ! app = Flask(__name__) MyExtension(app) #

    or my_extension = MyExtension() my_extension.init_app(app)
  3. def init_app(self, app): # Provide sane defaults app.config.setdefault(‘NYC_SETTING’) = True

    ! # Clean up after yourself app.teardown_appcontext(self.teardown)
  4. from flask import _app_ctx_stack as stack ! def teardown(self, exception):

    # Get the context ctx = stack.top ! # Take actions using the context
  5. from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base

    ! Base = declarative_base() ! class User(Base): __tablename__ = ‘users’ ! id = Column(Integer, primary_key=True) name = Column(String)
  6. from flask.ext.sqlalchemy import SQLAlchemy ! db = SQLAlchemy(app) ! class

    User(db.Model): __tablename__ = ‘users’ ! id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String)
  7. from flask.ext.mail import Mail, Message ! mail = Mail(app) !

    msg = Message( ‘Hello’, sender=‘[email protected]’, recipients=[‘[email protected]’], ) ! msg.body, msg.html = ‘Hello’, ‘<b>Hello</b>’ ! mail.send(msg)