Skip to main content

Registering url routes for Flask app and blueprints in lazy way.

Project description

https://secure.travis-ci.org/playpauseandstop/Flask-LazyViews.png?branch=master https://pypip.in/v/Flask-LazyViews/badge.png

Registering url routes for your Flask app or blueprint in lazy way :)

Based on original snippet from Flask documentation!

Requirements

Installation

# pip install Flask-LazyViews

License

Flask-LazyViews is licensed under the BSD License.

Usage

For application

project/app.py

from flask import Flask
from flask.ext.lazyviews import LazyViews


app = Flask(__name__)
views = LazyViews(app)

views.add('/', 'views.home')
views.add('/page/<int:page>', 'views.page')

project/views.py

from flask import render_template


def home():
    return render_template('home.html')


def page(page_id):
    page = get_page(page_id)
    return render_template('page.html', page=page)

For blueprint

project/app.py

...

from project.test import blueprint as test_blueprint

...

app.register_blueprint(test_blueprint, url_prefix='/test')

project/test/__init__.py

from flask import Blueprint
from flask.ext.lazyviews import LazyViews


blueprint = Blueprint('test', __name__)
views = LazyViews(blueprint, '.views')

views.add('/', 'test')
views.add('/advanced', 'advanced_test', methods=('GET', 'POST'))

project/test/views.py

from flask import render_template, request


def advanced_test():
    context = generate_context(request.form)
    return render_template('test/advanced.html', **context)


def test():
    return render_template('test/test.html')

Explanations

The main point of Flask-LazyViews is simplifying process of adding views to the app and blueprint using lazy technique from Flask documentation.

Also the next goal is simplifying viewname definition. For most cases our views functions placed in .views module of app or blueprint, so we don’t need to input full path to that module.

This especially useful for blueprints. Let see the example above, if we using original snippet - we’ll need to provide path to blueprint’s views module:

add_url(blueprint, '/', 'test.views.test')

but with Flask-LazyViews we could to ignore test.

From other side if your view functions placed in some other location or you need to provide full path to its - you still could do this.

Also you could setup import_prefix like done in Django’s patterns:

views = LazyViews(app, 'views')
views.add('/', 'home')
views.add('/page/<int:id>', 'page', methods=('GET', 'POST'))

Important

Be careful with import_prefix value if you used __name__ as Flask application name or blueprint import_name. Setting relative path could cause server errors.

Other methods

add_admin

Add admin view if Flask-Admin extension is added to Flask application.

admin = Admin(app)
views = LazyViews(app, 'views')
views.add_admin('PageAdmin', name='Page Admin')

add_error

Add error handler to Flask application or blueprint, e.g.:

views = LazyViews(app, 'views')
views.add_error(404, 'error')
views.add_error(500, server_error_view)

add_static

Add custom URLs for serving static files. It useful when you want handle some static files outside static_url, e.g.:

views = LazyViews(app)
views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'})

Bugs, feature requests?

If you found some bug in Flask-LazyViews library, please, add new issue to the project’s GitHub issues.

ChangeLog

0.5.1 (Jan 31, 2014)

  • Fixes #3. Make LazyView proxy class lazy again. Fix circullar imports and working outside application context

0.5 (Dec 27, 2013)

  • Python 3 support (only for Flask 0.10+)

  • Flask 0.10+ support

  • Fixes #2. Access view function documentation and repr while loading views via strings

0.4 (Oct 28, 2012)

  • Add support of adding admin views to Flask applications via add_admin method.

  • Configure Travis CI support.

0.3 (Oct 4, 2012)

  • Implement add_error shortcut method for adding custom error handling for Flask application or blueprint.

0.2 (Sep 17, 2012)

  • Implement init_app and init_blueprint methods for configuring LazyViews instance outside main application module or for multiple applications.

  • Add add_static shortcut method for adding custom URL rules for serving static files.

  • Add ability to register real view functions with LazyViews instance.

0.1 (Apr 3, 2012)

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-LazyViews-0.5.1.tar.gz (7.0 kB view details)

Uploaded Source

File details

Details for the file Flask-LazyViews-0.5.1.tar.gz.

File metadata

File hashes

Hashes for Flask-LazyViews-0.5.1.tar.gz
Algorithm Hash digest
SHA256 21be8edbd44766f82c8d45b951cdb2e2d93276af4a0c99fc5e8309f4f5b10241
MD5 55d16cb3b6128f29afcc8efefb71ff12
BLAKE2b-256 cabf5cfb9ca86ca4b480385f3905cb85879803a67f21bbff378847e9b84533d9

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page