Flask-Menu is a Flask extension that adds support for generating menus.
Flask-Menu is on PyPI so all you need is:
$ pip install Flask-Menu
The development version can be downloaded from its page at GitHub.
$ git clone https://github.com/inveniosoftware/flask-menu.git
$ cd flask-menu
$ python setup.py develop
$ ./run-tests.sh
This guide assumes that you have successfully installed Flask-Menu package already. If not, please follow the Installation instructions first.
Here is a simple Flask-Menu usage example:
from flask import Flask
from flask import render_template_string
from flask.ext import menu
app = Flask(__name__)
menu.Menu(app=app)
def tmpl_show_menu():
return render_template_string(
"""
{%- for item in current_menu.children %}
{% if item.active %}*{% endif %}{{ item.text }}
{% endfor -%}
""")
@app.route('/')
@menu.register_menu(app, '.', 'Home')
def index():
return tmpl_show_menu()
@app.route('/first')
@menu.register_menu(app, '.first', 'First', order=0)
def first():
return tmpl_show_menu()
@app.route('/second')
@menu.register_menu(app, '.second', 'Second', order=1)
def second():
return tmpl_show_menu()
if __name__ == '__main__':
app.run(debug=True)
If you save the above as app.py, you can run the example application using your Python interpreter:
$ python app.py
* Running on http://127.0.0.1:5000/
and you can observe generated menu on the example pages:
$ firefox http://127.0.0.1:5000/
$ firefox http://127.0.0.1:5000/first
$ firefox http://127.0.0.1:5000/second
You should now be able to emulate this example in your own Flask applications. For more information, please read the Templating guide, the Blueprint Support guide, and peruse the API.
By default, a proxy object to current_menu is added to your Jinja2 context as current_menu to help you with creating navigation bar. For example:
<ul>
{%- for item in current_menu.children recursive -%}
<li>
<a href="{{ item.url}}">{{ item.text }}</a>
{%- if item.children -%}
<ul>
{{ loop(item.children) }}
</ul>
{%- endif -%}
</li>
{%- endfor -%}
</ul>
The most import part of an modular Flask application is Blueprint. You can create one for your application somewhere in your code and decorate your view function, like this:
from flask import Blueprint
from flask.ext import menu
bp_account = Blueprint('account', __name__, url_prefix='/account')
@bp_account.route('/')
@menu.register_menu(bp_account, '.account', 'Your account')
def index():
pass
Sometimes you want to combine multiple blueprints and organize the navigation to certain hierarchy.
from flask import Blueprint
from flask.ext import menu
bp_social = Blueprint('social', __name__, url_prefix='/social')
@bp_account.route('/list')
@menu.register_menu(bp_social, '.account.list', 'Social networks')
def list():
pass
As a result of this, your current_menu object will contain a list with 3 items while processing a request for /social/list.
>>> from example import app
>>> from flask.ext.menu import current_menu
>>> import account
>>> import social
>>> app.register_blueprint(account.bp_account)
>>> app.register_blueprint(social.bp_social)
>>> with app.test_client() as c:
... c.get('/social/list')
... assert current_menu.submenu('account.list').active
... current_menu.children
If you are looking for information on a specific function, class or method, this part of the documentation is for you.
Flask extension implementation.
Initialize a Flask application.
Return a root entry of current application’s menu.
Represent a entry node in the menu tree.
Provides information for displaying links (text, url, visible, active). Navigate the hierarchy using children() and submenu().
Return True if the menu item is active.
Return list of sorted children.
Return list from dynamic list constructor.
Return True if the menu has an active child.
Make the entry always hidden.
Return all items on path between two specified entries.
Only if one of them is an ancestor of the other.
Parameters: |
|
---|---|
Returns: | List of entries between those items or None if they are on different branches. |
Assign endpoint and display values.
Return submenu placed at the given path in the hierarchy.
If it does not exist, a new one is created. Return None if path string is invalid.
Parameters: |
|
---|---|
Returns: | Submenu placed at the given path in the hierarchy. |
Generate url from given endpoint and optional dynamic arguments.
Return True if the menu item is visible.
Decorate endpoints that should be displayed in a menu.
Example:
@register_menu(app, '.', _('Home'))
def index():
pass
Parameters: |
|
---|
Changed in version 0.2.0: The kwargs arguments.
Version 0.3.0 (released 2015-03-17)
Version 0.2.0 (released 2014-11-04)
Version 0.1.0 (released 2014-06-27)
Bug reports, feature requests, and other contributions are welcome. If you find a demonstrable problem that is caused by the code of this library, please:
If you create a feature branch, you can run the tests to ensure everything is operating correctly:
$ ./run-tests.sh
Flask-Menu is free software; you can redistribute it and/or modify it under the terms of the Revised BSD License quoted below.
Copyright (C) 2013, 2014 CERN.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction.
Flask-Menu is developed for use in Invenio digital library software.
Contact us at info@invenio-software.org