Skip to content

Instantly share code, notes, and snippets.

@Stumblinbear
Created October 24, 2016 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stumblinbear/969b15a4e49a06a54a01542c53d1729b to your computer and use it in GitHub Desktop.
Save Stumblinbear/969b15a4e49a06a54a01542c53d1729b to your computer and use it in GitHub Desktop.
Note: "intended" only shows what I want to happen, while "current" shows (basically) the code I'm using.
from flask import Flask
from flask import render_template_string
from flask_menu import Menu, register_menu, current_menu
app = Flask(__name__)
Menu(app=app)
def tmpl_show_menu():
return render_template_string(
"""
{%- for item in current_menu.children recursive %}
<a href="{{ item.url }}">{% if item.active %}*{% endif %}{{ item.text }}</a><br />
{% if item.children %}
<ul>
{{ loop(item.children) }}
</ul>
{% endif %}
{% endfor -%}
""")
@app.route('/')
@register_menu(app, '.', 'Home')
def index():
return tmpl_show_menu()
@app.route('/first')
@register_menu(app, '.first', 'First', order=0)
def first():
return tmpl_show_menu()
@app.route('/second')
@register_menu(app, '.second', 'Second', order=1)
def second():
return tmpl_show_menu()
@app.route('/third')
@app.route('/third/<uuid>')
@register_menu(app, '.third', 'Third', order=2)
def third(uuid):
return tmpl_show_menu()
def uuid_construct(uuid):
def wrapper():
return {'uuid': uuid}
return wrapper
third = ['a', 'b', 'c']
with app.app_context():
for uuid in third:
item = current_menu.submenu('third.' + uuid)
item.register('third', uuid, 1,
endpoint_arguments_constructor=uuid_construct(uuid))
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask
from flask import render_template_string
from flask_menu import Menu, register_menu, current_menu
app = Flask(__name__)
Menu(app=app)
def tmpl_show_menu():
return render_template_string(
"""
{%- for item in current_menu.children recursive %}
<a href="{{ item.url }}">{% if item.active %}*{% endif %}{{ item.text }}</a><br />
{% if item.children %}
<ul>
{{ loop(item.children) }}
</ul>
{% endif %}
{% endfor -%}
""")
@app.route('/')
@register_menu(app, '.', 'Home')
def index():
return tmpl_show_menu()
@app.route('/first')
@register_menu(app, '.first', 'First', order=0)
def first():
return tmpl_show_menu()
@app.route('/second')
@register_menu(app, '.second', 'Second', order=1)
def second():
return tmpl_show_menu()
@app.route('/third')
@register_menu(app, '.third', 'Third', order=2)
def third():
return tmpl_show_menu()
@app.route('/third/a')
@register_menu(app, '.third.a', 'a')
def third_a():
return tmpl_show_menu()
@app.route('/third/b')
@register_menu(app, '.third.b', 'b')
def third_b():
return tmpl_show_menu()
@app.route('/third/c')
@register_menu(app, '.third.c', 'c')
def third_c():
return tmpl_show_menu()
if __name__ == '__main__':
app.run(debug=True)
@jirikuncar
Copy link

current.py update

from flask import Flask, request
from flask import render_template_string
from flask_menu import Menu, register_menu, current_menu

app = Flask(__name__)
Menu(app=app)

def tmpl_show_menu():
    return render_template_string(
        """
        {%- for item in current_menu.children recursive %}
            <a href="{{ item.url }}">{% if item.active %}*{% endif %}{{ item.text }}</a><br />
            {% if item.children %}
                <ul>
                    {{ loop(item.children) }}
                </ul>
            {% endif %}
        {% endfor -%}
        """)

@app.route('/')
@register_menu(app, '.', 'Home')
def index():
    return tmpl_show_menu()

@app.route('/first')
@register_menu(app, '.first', 'First', order=0)
def first():
    return tmpl_show_menu()

@app.route('/second')
@register_menu(app, '.second', 'Second', order=1)
def second():
    return tmpl_show_menu()

@app.route('/third', defaults={'uuid': None})
@app.route('/third/<uuid>')
@register_menu(app, '.third', 'Third', order=2)
def third(uuid):
    return tmpl_show_menu()

third = ['a', 'b', 'c']

def build(item, uuid, order):
    item.register(
        'third', uuid, order=order,
        endpoint_arguments_constructor=lambda: {'uuid': uuid},
        active_when=lambda self: request.path.endswith(uuid))

with app.app_context():
    for order, uuid in enumerate(third):
        item = current_menu.submenu('.third.{0}'.format(uuid))
        build(item, uuid, order)

if __name__ == '__main__':
    app.run(debug=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment