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 Get Current Date And Time In Node.js
How To Get Current Date And Ti...

In this example we will see how to get current date and time in Node.js application. In Node.js date and time are handle...

Read More

Sep-01-2021

How to Add Toastr Notification in Laravel 11 Example
How to Add Toastr Notification...

Hello developer! In this guide, we'll see how to add toastr notification in laravel 11. Here, we'll di...

Read More

Apr-24-2024

Laravel 8 Remove/Hide Columns While Export Data In Datatables
Laravel 8 Remove/Hide Columns...

In this article, we will see how to remove/hide columns while export data in datatables in laravel 8. When we are u...

Read More

Oct-13-2020

Laravel 11 Move Data from One Table to Another Table
Laravel 11 Move Data from One...

Moving data from one table to another is a common task when managing databases in Laravel applications. Laravel provides...

Read More

Dec-26-2024