Flask-Menu
raw JSON → 2.0.0 verified Fri May 01 auth: no python
Flask-Menu is a Flask extension that adds support for generating menus and navigation bars. Version 2.0.0 supports Python >=3.7, with a stable release cadence. It provides decorators and template helpers to define menus hierarchically and render them in templates.
pip install flask-menu Common errors
error ImportError: cannot import name 'Menu' from 'flask.ext.menu' ↓
cause Using the deprecated Flask-Ext import style.
fix
Use
from flask_menu import Menu instead. error RuntimeError: The menu entry '...' is not registered because the endpoint '...' does not exist. ↓
cause The decorator `@register_menu` is applied after the route decorator, or the view function is not defined.
fix
Ensure
@register_menu is placed directly above @app.route and that the endpoint matches the route's endpoint. Warnings
breaking Version 2.0.0 removed the `Menu` class's `register_menu` method and uses the decorator `@register_menu` directly on view functions. ↓
fix Use the `register_menu` decorator instead of `Menu.register_menu`.
breaking The `active` key in the menu entry's attributes is no longer automatically set by the extension; you must use Jinja2 extensions to check current endpoint. ↓
fix Use `{% active_menu %}` or `current_menu` in templates to highlight active items.
gotcha The `register_menu` decorator must be placed above the route decorator, otherwise the endpoint might not be registered. ↓
fix Apply `@register_menu` before `@app.route`.
Imports
- Menu wrong
from flask.ext.menu import Menucorrectfrom flask_menu import Menu - register_menu
from flask_menu import register_menu
Quickstart
from flask import Flask
from flask_menu import Menu, register_menu
app = Flask(__name__)
Menu(app)
@app.route('/')
@register_menu(app, '.index', 'Home', order=0)
def index():
return 'Hello'
if __name__ == '__main__':
app.run()