flask - jinja2 template takes over 10 secs to render, how to optimize? -


in flask web app, there route display either member first name starting 'a, b, c, etc..' or show button shows members registered . right sum 750 users. problem displaying 'show all' list takes on 10 seconds. request db fast, it's rendering takes seconds. wondering if there way speed up? i'm pretty new python, flask , jinja2 don't know optimiztion paths yet.

here views.py, when click 'show all" button on webpage, calls url letter argument set '0':

@main.route('/all-members') @login_required @admin_required def all_members():     alphabet = list(string.ascii_lowercase)     first_letter = request.args.get('letter')     if first_letter == '0':         user_ = user.query.order_by(user.fullname.asc()).all()     else:         user_ = user.query.filter(user.username.startswith(first_letter)).order_by(user.fullname.asc()).all()     return render_template('all_members.html', all_members = user_, alphabet = alphabet) 

it return 750 user objects. pass result template:

    return render_template('all_members.html', all_members = user_, alphabet = alphabet) 

and template:

{% extends "base.html" %}  {% block title %}zata - members list{% endblock %}  {% block page_content %} <div class="page-header" xmlns="http://www.w3.org/1999/html">     <h1>tribe members list</h1> <p></p> {% letter in alphabet %}  <a class="btn btn-success" href="{{ url_for('.all_members', letter = letter) }}">{{ letter }}</a> {% endfor %} <a class="btn btn-info" href="{{ url_for('.all_members', letter = 0) }}">show all</a> </div> {% member in all_members %}      <div class="row"> <p>         <div class="col-md-2">             <a href="{{ url_for('main.member',username=member.username) }}">{{ member.fullname }}</a>         </div>         <div class="col-md-1">             <a class="btn btn-warning btn-xs" href="{{ url_for('.edit_profile_admin', id=member.id) }}"> edit profile</a>         </div>         <div class="col-md-3">             <a class="btn btn-info btn-xs" href="{{ url_for('.add_punchcard', id=member.id, next=request.url) }}"> manage punchcard</a>             {% if member.punchcard_gold %}<span class="btn btn-xs"  style="background-color:gold">g:{{ member.punchcard_gold }}</span>{% endif %}             {% if member.punchcard_normal %}<span class="btn btn-xs"  style="background-color:#c0c0c0">r:{{ member.punchcard_normal }}</span>{% endif %}         </div>         <div class="col-md-4">             <a class="btn btn-info btn-xs" href="{{ url_for('.add_seasonpass', id=member.id, next=request.url) }}"> manage season pass</a>             {% if member.season_pass_gold %}<span class="btn btn-xs"  style="background-color:gold">g:{{ member.season_pass_gold }}</span>{% endif %}             {% if member.season_pass_normal %}<span class="btn btn-xs"  style="background-color:#c0c0c0">r:{{ member.season_pass_normal }}</span>{% endif %}         </div>         <div class="col-md-2">              <span class="btn btn-xs"  style="background-color:yellowgreen">class attended: {{ member.class_attended|length }}         </div>     </p>     </div>    {% endfor %}  {% endblock %} 

the slow part {% member in all_members %} there way go through list of user , build page?

the problem isn't jinja template python code. you're returning records @ same time slows down execution. need perform sort of pagination/lazy loading. if you're using flask-sqlalchemy, has paginate method work you. example, query this

user_ = user.query.order_by(user.fullname.asc()).paginate(1, 10, true) 

you can find more information here


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -