Laravel-Python Automation Efficient Workflow Optimization

Websolutionstuff | Nov-06-2024 | Categories : Laravel Python

In this guide, I’ll show you how to use Python scripts to streamline workflows in Laravel by automating repetitive tasks, such as data migration and report generation. Combining Laravel's powerful backend framework with Python's automation capabilities can save time and ensure consistency in our tasks.

By the end of this tutorial, you'll be able to run Python scripts in a Laravel project for quick, automated solutions.

Laravel-Python Automation: Streamlining Workflows for Efficiency

Laravel-Python Automation: Streamlining Workflows for Efficiency

 

Step 1: Setting Up the Environment

First, ensure that Python and Laravel are installed on your system. Laravel runs on PHP, but we can execute Python scripts from within our Laravel app by using the shell_exec function.

Requirements:

  • Laravel 11
  • Python 3.x

Verify that Python is accessible by running:

python --version

 

Step 2: Create a Python Script for Data Migration

Let's create a simple Python script that reads data from a CSV file and formats it for use in Laravel’s database.

Create the Python Script in the Laravel project directory, like scripts/migrate_data.py

scripts/migrate_data.py:

import csv
import json
import sys

def csv_to_json(csv_file):
    data = []
    with open(csv_file, 'r') as file:
        reader = csv.DictReader(file)
        for row in reader:
            data.append(row)
    return json.dumps(data)

if __name__ == "__main__":
    csv_file = sys.argv[1]  # First argument passed is the CSV file
    print(csv_to_json(csv_file))

This script converts CSV data to JSON format, which we can easily work with in Laravel.

 

Step 3: Create a Laravel Controller to Run the Python Script

In Laravel, we’ll create a controller method to execute the Python script and capture its output. This approach enables us to integrate Python’s capabilities into Laravel’s workflow.

php artisan make:controller AutomationController

Then, add the following code to execute the Python script and process the JSON output.

app/Http/Controllers/AutomationController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AutomationController extends Controller
{
    public function migrateData()
    {
        // Path to the Python script
        $scriptPath = base_path('scripts/migrate_data.py');
        
        // Path to the CSV file
        $csvPath = storage_path('data/sample_data.csv');

        // Execute Python script and capture the output
        $command = escapeshellcmd("python $scriptPath $csvPath");
        $output = shell_exec($command);
        
        // Decode JSON output from Python
        $data = json_decode($output, true);

        // Store data in Laravel database (Example: storing in users table)
        foreach ($data as $item) {
            \App\Models\User::create([
                'name' => $item['name'],
                'email' => $item['email'],
                'password' => bcrypt($item['password']), // Use hashed password
            ]);
        }

        return response()->json(['status' => 'Data migrated successfully']);
    }
}

This method runs the Python script, converts the CSV data to JSON, and then uses Laravel’s User model to insert each record into the database.

 

Step 4: Set Up a Route to Trigger the Automation

Now, we need a route to access this automation task from the browser or API client.

Add the following route in routes/web.php

use App\Http\Controllers\AutomationController;

Route::get('/migrate-data', [AutomationController::class, 'migrateData']);

 

Step 5: Create a Python Script for Report Generation

For our second task, let’s use Python to generate a report in JSON format from database data. This report could, for instance, summarize user statistics.

Create another script, scripts/generate_report.py

scripts/generate_report.py:

import json
import sys

def generate_report(data):
    report = {
        "total_users": len(data),
        "emails": [user['email'] for user in data]
    }
    return json.dumps(report)

if __name__ == "__main__":
    data = json.loads(sys.stdin.read())  # Accept JSON input via stdin
    print(generate_report(data))

 

Step 6: Modify the Controller to Generate the Report

We’ll add a new method in AutomationController to collect data from Laravel, pass it to the Python script, and handle the report output.

app/Http/Controllers/AutomationController.php

public function generateReport()
{
    // Retrieve all users from the database
    $users = \App\Models\User::all()->toArray();
    
    // Convert users data to JSON and pass it to Python script via stdin
    $scriptPath = base_path('scripts/generate_report.py');
    $command = "python $scriptPath";
    
    // Open a process to send JSON data and read output
    $process = popen($command, 'w');
    fwrite($process, json_encode($users));
    $report = stream_get_contents($process);
    pclose($process);
    
    // Return the report as JSON
    return response()->json(['report' => json_decode($report)]);
}

This method fetches user data from Laravel’s database, sends it to the Python script, and returns a JSON report.

 

Step 7: Create a Route for Report Generation

Add a new route for the report generation in routes/web.php

Route::get('/generate-report', [AutomationController::class, 'generateReport']);

 


You might also like:

Recommended Post
Featured Post
How to Install PHP JSON Extension in Ubuntu 23.04
How to Install PHP JSON Extens...

Hey there! If you're working with PHP on Ubuntu 23.04 and find yourself needing JSON support, you're in the righ...

Read More

Feb-05-2024

Laravel 8 Image Upload Example
Laravel 8 Image Upload Example

In this article, we will see the laravel 8 image upload example. Image or file upload is the most common task...

Read More

Oct-06-2020

Reseller Hosting Myths to Know and Unfollow
Reseller Hosting Myths to Know...

If you work in the web hosting sector, you're probably acquainted with the term reseller hosting. Resellers make up...

Read More

Apr-07-2022

Datatables Expand/Collapse Columns
Datatables Expand/Collapse Col...

In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of method...

Read More

Jun-05-2022