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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Andy Dirnberger
December 04, 2013
Technology
140
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
Bits Agent Builder の⼊⾨と活⽤事例
nulabinc
PRO
0
140
Forza Horizon 6 のテレメトリ機能で 自動運転に使えそうな学習データを集める話
henjin0
0
180
【AG-UI × A2UI × MCP Apps】Generative UIをやさしく解説する
nrinetcom
PRO
1
120
Breaking the Seal: Static Deobfuscation of Compiled V8 JavaScript Bytecode Malware
hshrzd
0
780
制約理論(ToC)入門 2026版
recruitengineers
PRO
8
2.3k
変化の早いClaude Codeを 書籍に落とし込む
oikon48
7
1.4k
Digitization部 紹介資料
sansan33
PRO
2
7.7k
Agent 時代の Kaggle 展望 / kaggle-in-the-agentic-era
upura
1
750
その“隠したつもり”が命取り ── 自前と平文をやめて「正解」に委ねる
kuroneko13
0
120
AIペネトレーションテスト・ セキュリティ検証「AgenticSec」紹介資料
laysakura
2
9k
ガバメント AI 源内を地方自治体は活用できるのか可能性と課題、期待について
takeda_h
1
410
『三匹の子ぶた』から学ぶネットワークセキュリティの昔と今 / Network Security: Then and Now Through the Lens of The Three Little Pigs
nttcom
1
6.1k
Featured
See All Featured
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
From π to Pie charts
rasagy
0
280
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Skip the Path - Find Your Career Trail
mkilby
1
180
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Documentation Writing (for coders)
carmenintech
77
5.4k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
280
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
200
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!