Tuesday, 3 June 2014

Calling Python Program in PHP with response feedback progressbar

Hey all,
I have a question of about how to get response of python program in php while we executing python in php. I found this solution. Which may be useful for some of you.


 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">  
 <html lang="en">  
 <head>  
   <title>Progress Bar</title>  
 </head>  
 <body>  
 <!-- Progress bar holder -->  
 <div id="progress" style="width:500px;border:1px solid #ccc;"></div>  
 <!-- Progress information -->  
 <div id="information" style="width:200px;"></div>  
 <div id="myDiv"></div>  
 <?php  
 // Total processes  
 $total = 3;  
 // Loop through process  
 for($i=1; $i<=$total; $i++){  
   // Calculate the percentation  
   $percent = intval($i/$total * 100)."%";  
   // Javascript for updating the progress bar and information  
   echo '<script language="javascript">  
   document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";  
   document.getElementById("information").innerHTML="processed.";  
   </script>';  
 // This is for the buffer achieve the minimum size in order to flush data  
   echo str_repeat(' ',1024*64);  
   echo 'Please wait...<br/>';  
   ob_flush();  
 // Send output to browser immediately  
   flush();  
 // Sleep one second so we can see the delay  
   sleep(1);  
 }  
 // Tell user that the process is completed  
 $pyscript = 'G:\wamp\www\ll\test.py';  
 $python = 'C:\\Python27\\python.exe';  
 $cmd="$pyscript $python";  
 exec("$cmd", $output);  
 file_put_contents('test.txt', $output);  
 $myfile = fopen("test.txt", "r") or die("Unable to open file!");  
 sleep(0.5);  
 echo fread($myfile,filesize("test.txt")).'<br/>';  
 fclose($myfile);  
 echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';  
 ?>  
 </body>  
 </html>  

If you found this useful.Like my blog.Subscribe me.Please give your valuable comments about my blog.

No comments:

Post a Comment