Simple single file application as a starter

In context of MVP is important to ensure that style of development and chosen framework provides wide abilities for growth without loosing maintainability. Usually if framework allows you to build a single file app it means that this framework is quite easy to use, understand and it may be quite flexible and explicit.

Flask

Example from the docs:

from flask import Flask
app = Flask(__name__)

@app.route('/user/')
def show_user_profile(username):
    return 'User %s' % username

@app.route('/post/', method='POST')
def show_post(post_id):
    assert request.path.startswith('/post')
    assert request.method == 'POST'
    return 'Post %d' % post_id

if __name__ == "__main__":
    app.run()

Overall impression:

  • App instance has a built-in server
  • URL binded directly to views
  • You should have app object instantiated before declaring views (*there is a way to declare view without instance of app, but it doesn't seems to be a recommended approach by flask?)
  • Allows to response with string, the response logic is hidden

Pyramid

Example code from the docs:

from pyramid.config import Configurator
from pyramid.view import view_config
from pyramid.response import Response
from wsgiref.simple_server import make_server

@view_config(route_name='user')
def show_user_profile(request):
    return Response('Hello %(name)s!' % request.matchdict)

@view_config(route_name='post', method='POST')
def show_post(post_id):
    assert request.path.startswith('/post')
    assert request.method == 'POST'
    return Response('Post %(post_id)s!' % request.matchdict)

def app():
    config = Configurator()
    config.add_route('user', '/user/{name}')
    config.add_route('post', '/post/{post_id}')
    return config.make_wsgi_app()

if __name__ == '__main__':
 server = make_server('0.0.0.0', 8080, app())
 server.serve_forever()

Overall impression:

  • sever is not included in app
  • app build from config
  • views are bind to routes
  • routes explicitly added to config
  • views return response objects

Django

There is no such thing as single file app in Django, framework heavily depends on project file structure due to hidden module loading mechanisms and from the start provides you with quite complicated structure.

Afterword

In most cases to start a project as single file application is a kind of bad approach. The cost of refactoring, maintenance and adding new features will grow exponentially with growth of the application. Following recommendations could help reduce the cost:

  • Start with the whole project structure in mind, split it in modules from the very beginning
  • When you have more then 400 lines in module in most cases it means that you need to split it
  • If your class has more than 100 lines probably you should split it and use composition to achieve functionality
  • Don't use global variables
  • Use linters and cover app with basic functional tests from the very start

Usually framework provides a starter template with basic project structure which nicely splits everything into modules so use it.

There is a couple of cases where single file apps is quite useful:

  • developing and testing case, where app would have a role of a mocks
  • simple support tools for your app while its in development
Prev Post Next Post