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
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:
Verify that Python is accessible by running:
python --version
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.
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.
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']);
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))
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.
Add a new route for the report generation in routes/web.php
Route::get('/generate-report', [AutomationController::class, 'generateReport']);
You might also like:
Hey there! If you're working with PHP on Ubuntu 23.04 and find yourself needing JSON support, you're in the righ...
Feb-05-2024
In this article, we will see the laravel 8 image upload example. Image or file upload is the most common task...
Oct-06-2020
If you work in the web hosting sector, you're probably acquainted with the term reseller hosting. Resellers make up...
Apr-07-2022
In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of method...
Jun-05-2022