Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
HTML
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <script type="text/javascript">
    var es;

    function startTask() {
        es = new EventSource('process.php');

        //a message is received
        es.addEventListener('message', function(e) {
              var result = JSON.parse( e.data );

              addLog(result.message);

              if(e.lastEventId == 'CLOSE') {
                  addLog('Received CLOSE closing');
                  es.close();
                  var pBar = document.getElementById('progressor');
                  pBar.value = pBar.max; //max out the progress bar
              }
              else {
                  var pBar = document.getElementById('progressor');
                  pBar.value = result.progress;
                  var perc = document.getElementById('percentage');
                  perc.innerHTML   = result.progress  + "%";
                  perc.style.width = (Math.floor(pBar.clientWidth * (result.progress/100)) + 15) + 'px';
              }
        });

        es.addEventListener('error', function(e) {
            addLog('Error occurred');
            es.close();
          });
      }

        function stopTask() {
          es.close();
          addLog('Interrupted');
       }

        function addLog(message) {
          var r = document.getElementById('results');
          r.innerHTML += message + '<br>';
          r.scrollTop = r.scrollHeight;
      }
    </script>
    <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
<?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 display 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 relevant but the python script just opens up a batch file.

What I have tried:

I have searched for solutions to show the progress of a script executing in php, but most examples are about uploading/downloading files not executing a script. There are examples like the one used by the link that uses SSE but all of them uses a loop. I'm trying to show the progress of the python script but is not working. I'm new to php and anything would help, thanks
Posted
Updated 14-May-20 1:15am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900