I usually use shell_exec when I need to run python code. The benefit of shell_exec is returning the result as string, so I can use it for other purpose.


The below is the example to create graph by python, and display as a result in php.

matplot_test.py
import matplotlib
matplotlib.use('Agg')

from matplotlib import pyplot as plt

plt.plot(["Seoul","Paris","Seattle"], [30,25,55])
plt.xlabel('City')
plt.ylabel('Response')
plt.title('Experiment Result')
plt.savefig('test.png')

print("<img src=test.png>")
matplot_test.php
<?php

$locale = 'ko_KR.UTF-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);

echo shell_exec("python matplot_test.py");

?>

You will be able to see a graph when you run your code like http://your url/..../matplot_test.php