PHP code example of omaralalwi / laravel-py

1. Go to this page and download the library: Download omaralalwi/laravel-py library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

omaralalwi / laravel-py example snippets




use LaravelPy;

class LaravelPyController
{
  public function testLaravelPy() {
      $laravelPy = app(LaravelPy::class);
      $script = 'total_calculator.py';
      $arguments = [10, 20, 30];
          
      try {
           $result = $laravelPy
              ->loadScript($script)
              ->withArguments($arguments)
              ->run();
              
          print_r($result); // 60.0
      } catch (Exception $e) {
          echo "Error: " . $e->getMessage();
      }
  }
}



use LaravelPy;
use Omaralalwi\PhpPy\Managers\ConfigManager;

class LaravelPyController
{
  public function testLaravelPy() 
  {
     try {
        $laravelPy = app(LaravelPy::class);
        $script = 'advance_example.py';

        $numbers = [2,4, 5,7,9];

        $config = new ConfigManager([
            'scripts_directory' => 'phpPyScripts',
            'python_executable' => '/usr/bin/python3',
            'max_timeout' => 120,
        ]);

        $result = $laravelPy
            ->setConfig($config)
            ->loadScript($script)
            ->withArguments($numbers)
            ->withEnvironment(['FIRST_ENV_VAR' => 10, 'SECOND_ENV_VAR' => 'second var value'])
            ->timeout(60)
            ->asJson()
            ->run();

        print_r(json_encode($result));
    } catch (\Exception $e) {
        print_r("Error: " . $e->getMessage());
    }
  }
}
bash
php artisan vendor:publish --tag=laravel-py