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
Context Engineeringが企業で不可欠になる理由
hirosatogamo
PRO
3
390
【インシデント入門】サイバー攻撃を受けた現場って何してるの?
shumei_ito
0
1.5k
Bill One急成長の舞台裏 開発組織が直面した失敗と教訓
sansantech
PRO
1
290
We Built for Predictability; The Workloads Didn’t Care
stahnma
0
130
コスト削減から「セキュリティと利便性」を担うプラットフォームへ
sansantech
PRO
3
1.3k
What happened to RubyGems and what can we learn?
mikemcquaid
0
240
Mosaic AI Gatewayでコーディングエージェントを配るための運用Tips / JEDAI 2026 新春 Meetup! AIコーディング特集
genda
0
150
All About Sansan – for New Global Engineers
sansan33
PRO
1
1.3k
GitHub Issue Templates + Coding Agentで簡単みんなでIaC/Easy IaC for Everyone with GitHub Issue Templates + Coding Agent
aeonpeople
1
170
SREのプラクティスを用いた3領域同時 マネジメントへの挑戦 〜SRE・情シス・セキュリティを統合した チーム運営術〜
coconala_engineer
2
590
月間数億レコードのアクセスログ基盤を無停止・低コストでAWS移行せよ!アプリケーションエンジニアのSREチャレンジ💪
miyamu
0
800
予期せぬコストの急増を障害のように扱う――「コスト版ポストモーテム」の導入とその後の改善
muziyoshiz
1
1.5k
Featured
See All Featured
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
Making Projects Easy
brettharned
120
6.6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Embracing the Ebb and Flow
colly
88
5k
Building the Perfect Custom Keyboard
takai
2
680
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.3k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
66
36k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
220
A Modern Web Designer's Workflow
chriscoyier
698
190k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
280
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!