search

Home  >  Q&A  >  body text

Run python script in PHP (add to Pandas df and save file)

When the user selects an xlsx file and submits it via POST, I want to take the file, put it into a pandas df, do some data cleaning/formatting, and then send it back to the browser as a downloadable csv.

I've got the python/pandas script working fine. I've been able to successfully pass a simple $PHP variable (the filename) to a Python script, print it out in Python, and then echo it back in PHP. ...but I can't run the rest of the Python script (the pandas part). It's like the rest of my script doesn't exist.

What I'm doing is:

  1. Move the temporarily committed files from $_FILES to another folder named "uploads" (same directory).
  2. Get the path of the new saved file.
  3. Pass this into a Python script and save the formatted file to upload/finish - I think this is where I'm failing somewhere

(I haven't figured out the part about how to send the file back to the browser - just trying to get the file first).

I installed python venv in 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 works:

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

My Python won't do anything (works when I use regular filename sub sys.argv[1]):

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 haven't tried printing df to PHP yet. I just want to see if I can generate a csv file.

I tried escapeshellcmd and system with no success. I read a very similar question here which mentioned chmod 777 for setting permissions. I did this to change the permissions and now my folder is readable and writable by everyone, but it still doesn't work.

I'm confused because everything seems to be working fine and connected except the pandas part. Can anyone help? I'm new to PHP and really appreciate it.

P粉536532781P粉536532781231 days ago464

reply all(1)I'll reply

  • P粉488464731

    P粉4884647312024-03-28 17:43:57

    Sounds like you're close. The problem is that PHP is handled on the server side, so if you want the POST to return the web page code, send back HTML, since PHP is used to generate HTML on the 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 created PHP page in the POST response.

    reply
    0
  • Cancelreply