<?php
// pie_chart_python.php
// RJM Programming - September, 2026
if (php_sapi_name() === 'cli') {
$command = '/usr/local/bin/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython.py'; // Your command
if (1 == 1) {
exec($command);
} else {
// Open the process pointer for reading
$handle = popen($command, 'r');

if ($handle) {
    // Read and output the buffer until the end of the file
    while (!feof($handle)) {
        echo fread($handle, 2048);
        flush(); // Send it to the browser immediately
    }
    pclose($handle);
}
}
exit;
} else if (strpos(str_replace('?','',$_SERVER['QUERY_STRING']), '&') === false) {
$waspy=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.py');
if (strpos($waspy, "nums = []") !== false) {
    if (isset($_GET['array'])) {
      file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython.py', str_replace("nums = []","nums = [" . str_replace('+',' ',urldecode($_GET['array'])) . "]",$waspy));
    }
}
//exec('php ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.php > /dev/null 2> /dev/null');
exec('cd ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ; /Applications/MAMP/bin/php/php7.4.33/bin/php -n ./pie_chart_python.php > /dev/null 2> /dev/null ; open ./plot.png');
sleep(3);
}
echo "<html><body style=\"background:url(./plot.png);background-repeat:no-repeat;background-size:contain;\"></body></html>";
?>
