Flask-LazyViews

Registering URL routes for Flask app and blueprints in lazy way
Download

Flask-LazyViews Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Igor Davydenko
  • Publisher web site:
  • http://github.com/playpauseandstop/

Flask-LazyViews Tags


Flask-LazyViews Description

Flask-LazyViews is a Flask extension that allows registering URL routes for your Flask app or blueprint in lazy way.Installation pip install Flask-LazyViewsUsageFor applicationproject/app.pyfrom flask import Flaskfrom flask.ext.lazyviews import LazyViewsapp = Flask(__name__)views = LazyViews(app)views.add('/', 'views.home')views.add('/page/< int:page >', 'views.page')project/views.pyfrom flask import render_templatedef home(): return render_template('home.html')def page(page_id): page = get_page(page_id) return render_template('page.html', page=page)For blueprintproject/app.py...from project.test import blueprint as test_blueprint...app.register_blueprint(test_blueprint, url_prefix='/test')project/test/__init__.pyfrom flask import Blueprintfrom flask.ext.lazyviews import LazyViewsblueprint = Blueprint('test', __name__)views = LazyViews(blueprint, '.views')views.add('/', 'test')views.add('/advanced', 'advanced_test', methods=('GET', 'POST'))project/test/views.pyfrom flask import render_template, requestdef advanced_test(): context = generate_context(request.form) return render_template('test/advanced.html', **context)def test(): return render_template('test/test.html')ExplanationsThe 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'))ImportantBe careful with import_prefix value if you used __name__ as Flask application name or blueprint import_name. Setting relative path could cause server errors.Bugs, feature requests?If you found some bug in Flask-LazyViews library, please, add new issue to the project's GitHub issues.Product's homepage


Flask-LazyViews Related Software