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
570
Other Decks in Technology
See All in Technology
社内報はAIにやらせよう / Let AI handle the company newsletter
saka2jp
8
1.3k
AI駆動開発を推進するためにサービス開発チームで 取り組んでいること
noayaoshiro
0
250
「使い方教えて」「事例教えて」じゃもう遅い! Microsoft 365 Copilot を触り倒そう!
taichinakamura
0
320
『OCI で学ぶクラウドネイティブ 実践 × 理論ガイド』 書籍概要
oracle4engineer
PRO
3
200
ComposeではないコードをCompose化する case ビズリーチ / DroidKaigi 2025 koyasai
visional_engineering_and_design
0
100
ガバメントクラウド(AWS)へのデータ移行戦略の立て方【虎の巻】 / 20251011 Mitsutosi Matsuo
shift_evolve
PRO
2
190
Azure Well-Architected Framework入門
tomokusaba
1
350
AWS IoT 超入門 2025
hattori
0
300
「れきちず」のこれまでとこれから - 誰にでもわかりやすい歴史地図を目指して / FOSS4G 2025 Japan
hjmkth
1
270
20201008_ファインディ_品質意識を育てる役目は人かAIか___2_.pdf
findy_eventslides
2
600
Reflections of AI: A Trilogy in Four Parts (GOTO; Copenhagen 2025)
ondfisk
0
110
大規模サーバーレスAPIの堅牢性・信頼性設計 〜AWSのベストプラクティスから始まる現実的制約との向き合い方〜
maimyyym
6
4.1k
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1032
470k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.5k
Keith and Marios Guide to Fast Websites
keithpitt
411
23k
The World Runs on Bad Software
bkeepers
PRO
72
11k
Making the Leap to Tech Lead
cromwellryan
135
9.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Visualization
eitanlees
149
16k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
How to Ace a Technical Interview
jacobian
280
24k
A Modern Web Designer's Workflow
chriscoyier
697
190k
It's Worth the Effort
3n
187
28k
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!