'Running python script inside PHP (add to Pandas df and save file)

When a user chooses an xlsx file and submits through POST, I would like to get that file, put it in a pandas df, do some data cleansing/formatting, and then send it back to the browser as a downloadable csv.

I've got the python/pandas script working. I've been able to successfully pass a simple $PHP variable (file name) to the Python script, print that out in Python, and then echo that back out in PHP. ...but I can't get the rest of my Python script to run (pandas portion). It's as though the rest of my script doesn't exist.

What I'm doing is:

  1. Move temporary submitted file from $_FILES to another folder (same directory) called 'uploads'.
  2. Grab the path of the newly saved file.
  3. Pass that into Python script and save formatted file to uploads/completed-- I assume here is where I've failed somewhere

(I'm not figuring out the part on how to send the file back to the browser yet-- just trying to get the file in first).

I have a python venv installed in my htdocs folder and it is activated.

My PHP:

$fname = $_FILES["upload"]["tmp_name"];
$upload_path = 'uploads/' . $fname;

$output = shell_exec('/Applications/MAMP/htdocs/venv/bin/python3 /Applications/MAMP/htdocs/data_cleanse.py ' . $upload_path);
echo $output;

My Python that works:

import sys
print("Variable passed to Python is: " + sys.argv[1])

My Python that won't do anything (works when I sub sys.argv[1] with a regular file name):

import sys
import pandas as pd

uploaded_file = sys.argv[1]

df = pd.read_excel(uploaded_file)
df.to_csv('uploads/completed/completed_doc.csv')

I am not yet trying to print the df to PHP. I am just trying to see if I can generate the csv file.

I've tried escapeshellcmd and system which did not work. I read a really similar question here which mentioned chmod 777 for setting permissions. I did that to change permissions and now my folder is readable & writable by everyone but it still won't run.

I'm confused because it all seems to run fine and connect fine except for the pandas portion. Can someone please help? I am new to PHP and would really appreciate it.



Solution 1:[1]

Sounds like you’re close. The hick is that PHP is processed server-side, so if you want your POST to return the webpage code, send back HTML, as PHP is used to generate HTML server-side.

If you have a PHP engine on your server, you can save the PHP server-side, and return a public link to the newly minted PHP page in the POST response.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam Smooch