Skip to end of metadata
Go to start of metadata

You can run your python code easily based on your web browser if you have Apache+PHP server. Below code enable you to run your code by click

PHP is a useful programming language to implement web application, and easy to run python code by shell_exec(). 

<?php

echo shell_exec( "hello_world.py");

?>

Note that you should use echo to print out the executed result on the browser.


For beginners, below simple code will be user useful. Just copy&paste to your web server and you will be able to run your code by click.

index.php
<?php
/*
 * Easy Python Launcher
 *
 * Programmed by Chun Kang (2021-08-03) for Johnny Kang
 *
**/

extract( $_POST);
extract( $_GET);
extract( $_COOKIE);
extract( $_SERVER);

function findfiles( $path, $regexp="", $order=0)
{
    $r = scandir( $path, $order); // $order: 0->ascending / 1->descending
    if ($r==false) $ret=$r;
    else
    {
        $ret=array();
        $i=count($r)-1;
        for($i=0; $i<count($r); $i++)
        {
            if (
                    (($regexp=="") || preg_match('/' . $regexp . '/', $r[$i]))
                    && strlen($r[$i])
                    && $r[$i]!='.'
                    && $r[$i]!='..'
                ) $ret[]=$r[$i];
        }
    }
    return $ret;
}


if (strlen($p))
{
	header("Content-Type: text/html; charset=UTF-8");

	if (preg_match("/\.py$/", $p))
	{
		echo shell_exec("python {$p}");
	}
	else
	{
		echo shell_exec("python {$p}.py");
	}
}
else
{
	$code_list = findfiles( ".", ".py");
	if (@count($code_list))
	{
		echo "Your codes available<br>";
		echo "<hr>";
		foreach($code_list as $filename)
		{
			echo "<a href=\"?p=" . urlencode($filename) . "\">{$filename}</a><br>";
		}
	}
	else
	{
		echo "You can run your code like below:<br><br>";
		echo "http://{$_SERVER['SERVER_NAME']}{$PHP_SELF}?p=[your_python_filename]";
	}
}

?>