tonyseek / flask-navigation Goto Github PK
View Code? Open in Web Editor NEWBuild navigation bars in your Flask application.
Home Page: https://pypi.python.org/pypi/Flask-Navigation
License: MIT License
Build navigation bars in your Flask application.
Home Page: https://pypi.python.org/pypi/Flask-Navigation
License: MIT License
RT
In the topic of #5 we found the automatic-naming item is a bad idea.
With the Flask reloader in development, the app context may be pushed many times. It may cause the initializer
function of NavigationBar
be executed multiple times.
The related topic is here #4
The situation is like this:
top_bar = nav.Bar('top', [
nav.Item(u'Home', 'home'),
nav.Item(u'Posts', 'post_list'),
])
@bp.route('/post/<int:post_id>')
def post(post_id):
post = Post.query.get_or_404(post_id)
return render_template('post.html', post=post)
The problem is how to let the item.is_active
be True
while accessing /post/123
. I have three solutions for it:
top_bar['post_list'].mount_views(url_prefix='/post', blueprint=bp)
top_bar['post_list'].mount_url_rule(endpoint='post')
top_bar['post_list'].mount_blueprint(bp)
@shonenada @tonicbupt What do you think about these?
Maybe we can improve inerfaces in rendering html like:
<ul>
{% for item in nav.top %}
<li class="{{ 'current' if item.is_active else '' }}">
{{ item.a(class="some_style", id="some_id") }}
</li>
{% endfor %}
</ul>
This line of code:
https://github.com/tonyseek/flask-navigation/blob/develop/flask_navigation/api.py#L48
Means that if you name the navigation object something other than nav
it won't work. Similarly, you are limited to only one navigation instance per site.
Specifically, this will work:
nav = Navigation(app)
This will not:
foo = Navigation(app)
This name should probably be configurable. Maybe Navigation(app, instance_id=...)
?
@tonyseek can suggest a way to handle conditional render of nav items based on role/permissions?
I have functioning @roles_required('role-name')
on my routes. I would like to not render a link if the User does not have permission to that route.
Thanks.
Actually it's not possible to write:
from flask import g
args_getter = lambda: {'user': g.current_user}
nav.Bar('usermenu', (
nav.Item(_('Dashboard'), 'user.dashboard', args_getter),
nav.Item(_('Profile'), 'user.profile', args_getter),
nav.Item(_('Settings'), 'user.settings', args_getter),
))
Args are evaluated on navbar creation to fill the ident but they should only be accessed on rendering to ensure app/request context is pushed.
I will try to submit a pull-request to fix this behavior but idents need to be changed.
Another solution is to provide a callable to generate url like that:
<a href="{{ item.url_for(user=current_user) }}">{{ item.label }}</a>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.