Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Plugging into Flask
Search
Andy Dirnberger
December 04, 2013
Technology
150
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Plugging into Flask
Andy Dirnberger
December 04, 2013
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
AI駆動開発の時代になったのでトヨタ生産方式から見直す
terurou
7
6k
AI時代のPRレビューにどう向き合うか?
kairim0
1
240
初海外で注意すべきこと12選!!!安全快適なre:invent生活にするために
yuuuuuuu168
1
130
AIにシークレットを見せない、WSL2で安全な開発環境
yazm
0
280
TinyGoでオブザーバビリティ?OpenTelemetryはTinyGoで使えるのか / Is OpenTelemetry available in TinyGo environment?
ymotongpoo
2
140
Active Record Connection Adaptersにおける長年の課題に再び取り組む
yahonda
0
130
ブラウザとサーバーから考えるAI時代のフロントエンド監視
shogo4131
3
920
Browser Spec: Testing Hotwire with Ruby
takahashim
1
110
スプシの手作業からの解脱 ~更新はClaude Codeに、見せ方と使い方はGASアプリに任せる~
kentarofujii
0
150
USB PDを使ってスマホを労わりたい
raspython3
0
170
Code for Security プロジェクト紹介 2026年10月
yu23ki14
0
100
Webの常識が通じないHTMLメールのダークモード
teamlab
PRO
0
120
Featured
See All Featured
Writing Fast Ruby
sferik
630
63k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
890
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
Prompt Engineering for Job Search
mfonobong
0
510
Thoughts on Productivity
jonyablonski
76
5.4k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
930
Testing 201, or: Great Expectations
jmmastey
46
8.3k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
870
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
540
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!