简体   繁体   中英

How to get python output and print with php

This my python file hi.py:

import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer

data = pd.read_csv("C:/xampp/htdocs/product.csv")
data['content'] = data['Brand']+' '+data['Title']+' '+data['Product Type']+' 
'+data['Gender']+' '+data['Price'].astype(str)+' '+data['MRP'].astype(str)+' 
'+data['Rating'].astype(str)

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features=1000)
product_vectors = vectorizer.fit_transform(data['content'].values)


from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(product_vectors)

def recommend(content):

 # find  index from dataset
 content_index = data[data['Title'] == content].index[0]

 # finding cosine similarities 
 distances = similarity[content_index]

# sorting cosine similarities
 my_list = sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[1:10]
 dh=[]
#return my_list
 for i in my_list:
    #print(data.iloc[i[0]].Title)
    dh.append(data.loc[i[0]].Title)
     #return dh
print(recommend('Adidas Men Running Shoes'))

I need the list of recommended products to be printed in php localhost. This is my php file:

<?php
$output=shell_exec('py hi.py');
echo $output;
?>

However when I'm running the php file I'm getting output in VSCode but nothing is printed when I run on localhost.

What might be the problem please help me.

The problem is in your PHP code eg it's executing the command but it is only returning the standard output stream to you, that's the reason on error you are getting the blank screen because the shell_exec is not redirecting the error stream, to do so modify your code as shown below

<?php
$output=shell_exec('py hi.py 2>&1');
echo $output;
?>

Once the error is shown you will be able to solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM