Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Code Block
languagepy
titlematplot_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>")


Code Block
languagephp
titlematplot_test.php
<?php

echo shell_exec("python matplot_test.py");

?>