alvin Christianto
alvin Christianto

Reputation: 106

how to connect output from python script to laravel app?

I want to develop a laravel app that get data from python script, what do i need to configure?

I already search how to connect python output via Symfony, but I can't imagine what file I need to change in my Laravel app from this tutor

https://www.sandervanhooft.com/blog/laravel/how-to-use-laravel-with-python-and-the-command-line/

from the beginning, I just want to get the output from python script like text output "hello world".

Upvotes: 1

Views: 1192

Answers (2)

alvin Christianto
alvin Christianto

Reputation: 106

finally after a few day of searching, i try to make simple print statement of python script, like

color ='red'
print "my color is " + color

then, in controller(laravel app), i add some php code like : call python with php. I replace echo command with dd($output), then i got the output. thanks all for your answers.

Upvotes: 1

Alex P.
Alex P.

Reputation: 3787

The article you linked says that the code is for Laravel.

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

// $json = an encoded JSON string
$process = new Process("python3 /Path/To/analyse_json.py {$json}");
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

$output = $process->getOutput();
$jsonDoc = json_decode($output, true);
dump($jsonDoc);

It just uses a Symfony component which probably can be installed separately via Composer, or maybe even already included in the standard Laravel installation (Laravel itself uses some Symfony components too).

Upvotes: 1

Related Questions