Ian305 Posted May 15, 2020 Share Posted May 15, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310785-progress-bar-of-a-script-using-server-sent-eventssse-in-php/ Share on other sites More sharing options...
requinix Posted May 15, 2020 Share Posted May 15, 2020 Does the python script output anything as it goes? Are you trying to return that output? Quote Link to comment https://forums.phpfreaks.com/topic/310785-progress-bar-of-a-script-using-server-sent-eventssse-in-php/#findComment-1578024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.