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>
<script type="text/javascript">
var es;
function startTask() {
es = new EventSource('process.php');
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;
}
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
header('Content-Type: text/event-stream');
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();
}
$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