<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mark Steve Samson on Medium]]></title>
        <description><![CDATA[Stories by Mark Steve Samson on Medium]]></description>
        <link>https://medium.com/@themarksteve?source=rss-8d7493bcdce9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nfUr_qEtRgTH5GCb.jpeg</url>
            <title>Stories by Mark Steve Samson on Medium</title>
            <link>https://medium.com/@themarksteve?source=rss-8d7493bcdce9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:37:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@themarksteve/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My Flask Pattern]]></title>
            <link>https://medium.com/@themarksteve/my-flask-pattern-aac35a3ef2ff?source=rss-8d7493bcdce9------2</link>
            <guid isPermaLink="false">https://medium.com/p/aac35a3ef2ff</guid>
            <dc:creator><![CDATA[Mark Steve Samson]]></dc:creator>
            <pubDate>Fri, 10 May 2013 10:48:22 GMT</pubDate>
            <atom:updated>2013-05-10T11:14:18.687Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been using <a href="http://flask.pocoo.org/">Flask</a> for my recent projects. It’s an awesome library — unopinionated but still provides enough convenience and good defaults. You’ll only be using the functions shown in the tutorial and examples most of the time. Only when you need to do something specific will you need to check the <a href="http://flask.pocoo.org/docs/api/">API docs</a>. But I suggest you browse through it anyways since there are quite a few stuff not mentioned in the user guide that are quite useful.</p><p>The best thing for me about Flask is that it’s more of a library than a framework. There’s no bootstrap script. You don’t need to follow a specific directory structure. You can write an app in a single module if that’s how you roll. I formed my conventions and preferences as I wrote more Flask apps. And now I’ve finally settled on a pattern after experimenting with different setups.</p><p>I start a project by creating a package the following dirs and modules:</p><pre>project/<br> static/<br> templates/<br> __init__.py<br> config.py<br> extensions.py<br> views.py<br> wsgi.py</pre><p>Let’s go through them one by one. I’ll be skipping static/, templates/ and __init__.py since they’re, well, empty.</p><h4>config.py</h4><p>It put my default settings here. I don’t put actual config values here since I include this in the VCS. It’s a good idea to document each setting too if you’re releasing it as an open source project.</p><h4>extensions.py</h4><p>Don’t reinvent the wheel. <a href="http://flask.pocoo.org/extensions/">Extensions</a> are available for common tasks such as db access, authentication and assets management.</p><pre>from flask_sqlite3 import SQLite3<br>from flask_googlelogin import GoogleLogin</pre><pre>db = SQLite3()<br>googlelogin = GoogleLogin()</pre><p>Note that I only create extension instances here. I set them up with the app later as you’ll see.</p><h4>views.py</h4><p>I don’t add routes to the main app directly. I use blueprints. Here’s an example:</p><pre>from flask import Blueprint</pre><pre>root = Blueprint(‘root’, __name__)</pre><pre>@root.route(‘/’)<br>def index():<br> return “Hello, world”</pre><p>Now if I want to add another section to my app, say an admin section, I can promote views.py to a package. I do this with <a href="https://github.com/nvie/python-fu">python-fu</a>:</p><pre>$ promote project.views</pre><p>This will move views.py to views/__init__.py. I move the root blueprint code into its own module views/root.py and import it from views/__init__.py. I can also add, let’s say, an admin blueprint.</p><pre># __init__.py<br><br>from .root import root<br>from .admin import admin</pre><pre># root.py<br><br>from flask import Blueprint</pre><pre>root = Blueprint(‘root’, __name__)</pre><pre>@root.route(‘/’)<br>def index():<br> return “Hello, world”</pre><pre># admin.py<br><br>from flask import Blueprint</pre><pre>admin = Blueprint(‘root’, __name__)</pre><pre>@admin.route(‘/’)<br>def index():<br> return “Admin”</pre><p>Notice I still haven’t associated the config and routes to the app itself. I haven’t even defined the app yet! Time for the last module.</p><h4>wsgi.py</h4><p>I chose to name it like this because most WSGI servers use this convention. This is where I put the main Flask app, set up extensions and register the blueprints.</p><pre>from flask import Flask</pre><pre>from .extensions import db, googlelogin<br>from .views import root, admin</pre><pre>app = Flask(__name__, instance_relative_config=True)<br>app.config.from_object(‘project.config’)<br>app.config.from_pyfile(‘application.cfg’, silent=True)</pre><pre>db.init_app(app)<br>googlelogin.init_app(app)</pre><pre>app.register_blueprint(root)<br>app.register_blueprint(admin, url_prefix=’/admin’)</pre><pre>application = app</pre><p>You must have noticed I toggled instance_relative_config to True. This is my preferred way of handling configuration files. You can know more about how instance folder works in the <a href="http://flask.pocoo.org/docs/config/#instance-folders">docs</a>.</p><p>There are other modules you might want to add like models.py and helpers.py. Also, aside from the package modules, it’s a good idea to have requirements.txt and/or setup.py to handle your dependencies. Finally, for local testing I usually have a runserver.py script.</p><pre>if __name__ == ‘__main__’:<br> from project.wsgi import app<br> app.run(debug=True)</pre><p>So that’s how I set up my Flask projects. Next time I’ll write about how I manage and serve WSGI apps with <a href="http://circus.readthedocs.org/">Circus</a> and <a href="http://nginx.org/">Nginx</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aac35a3ef2ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>