Fast Export of Large MySQL Data to Excel using Laravel and Python

30th September 2023 Uncategorised No comments

Handling large datasets can be a challenge. When tasked with exporting thousands of rows from a MySQL database using Laravel, I found traditional methods lacking. This article showcases a solution that marries Laravel, Python, and Pandas for efficient data export.

Why Python with Pandas?
Python’s Pandas module is a powerhouse for data manipulation. Its prowess in swiftly exporting large datasets to Excel makes it an attractive choice for this challenge.

Integrating Symfony/Process in Laravel:
The symfony/process extension in PHP is our bridge to the Python world. It facilitates the execution of shell commands, in this case, running our Python script.

Laravel Export Function:
This function is the core of our solution. Here’s a breakdown:

$result = DB::table('full_report');
// ... [Database querying based on request parameters]
$sql = $result->toSql();
$bindings = $result->getBindings();
$filledSql = vsprintf(str_replace('?', "'%s'", $sql), $bindings);
$filename = 'FullReport-' . Carbon::now()->timestamp . '.xlsx';
$process = new Process(['/opt/homebrew/bin/python3', 'export.py', $filledSql, $filename]);
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
return response()->json(['success' => 'Export started successfully!']);

The function begins by querying the database based on request parameters. It then generates the SQL query and its bindings. Finally, using the Symfony Process, it runs the Python script, passing the SQL query and filename as arguments.

AJAX Request:
The AJAX request serves as the trigger for our export function.

    // ... [AJAX setup and data parameters]
    success: function(response) {
        if (response.success) {
            console.log("Success:", response.success);
    error: function(error) {
        console.error("Error:", error);

This asynchronous request sends data parameters to the server. Upon successful data export, it provides feedback to the user. In case of errors, they are logged for debugging.

Python Script (export.py):
This script is responsible for fetching and exporting the data.

import pandas as pd
import mysql.connector
import sys
import os
# ... [Database connection setup]
query = sys.argv[1]
filename = sys.argv[2]
df = pd.read_sql(query, cnx);
storage_path = os.path.join(os.path.dirname(__file__), '..', 'storage', 'app', 'public');
os.makedirs(storage_path, exist_ok=True);
df.to_excel(os.path.join(storage_path, filename), index=False);

The script connects to the MySQL database using mysql.connector. It then fetches the data using Pandas based on the SQL query passed from Laravel. The data is saved as an Excel file in Laravel’s storage directory.

By combining Laravel, Python, and Pandas, we achieve:

  • Speed: Rapid export of large datasets.
  • Flexibility: Harnessing the strengths of both PHP and Python.
  • Background Processing: Ensuring system responsiveness.

In web development, such integrative solutions can lead to enhanced efficiency and user satisfaction.

How to shorten (terminal) command line (bash) prompt (hide computer name and user name) for Mac users and Linux

10th April 2017 Uncategorised No comments

Mac’s display name and username occupy space on the command line. some users who are working regularly on terminal may be disturbed by how long the computer name and username appears on the terminal line, here’s how to hide them.

Open Terminal, then go to Terminal menu > Preferences > Shell then select “Run command:” and write the following command:


And here we go, no more computer name and username 🙂