ExternalEvaluate

ExternalEvaluate["sys","cmd"]

evaluates the command cmd in the external evaluator sys, returning an expression corresponding to the output.

ExternalEvaluate[{"sys",opts},"cmd"]

uses the options opts for the external evaluator.

ExternalEvaluate[assoc,"cmd"]

evaluates cmd using the external evaluator specified by assoc.

ExternalEvaluate[session,"cmd"]

evaluates cmd in the specified running ExternalSessionObject.

ExternalEvaluate[sys"type",]

returns output converted to the specified type.

ExternalEvaluate[spec,obj]

evaluates the content of the specified File, URL or CloudObject.

ExternalEvaluate[spec,assoc]

evaluates the command specified by assoc.

ExternalEvaluate[spec,{cmd1,cmd2,}]

evaluates the list of commands cmdi.

ExternalEvaluate[spec]

represents an operator form of ExternalEvaluate that can be applied to a command or object.

Details

  • ExternalEvaluate[sys,"cmd"] returns either a string representing the result of evaluating cmd using the external evaluator system or a Failure object.
  • To be usable with ExternalEvaluate, language installations must have a standard 0MQ library installed for that particular language, as well as a "JSON" library.
  • In ExternalEvaluate[sys,obj], the contents of the external object obj will be used as the command. The external object can be File[], URL[], CloudObject[] or LocalObject[].
  • In ExternalEvaluate[sys,], possible choices of sys include:
  • "Python"Python
    "Python-NumPy"Python with NumPy loaded
    "Python-PIL"Python with the Python Image Library (PIL) loaded
    "NodeJS"JavaScript running through Node.js
    "WebDriver-Chrome"Chrome web browser
    "WebDriver-Chrome-Headless"Chrome web browser running headless
    "WebDriver-Firefox"Firefox web browser
    "WebDriver-Firefox-Headless"Firefox web browser running headless
    ExternalSessionObject[]session object started by StartExternalSession
  • In ExternalEvaluate[assoc,cmd], elements of the association can include:
  • "System"the external system or language to start
    "Version"version of the external system to use (e.g. "3.6")
    "Executable"the executable to use for evaluating code
    "ReturnType"type of object to return ("String", "Expression", ...)
    "Prolog"code to run before the command
    "Epilog"code to run after the command
    "SessionProlog"code to run before the session
    "SessionEpilog"code to run after the session
  • Possible settings for "type" in ExternalEvaluate[sys->"type",], or for "ReturnType", include:
  • "Expression"attempt to convert to a Wolfram Language expression
    "String"give the raw string output by the external evaluator
  • By default, ExternalEvaluate effectively uses the setting "ReturnType""Expression".
  • The interpreter used to convert the output depends on the system used. For Python, the default is "PythonExpression" and for JavaScript, the default is "JavaScriptExpression".
  • In the form ExternalEvaluate[{"sys",opts},], the possible options are the same as the elements for the association in ExternalEvaluate[assoc,].
  • ExternalEvaluate[sys,] is effectively equivalent to ExternalEvaluate[StartExternalSession[sys],].
  • ExternalEvaluate[sys,cmd] launches the external evaluator, evaluates cmd, then exits the external session.
  • ExternalEvaluate[session,cmd] sends the command to a running session and does not exit the session.
  • When sys is specified as a string, the session options used are searched first for user-specified options specified using RegisterExternalEvaluator, then from built-in session options included with the system or automatically discovered.
  • FindExternalEvaluators gives a dataset of evaluator systems that can be used.
  • Individual write operations to standard output are immediately printed to the notebook or terminal.

Examples

open allclose all

Basic Examples  (6)

Evaluate a simple arithmetic expression in Python:

In[1]:=
Click for copyable input
Out[1]=

Evaluate a basic math function in JavaScript using Node.js:

In[1]:=
Click for copyable input
Out[1]=

Capture a webpage from a web browser:

In[1]:=
Click for copyable input
Out[1]=

Import a library in Python and use a function:

In[1]:=
Click for copyable input
Out[1]=

Evaluate multiple lines of code in a Python session:

In[1]:=
Click for copyable input
Out[1]=

Use > at the beginning of a line to start an external code cell evaluated with ExternalEvaluate:

2+2
Out[2]=

Scope  (6)

Generalizations & Extensions  (4)

Applications  (3)

Properties & Relations  (1)

Possible Issues  (8)

Interactive Examples  (1)

See Also

StartExternalSession  RunProcess  Run  URLExecute  EmbedCode  SystemInstall  "Python"  "NodeJS"  "WebDriver-Chrome"  "WebDriver-Firefox"

Related Workflows

Introduced in 2017
(11.2)
| Updated in 2018
(11.3)