Giter Club home page Giter Club logo

phpython's Introduction

PHPPython

An extension to eval python codes in PHP

Requirements

  • pybind11 V2.1.1
  • PHP-CPP-LEGACY V1.5.7 / PHP-CPP V2.x
  • PHP 5 / PHP 7
  • Python 3
  • C++ 11

Example

Variables defined in Python

$code = <<<EOD
a = [1, 2, 3]
EOD;
$python = new Python();
$python->eval($code);
var_export($python->extract("a"));

... you can use extract method in php to get that python variable, codes above output:

array (
  0 => 1,
  1 => 2,
  2 => 3,
)

Variables defined in PHP

$a = ["a" => "b", "c" => "d"];
$code = <<<EOD
print(a)
EOD;

$python = new Python();
$python->assign("a", $a);
$python->eval($code);

... you can use assign method in php to let python know that php variable, codes above output:

{'a': 'b', 'c': 'd'}

Functions defined in Python

$a = ["a" => "b", "c" => "d"];
$code = <<<EOD
def dofunc(arg1, arg2):
   print("Python Output")
   print(arg1)
   print(arg2)
   return {"Python": {"a": arg1, "b": arg2}}

after = "abcd"

EOD;

$python = new Python();
$python->eval($code);
$python->assign("tmp", $a);
var_dump("PHP Here...", $python->dofunc($a, "py::tmp"));

... you can call functions defined in Python as-is-a $python->method(), codes above output:

Python Output

{'a': 'b', 'c': 'd'}
{'a': 'b', 'c': 'd'}
string(11) "PHP Here..."
array(1) {
  'Python' =>
  array(2) {
    'a' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
    'b' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
  }
}

Here, We can also use these ways to call functions defined in Python:

var_dump("PHP Here...", $python->call("dofunc", [$a, "py::tmp"]));
var_dump("PHP Here...", $python->call("dofunc(after, tmp)"));

this will output:

Python Output
{'a': 'b', 'c': 'd'} // $a in php
{'a': 'b', 'c': 'd'} // tmp in python which assigned by php
string(11) "PHP Here..."
array(1) {
  'Python' =>
  array(2) {
    'a' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
    'b' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
  }
}
Python Output
abcd // after in python
{'a': 'b', 'c': 'd'} // tmp in python which assigned by php
string(11) "PHP Here..."
array(1) {
  'Python' =>
  array(2) {
    'a' =>
    string(4) "abcd"
    'b' =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
  }
}

Functions defined in PHP

$code = <<<EOD
tmp = {'a': 'b', 'c': 'd'}
print("Python begin")
print(dofunc(tmp))
print("Python end")
EOD;

$python = new Python();
$python->def("dofunc", function($param) {
    echo __function__ . " in PHP: Get params from Python :" . PHP_EOL;
    echo var_export($param, true) . PHP_EOL;
    return [
        "php" => $param
    ];
});
$python->eval($code);

... you can call php funciton in python as-is-a real python function, codes above output:

Python begin
{closure} in PHP: Get params from Python :
array (
  0 =>
  array (
    'a' => 'b',
    'c' => 'd',
  ),
)
{'php': [{'a': 'b', 'c': 'd'}]}
Python end

if you like, you can also call that function using the php way, like this:

var_dump($python->dofunc("py::tmp", "phpString"));

this will output:

{closure} in PHP: Get params from Python :
array (
  0 =>
  array (
    'a' => 'b',
    'c' => 'd',
  ),
  1 => 'phpString',
)
array(1) {
  'php' =>
  array(2) {
    [0] =>
    array(2) {
      'a' =>
      string(1) "b"
      'c' =>
      string(1) "d"
    }
    [1] =>
    string(9) "phpString"
  }
}

PHP Call Python Function

PHP Call Python Function

Python Call PHP Function

Python Call PHP Function

phpython's People

Contributors

guweigang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.