Как выполнить python файл из laravel - PullRequest
0 голосов
/ 29 мая 2020
public function store(Request $request)
    {
        DB::table('accountancy')->insert([
            'nisn'          => $request->nisn,
            'student_name'  => $request->student_name,
            'study_period'  => $request->study_period,
            'mtk_1'         => $request->mtk_1,
            'b_indo_1'      => $request->b_indo_1,
            'b_ing_1'       => $request->b_ing_1,
            'p_a_1'         => $request->p_a_1,
            'e_p_1'         => $request->e_p_1,
            'd_p_1'         => $request->d_p_1,
            's_s_1'         => $request->s_s_1,
            's_d_1'         => $request->s_d_1,
            'mtk_2'         => $request->mtk_2,
            'b_indo_2'      => $request->b_indo_2,
            'b_ing_2'       => $request->b_ing_2,
            'p_a_2'         => $request->p_a_2,
            'e_p_2'         => $request->e_p_2,
            'd_p_2'         => $request->d_p_2,
            's_s_2'         => $request->s_s_2,
            's_d_2'         => $request->s_d_2,
            'mtk_3'         => $request->mtk_3,
            'b_indo_3'      => $request->b_indo_3,
            'b_ing_3'       => $request->b_ing_3,
            'p_a_3'         => $request->p_a_3,
            'a_p_3'         => $request->a_p_3,
            'a_p_d_3'       => $request->a_p_d_3,
            'a_k_3'         => $request->a_k_3,
            'k_a_3'         => $request->k_a_3,
            'mtk_4'         => $request->mtk_4,
            'b_indo_4'      => $request->b_indo_4,
            'b_ing_4'       => $request->b_ing_4,
            'p_a_4'         => $request->p_a_4,
            'a_p_4'         => $request->a_p_4,
            'a_p_d_4'       => $request->a_p_d_4,
            'a_k_4'         => $request->a_k_4,
            'k_a_4'         => $request->k_a_4,
            'mtk_5'         => $request->mtk_5,
            'b_indo_5'      => $request->b_indo_5,
            'b_ing_5'       => $request->b_ing_5,
            'a_p_5'         => $request->a_p_5,
            'a_p_d_5'       => $request->a_p_d_5,
            'a_k_5'         => $request->a_k_5,
            'k_a_5'         => $request->k_a_5,
            'a_p_m_5'       => $request->a_p_m_5,
        ]);

        $process = shell_exec("C:/Users/GaluhPrisillia/anaconda3/envs/tes/python C:/wamp/www/webpress/app/Http/Controllers/Python/avgkompetensi.py 2>&1");

        return redirect('prediction-system/accountancy/result');
    }

Это мой код в контроллере laravel. Я хочу выполнить python файл из laravel. Процесс, который я хочу: пользователь вводит данные из laravel view -> вставляет в mysql -> python получает данные из mysql, затем выполняет с использованием алгоритма KNN в python, затем отправляет (обновляет) результат в столбец статуса и университета в mysql -> laravel показать результат из таблицы в mysql, который уже обновлен на python. Когда я запускаю эту программу, python не может обновить результат до mysql. Но мой файл python не является ошибкой и может обновляться до mysql при запуске в анаконде. Вот мой python код:

from mysql.connector import Error
import pandas as pd
import numpy as np
from sklearn import neighbors, datasets
from sklearn.metrics.pairwise import euclidean_distances
from sklearn import preprocessing 
import mysql.connector

n_neighbors = 5
AK = pd.read_csv('alldata_ak.csv')
X = AK.drop(["Name","NISN", "Status"], axis = 1) 
y = AK["Status"]
h = .02

clf = neighbors.KNeighborsClassifier(n_neighbors, p=2, weights='distance')
clf.fit(X, y)

try:
    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        password="",
        database="webpress_db"
        )

    mycursor = mydb.cursor(dictionary=True)
    sql_select_query = ("SELECT id, mtk_1, b_indo_1, b_ing_1, p_a_1, e_p_1, d_p_1, s_s_1, s_d_1, mtk_2, b_indo_2, b_ing_2, p_a_2, e_p_2, d_p_2, s_s_2, s_d_2, mtk_3, b_indo_3, b_ing_3, p_a_3, a_p_3, a_p_d_3, a_k_3, k_a_3, mtk_4, b_indo_4, b_ing_4, p_a_4, a_p_4, a_p_d_4, a_k_4, k_a_4, mtk_5, b_indo_5, b_ing_5, a_p_5, a_p_d_5, a_k_5, k_a_5 ,a_p_m_5, status, university FROM accountancy")
   mycursor.execute(sql_select_query)
    myresult = mycursor.fetchall()

    for z in myresult :

        mtk_1 = z["mtk_1"]
        b_indo_1 = z["b_indo_1"]
        b_ing_1 = z["b_ing_1"]
        p_a_1 = z["p_a_1"]
        d_p_1 = z["d_p_1"]
        e_p_1 = z["e_p_1"]
        s_s_1 = z["s_s_1"]
        s_d_1 = z["s_d_1"]
        mtk_2 = z["mtk_2"]
        b_indo_2 = z["b_indo_2"]
        b_ing_2 = z["b_ing_2"]
        p_a_2 = z["p_a_2"]
        d_p_2 = z["d_p_2"]
        e_p_2 = z["e_p_2"]
        s_s_2 = z["s_s_2"]
        s_d_2 = z["s_d_2"]
        mtk_3 = z["mtk_3"]
        b_indo_3 = z["b_indo_3"]
        b_ing_3 = z["b_ing_3"]
        p_a_3 = z["p_a_3"]
        a_p_3 = z["a_p_3"]
        a_k_3 = z["a_k_3"]
        k_a_3 = z["k_a_3"]
        a_p_d_3 = z["a_p_d_3"]
        mtk_4 = z["mtk_4"]
        b_indo_4 = z["b_indo_4"]
        b_ing_4 = z["b_ing_4"]
        p_a_4 = z["p_a_4"]
        a_p_4 = z["a_p_4"]
        a_k_4 = z["a_k_4"]
        k_a_4 = z["k_a_4"]
        a_p_d_4 = z["a_p_d_4"]
        mtk_5 = z["mtk_5"]
        b_indo_5 = z["b_indo_5"]
        b_ing_5 = z["b_ing_5"]
        a_p_5 = z["a_p_5"]
        a_k_5 = z["a_k_5"]
        k_a_5 = z["k_a_5"]
        a_p_d_5 = z["a_p_d_5"]
        a_p_m_5 = z["a_p_m_5"]
        status = z["status"]
        university = z["university"]

        AVGK1 = (p_a_1 + e_p_1 + d_p_1 + s_s_1 + s_d_1)/5
        AVGK2 = (p_a_2 + e_p_2 + d_p_2 + s_s_2 + s_d_2)/5
        AVGK3 = (p_a_3 + a_p_3 + a_p_d_3 + a_k_3 + k_a_3)/5
        AVGK4 = (p_a_4 + a_p_4 + a_p_d_4 + a_k_4 + k_a_4)/5
        AVGK5 = (a_p_5 + a_p_d_5 + a_k_5 + k_a_5 + a_p_m_5)/5

        dataClass = clf.predict([[mtk_1, b_indo_1, b_ing_1, AVGK1, mtk_2, b_indo_2, b_ing_2, AVGK2, mtk_3, b_indo_3, b_ing_3, AVGK3, mtk_4, b_indo_4, b_ing_4, AVGK4, mtk_5, b_indo_5, b_ing_5, AVGK5]])

        sql_update_query = ("UPDATE accountancy set status = %s, university = %s ORDER BY id DESC LIMIT 1")
        input0 = ('NOT ACCEPTED', '')
        input1 = ('ACCEPTED', 'UNIVERSITAS UPN VETERAN JAKARTA')
        input2 = ('ACCEPTED', 'UNIVERSITAS TIDAR')
        input3 = ('ACCEPTED', 'UNIVERSITAS NEGERI JAKARTA')

        if dataClass == 0:
            mycursor.execute(sql_update_query, input0)
            mydb.commit()
        elif dataClass == 1:
            mycursor.execute(sql_update_query, input1)
            mydb.commit()
        elif dataClass == 2:
            mycursor.execute(sql_update_query, input2)
            mydb.commit()
        else:
            mycursor.execute(sql_update_query, input3)
            mydb.commit()

except Error as e:
    print("Error reading data from MySQL table", e)
finally:
    if (mydb.is_connected()):
       mydb.close()
       mycursor.close()
...