Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to add progress bar to the CGI script

by ashwin (Initiate)
on Aug 26, 2009 at 16:03 UTC ( [id://791376]=perlquestion: print w/replies, xml ) Need Help??

ashwin has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone,

I have a CGI script that takes input from the enduser, processes the request and shows the output to the user. The script takes a bit long to finish. As of now user can see only the process symbol until the request is completely processed.

To keep the user stick to the same page I'd like to add a progress bar that should let the user know of what's happening at the webserver like what's is the task being done, how long it will take to finish processing the request and showing the progress in a horizontal bar filling with a color gradually ...

I've tried using Javascript, HTML and CGI::ProgressBar perl module to do this. But, these didn't work the way I wanted.

Can you please guide me how to achieve this?

Here is how my script looks like:

--------------------------------------------------------
#!/usr/bin/perl use strict; use warnings; use CGI; require 'myroutines.pl'; # other required routines here ... ... my $cgi = new CGI; my $JSCRIPT=<<EOF; // JS functions to validate user input ... ... EOF print $cgi->header(); print $cgi->start_html(-Title=>'myTitle', -script=>$JSCRIPT); ## code here ## if( $cgi->param('submit') { my $output1 = & subRoutine1(arg1,arg2..); ... ... my $output2 = & subRoutine2(arg1,arg2,arg3..); ... ... ### I've used several subroutines ### showing the output to the user } else { ### code to show form fields the user has to fill in } print $cgi->end_html;
---------------------------------------------------------

Please let me know if anything needs to be clarified.

Thank you all in advance.

Regards,
Ashwin.

Replies are listed 'Best First'.
Re: how to add progress bar to the CGI script
by BioLion (Curate) on Aug 26, 2009 at 16:58 UTC

    What didn't you like about CGI::ProgressBar? There are a lot of other graphical and non-graphical progressbar options if you haven't already tried them?

    Just a something something...
      BiLion,

      I just tried the sample program given in the module documentation. I could only see a horizontal bar with no changes done to it.

      I'll give it a try again.
      
      Thank You
      
Re: how to add progress bar to the CGI script
by Sewi (Friar) on Aug 26, 2009 at 17:24 UTC
    At first: Use$|=1as early as possible in your script (usually you have #!/usr/bin/perl, use someting; $|=1;) otherwise your progress won't be shown until your script is done.

    Here is what I'm using:

    #!/usr/bin/perl $|=1; print '<header>'; # placeholder for your whole HTML header block print '<span id=Progress>Working...</span>'; print '<footer>'; # placeholder for the rest of your document until bu +t without </body> sleep 1; # Something is done here print <<_EOT_; <script language=JavaScript type=text/javascript><!-- document.getElementById('Progress').innerHTML='Still working...'; //--></script> _EOT_ sleep 1; # Something is done here print <<_EOT_; <script language=JavaScript type=text/javascript><!-- document.getElementById('Progress').innerHTML='Sorry, still not finish +ed...'; //--></script> _EOT_ sleep 1; # Something is done here print <<_EOT_; <script language=JavaScript type=text/javascript><!-- document.getElementById('Progress').style.display='none'; //--></script> _EOT_ print '</body></html>'; # Page finished
    I learned that most people prefer this:
    #!/usr/bin/perl $|=1; print '<header>'; # placeholder for your whole HTML header block print '<img src=animated.gif id=Progress>'; print '<footer>'; # placeholder for the rest of your document until bu +t without </body> sleep 1; # Something is done here sleep 1; # Something is done here sleep 1; # Something is done here print <<_EOT_; <script language=JavaScript type=text/javascript><!-- document.getElementById('Progress').style.display='none'; //--></script> _EOT_ print '</body></html>'; # Page finished
    Once I had a customer waiting three hours for the result not knowing that his internet connection broke early after starting the job.

    These solutions are not the best ones - they don't use any modules. But they're simple and work for me in a number of projects. You could also output your data & footer after finishing the processing. In this case, add a <br>\r\n behind the Progress-object to force flushing the buffer (\r\n), sending to the client and showing to the user (<br>).

      Sewi,

      Thank you very much for the information

      I'll follow the things you mentioned and let you know how it goes.

Re: how to add progress bar to the CGI script
by Anonymous Monk on Aug 26, 2009 at 16:51 UTC
Re: how to add progress bar to the CGI script
by leocharre (Priest) on Aug 26, 2009 at 18:28 UTC

    What do you mean, 'a long time'.

    Why not id the job, and execute it in the background.

    And when it's done, send them an email.

    Your output could be saved to /tmp/finished/email@address.com

    And a cron could check if there's anything in /tmp/finished, if so, they are emailed. Maybe even email the output to them (security?).

      Leocharre,

      I meant the script would take 2-3mins to get the output.
      It's a CGI script and follows client/server architecture. The output is shown on user's browser.

      Thanks for the information.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://791376]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-16 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found