Jump to content

Progress bar of a script using Server Sent Events(SSE) in PHP


Ian305

Recommended Posts

I'm trying to show the progress of executing a python script in PHP with a progress bar. I found a link (https://www.htmlgoodies.com/beyond/php/show-progress-report-for-long-running-php-scripts.html) that uses SSE to send progress updates to the client. In the original code, a loop is used to simulate a long script but in my case I'm trying to execute a python script that takes roughly 50 seconds.

index.php

<!DOCTYPE html>
   <html>
      <head>
         <meta charset="utf-8" />
      </head>
      <body>
         <br />
         <input type="button" onClick="startTask()"  value="Start Long Task" />
         <input type="button" onClick="stopTask();"  value="Stop Task" />
         <br />
         <br />
         <p>Results</p>
         <br />
         <div id="results" style="border:1px solid #000; padding:10px; width:300px; height:250px; overflow:auto; background:#eee;"></div>
         <br />
         <progress id='progressor' value="0" max='100' style=""></progress>
         <span id="percentage" style="text-align:right; display:block; margin-top:5px;">0</span>
      </body>
   </html>

process.php

<?php
header('Content-Type: text/event-stream');
// recommended to prevent caching of event data.
header('Cache-Control: no-cache');

function send_message($id, $message, $progress) {
    $d = array('message' => $message , 'progress' => $progress);

    echo "id: $id" . PHP_EOL;
    echo "data: " . json_encode($d) . PHP_EOL;
    echo PHP_EOL;

    ob_flush();
    flush();
}


//LONG RUNNING TASK
// for($i = 1; $i <= 10; $i++) {
//     send_message($i, 'on iteration ' . $i . ' of 10' , $i*10);
//     sleep(1);
$path="test.py";
$test= shell_exec("python " . $path . ""); {
    echo "data: " . json_encode($test) . "\n\n";
    sleep(1)
}

send_message('CLOSE', 'Process complete');
 

Problem: As you can see in process.php I commented the original code and placed a simple python execution in php. I just want to diplay the progress of the script but it is not doing so. What do I have to do to show the progress of the script? Note: I don't think is relavent but the python script just opens up a batch file.

Are there other methods to display the progress bar of a script in php?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.