Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Plugging into Flask
Search
Andy Dirnberger
December 04, 2013
Technology
3
140
Plugging into Flask
Andy Dirnberger
December 04, 2013
Tweet
Share
More Decks by Andy Dirnberger
See All by Andy Dirnberger
A Crash Course in MongoDB
dirn
2
580
Other Decks in Technology
See All in Technology
フロントエンド刷新 4年間の軌跡
yotahada3
0
480
楽しく学ぼう!ネットワーク入門
shotashiratori
4
3.4k
複数クラスタ運用と検索の高度化:ビズリーチにおけるElastic活用事例 / ElasticON Tokyo2026
visional_engineering_and_design
0
170
It’s “Time” to use Temporal
sajikix
3
210
Claude Code のコード品質がばらつくので AI に品質保証させる仕組みを作った話 / A story about building a mechanism to have AI ensure quality, because the code quality from Claude Code was inconsistent
nrslib
12
8.5k
AI駆動AI普及活動 ~ 社内AI活用の「何から始めれば?」をAIで突破する
oracle4engineer
PRO
1
110
進化するBits AI SREと私と組織
nulabinc
PRO
1
240
システム標準化PMOから ガバメントクラウドCoEへ
techniczna
1
120
AI時代の「本当の」ハイブリッドクラウド — エージェントが実現した、あの頃の夢
ebibibi
0
130
最強のAIエージェントを諦めたら品質が上がった話 / how quality improved after giving up on the strongest AI agent
kt2mikan
0
190
VLAモデル構築のための AIロボット向け模倣学習キット
kmatsuiugo
0
240
スケールアップ企業でQA組織が機能し続けるための組織設計と仕組み〜ボトムアップとトップダウンを両輪としたアプローチ〜
tarappo
1
110
Featured
See All Featured
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
120
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
640
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
110
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
150
The Invisible Side of Design
smashingmag
302
51k
Darren the Foodie - Storyboard
khoart
PRO
3
2.9k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
130
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Transcript
Plugging into Flask Flask-NYC December 4, 2013
None
Andy Dirnberger @dirn github.com/dirn
>>> from flask.ext.sqlalchemy import SQLAlchemy
>>> from flask.ext.wtf import Form
>>> from flask.ext.mail import Mail
>>> from flask.ext import nyc
But that’s not how the code is laid out!
flask_sqlalchemy/__init__.py
flask_wtf/__init__.py
flask_mail.py
flask_nyc.py
What makes this work?
flask/ext/__init__.py
def setup(): from ..exthook import ExtensionImporter importer = ExtensionImporter(['flask_%s', 'flaskext.%s'],
__name__) importer.install() ! ! setup() del setup
PEP-302 New Import Hooks http://www.python.org/dev/peps/pep-0302/
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.”””
How to Build a Flask Extension http://flask.pocoo.org/extensions/
Pick a name
flask_myextension.py
Pick a license
• BSD (http://opensource.org/licenses/BSD-2- Clause, http://opensource.org/licenses/BSD-3- Clause) • MIT (http://opensource.org/licenses/MIT) •
WTFPL (http://www.wtfpl.net/)
Write some tests
$ python setup.py test or! $ make test
Write some docs
$ sphinx-quickstart
https://github.com/mitsuhiko/flask-sphinx-themes
sys.path.append(os.path.abspath(‘_themes’)) html_theme_path = [‘_themes’] html_theme = ‘flask’ html_theme = ‘flask_small’
Add a setup.py
install_requires=[…]
zip_safe=False
Python 2.6 and 2.7
Anatomy of an Extension
class MyExtension: def __init__(self, app=None): self.app = app if app
is not None: self.init_app(app)
from flask.ext.myextension import MyExtension ! app = Flask(__name__) MyExtension(app) #
or my_extension = MyExtension() my_extension.init_app(app)
What’s in init_app?
def init_app(self, app): # Provide sane defaults app.config.setdefault(‘NYC_SETTING’) = True
! # Clean up after yourself app.teardown_appcontext(self.teardown)
init_app should not assign self.app
from flask import current_app
from flask import _app_ctx_stack as stack ! def teardown(self, exception):
# Get the context ctx = stack.top ! # Take actions using the context
Flask-SQLAlchemy
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)
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)
Flask-WTF
from flask.ext.wtf import Form
class Form(SecureForm): “““ Flask-specific subclass of WTForms **SecureForm** class. !
”””
Flask-Mail
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)
Flask-NYC
None
Thank you!