Improve HTML page load

66 views
Skip to first unread message

Maurice Waka

unread,
Jun 11, 2018, 8:22:59 AM6/11/18
to web2py-users
I have an HTML page that take long to load,about 60 to 60 seconds sometimes. This is the code.
{{extend "layout.html"}}
<html lang="en">
 
<head>
   
<link rel="shortcut icon" href="favicon.ico" />
   
<link href="{{=URL('static', 'css/view_searches.css')}}" rel="stylesheet" type="text/css" />
 
</head>
    <body>
        <div class="chat_window">
        <ul class="messages">
        {{for reply in replies:}}
        <li class="message left appeared">
        <div>
        {{=prettydate(reply.modified_on)}}
        </
div>
       
<div class="text_wrapper">
       
<div class="text">{{=XML(reply.quest, sanitize=True)}}</div>
       
</div>
        </
li>
       
<li class="message right appeared">
       
<div>
       
{{=prettydate(reply.modified_on)}}
       
</div>
        <div class="text_wrapper">
        <div class="text">{{=XML(reply.message, sanitize=True)}}</
div>
       
</div>
        </
li>
       
{{pass}}
       
</ul>
        <div class="bottom_wrapper clearfix">
        <div class="message_input_wrapper">
        <div class="message_input_wrapper">
        {{=form.custom.begin}}
        <textarea name="message" id="message_input" placeholder="Type your message here..."></
textarea>
       
<button>send</button>
        {{=form.custom.end}}
        </
div>
       
</div>
        </
div>
       
</div>
        <div class="message_template">
        <li class="message">
        <div class="message-data-time" ></
div>
       
<div class="text_wrapper">
       
<div class="text"></div>
       
</div>
        </
li>
       
</div>
    </
body>

the controller code is as follows:
replies = db(db.answers.author == auth.user.id).select(db.answers.ALL)[-7:-1]#:-1]##if getting upto the last record, it appears twice when posting on view
    question
= db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))
    answer
= db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on, limitby=(2,0))
    q
= ''
    a
= ''
   
for q in question:
        q
= XML(q.quest, sanitize=True)
   
for a in answer:
        a
= XML(a.message, sanitize=True)
   
return dict(form=form, q=q, a=a, replies=replies)


How can I make it load faster?
KInd regards

Carlos Cesar Caballero Díaz

unread,
Jun 11, 2018, 10:04:01 AM6/11/18
to web...@googlegroups.com

The first thing is identifying the reason of the slow load. Is the render of the view, the controller, or the data access? you can debug or strip parts of your code to find that.

By example, if your code loads too many questions and/or answers it will be really slow because there will be too many objects in memory.

Greetings.


El 11/06/18 a las 08:22, Maurice Waka escribió:
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Jun 11, 2018, 11:10:03 AM6/11/18
to web2py-users
replies = db(db.answers.author == auth.user.id).select(db.answers.ALL)[-7:-1]#:-1]##if getting upto the last record, it appears twice when posting on view
    question
= db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))

What are you trying to achieve with limitby=(2, 0)? That doesn't make sense (it results in a negative LIMIT value, which will either generate an error or be ignored). If you want the latest two values, it should be (0, 2), not (2, 0).
 

    answer
= db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on, limitby=(2,0))
    q
= ''
    a
= ''
   
for q in question:
        q
= XML(q.quest, sanitize=True)

Assuming question contains more than one record, what is the point of looping here -- you only end up with the final record being assigned to q. You should just select a single record and use that (no loop needed).
 

   
for a in answer:
        a
= XML(a.message, sanitize=True)
   
return dict(form=form, q=q, a=a, replies=replies)

It doesn't appear that q or a are used in the view.

Anthony

Anthony

unread,
Jun 11, 2018, 11:10:50 AM6/11/18
to web2py-users
        {{=form.custom.begin}}

Where does form come from? It does not appear to be defined in the controller. If there is more code, you should show it.

Anthony

Maurice Waka

unread,
Jun 11, 2018, 2:20:52 PM6/11/18
to web...@googlegroups.com
Thanks for the help given.
To give some more details, I have several problems am trying to sort namely;
  1. I have a very long controller, which I'll be getting to separate to more controllers. This is why I gave only the last part of the code, excluding the form. I have noted that I may also need help in separating the long code to several controllers. I'll get to this later.See below.
  2. I am trying several options to get the best chatbot feature, hence the q, a coming in separately. The replies tables give the previous questions and answers the user has seen, while the q,a are the new questions and answers coming in, hence appearing at the bottom of the page. See the js code, and the limitby(0,2). My aim, since this maybe different from other bot codes is to allow a user to question or search, the question gets posted in the database and the system modules search for the answer that also gets posted into the answers table. This is the table that is iterated getting 'replies'. 
  3. I have dome some scripting work including 'minify' etc that so far has helped reduce the loading speed. That's why I gave this part of code, thinking that maybe that's where to issue is.
Below is the start of the controller code, containing several hundred lines:
@mobilize
@auth.requires_login()
def view_searches():
    if db(db.post).isempty():
        db.post.insert(message="Hi, it's" +' ' +auth.user.first_name+' '+ 'loging in...')
    form = SQLFORM(Post, formstyle='table3cols',)
    if form.process().accepted:
        pass
    messagev = ''
    for r in db(db.post.author == auth.user.id).select(db.post.ALL):
        messagev = r.message
    form1 = messagev.split()
    name3 = ' '.join(form1).lower()
    ...more lines of code

At the limitby(0,2) am using the js code below to obtain the string for the q, and a.
js code:
<script type="text/javascript" async="">
        (function(){var e;e=function(e){var t;return this.text=e.text,this.message_side=e.message_side,this.draw=(t=this,function(){var e;return(e=$($(".message_template").clone().html())).addClass(t.message_side).find(".text").html(t.text),$(".messages").append(e),setTimeout(function(){return e.addClass("appeared")},0)}),this},$(function(){var t,s,n;s="right",t=function(){return $(".message_input").val()},n=function(t){var n;if(""!==t.trim())return $(".message_input").val(""),n=$(".messages"),new e({text:t,message_side:s="left"===s?"right":"left"}).draw(),n.animate({scrollTop:n.prop("scrollHeight")},3)},$(".send_message").click(function(e){return n(t())}),$(".message_input").keyup(function(e){if(13===e.which)return n(t())}),n("{{=q}}"),setTimeout(function(){return n("{{=a}}")},1500)})}).call(this);
    </script> 


--
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/O6CxDRViyNA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Anthony

unread,
Jun 11, 2018, 3:29:12 PM6/11/18
to web2py-users
Below is the start of the controller code, containing several hundred lines:

Hard to know what's going on without the code. Consider profiling the code, or simply pick some spots you think might be taking long and return early to see if things speed up.
 
    form = SQLFORM(Post, formstyle='table3cols',)
    if form.process().accepted:
        pass

No need for the "if" or the ".accepted" here. Just do:

form = SQLFORM(Post, formstyle='table3cols').process()
 
    messagev = ''
    for r in db(db.post.author == auth.user.id).select(db.post.ALL):
        messagev = r.message

There is no reason to select the entire table and loop over it -- ultimately, you simply end up with the message from the last record. Replace the above with:

row = db(db.post.author == auth.user.id).select(db.post.id, db.post.message,
                                               
orderby=~db.post.id,
                                                limitby
=(0, 1)).first()

messagev
= row.message if row else None

At the limitby(0,2) am using the js code below to obtain the string for the q, and a.

Again, no reason to select the last two records if you end up using only the last record in your code.

Anthony

Maurice Waka

unread,
Jun 11, 2018, 3:32:42 PM6/11/18
to web...@googlegroups.com
Thank you. Am working on it. 

--

Maurice Waka

unread,
Jun 11, 2018, 10:58:01 PM6/11/18
to web...@googlegroups.com
Selecting the last item in q or a ends up with:
<Row {'message': '....strings....'', 'id': 8135L}>
How do I remove this to get only the strings without doing a loop?

Anthony

unread,
Jun 12, 2018, 12:47:05 PM6/12/18
to web2py-users
On Monday, June 11, 2018 at 10:58:01 PM UTC-4, Maurice Waka wrote:
Selecting the last item in q or a ends up with:
<Row {'message': '....strings....'', 'id': 8135L}>

That is the Python representation of an entire Row object. Probably you want to get just a single field from within it.

Anthony
Reply all
Reply to author
Forward
0 new messages