How To Run Python Script In Laravel 9

Websolutionstuff | May-07-2022 | Categories : Laravel Python

In this article, we will see how to run the python scripts in laravel 9. Python is a popular programming language. Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.

So, let's see how to use python script in laravel 9, laravel 9 run python script,  how to integrate python script in laravel, python code run in laravel, how to run python in laravel, how to use python script in laravel 7/8/9.

Using the python script you can check major functionalities like image recognition, face recognition, check image blur or not, import or export large datasets, and large CSV/Excel file download.

Step 1: Write a simple Python Script

 In this step, we will create a simple python script.

# User inputs the string and it gets stored in the variable str
str = input("Enter a string: ")

# counter variable to count the character in a string
counter = 0
for s in str:
      counter = counter+1

print("Length of the input string is:", counter)

 

 

Step 2: Run Python Code in Laravel

The laravel provides a process() method for running the script. The Process component executes commands in sub-processes. So, install the process package using the below command.

composer require symfony/process

After installing the package we will create a function and run the python script.

In the controller include a symphony process component.

use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
public function PythonScript()
{
    $process = new Process("python3 /var/www/laravel/Laravel_Python_Example/app/PythonScript/test.py");
    $process->run();
    
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }

    $data = $process->getOutput();

    dd($data);
}

 

 

If you want to pass parameters to the python script you can do it like this.

$process = new Process(['python', '/path/to/script.py', $arg]);

 


You might also like :

Recommended Post
Featured Post
Laravel 9 Livewire Dependent Dropdown
Laravel 9 Livewire Dependent D...

In this article, we will see the laravel 9 livewire dependent dropdown. Here, we will learn how to create a dependent dr...

Read More

Nov-29-2022

Laravel 11 Drag and Drop File Upload with Dropzone JS
Laravel 11 Drag and Drop File...

Hello web developers! In this article, we'll see laravel 11 drag and drop file upload with dropzone js. He...

Read More

May-06-2024

Laravel 8 Datatables Keeping Selected Page Number
Laravel 8 Datatables Keeping S...

In this tutorial we will see laravel 8 datatables keeping selected page number after callback. In datatable page nu...

Read More

Dec-03-2021

How To Add Ckeditor In Laravel
How To Add Ckeditor In Laravel

In this article, I will explain you how to add ckeditor in laravel, CKEditor is a WYSIWYG rich text edito...

Read More

Jun-18-2020