ExternalFunction

ExternalFunction[sys,"f"]

represents an external function named "f" defined in the external evaluator sys.

ExternalFunction[session,"f"]

represents an external function "f" in the specified external session.

ExternalFunction[sys,"code"]

represents an external function defined by the code fragment "code".

Details

  • In ExternalFunction[sys,"code"], "code" is typically a pure or lambda function in the external system.
  • In ExternalFunction[sys, ], possible choices for sys include:
  • "Python"Python
    "NodeJS"JavaScript running through Node.js
  • ExternalFunction[session,"f"] returns a Failure object if "f" does not exist in session.

Examples

open allclose all

Basic Examples  (2)

Create an external function using the absolute value function in Python:

Compute the absolute value of with the external Python function:

Start a new session:

Define a new function:

Use the function:

Close the session:

Scope  (4)

Define a lambda function in Python:

Use this Python function in the Wolfram Language:

Use a Python built-in function:

Use the external function to convert a string to a list:

Start a session:

Define a function using complex numbers:

Compute the argument of a complex number via the Python phase function:

Close the session:

Start a NodeJS session:

In NodeJS, it is not enough to just define a functionyou have to explicitly return the function for ExternalFunction to recognize it as a callable function:

Call the function:

Close the session:

Applications  (3)

Start a new session:

Define a function in the session:

Create an ExternalFunction from this Python function:

Use the function within normal Wolfram Language code:

Start a new session:

Import the Python NumPy package to load additional functionality:

Call the external function:

It partitioned the list into a 3×4 matrix:

The equivalent Wolfram Language command:

Start a new session:

Define a Python function that imports an image from a URL and rotates it:

The result is a ByteArray:

Import the ByteArray to display the rotated image:

Introduced in 2019
 (12.0)