2

Consider the following command.

CellPrint[Cell["[1,2,3]", "ExternalLanguage"]]

It creates a python cell with command [1,2,3].

If evaluated then the result is {1,2,3}

Is there a way to Evaluate the cell expression programmatically and get the result in a variable. Like following,

result=CellEvaluate[Cell["[1,2,3]", "ExternalLanguage"]];

So now the result variable contains {1,2,3}.

Glorfindel
  • 547
  • 1
  • 8
  • 14
user13892
  • 9,375
  • 1
  • 13
  • 41

1 Answers1

4

You really want to be able to use the same evaluator as the .nb does, so we'll figure out what the .nb does:

CurrentValue[
 EvaluationNotebook[], {StyleDefinitions, "ExternalLanguage", 
  CellEvaluationFunction}]

FrontEnd`Private`ExternalLanguageCellEvaluationFunction

Then look at the DownValue:

GeneralUtilities`PrintDefinitionsLocal@
 FrontEnd`Private`ExternalLanguageCellEvaluationFunction

And we see it just calls this:

ExternalEvaluate`FE`ExternalCellEvaluate

Which we dig into and get that it's just working off of this Association:

ExternalEvaluate`FE`$CellSessions

And then we use that like so:

ExternalEvaluate[ExternalEvaluate`FE`$CellSessions["Python"], "1"]

And now you can programmatically access .nb definitions.

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • Beautiful ... is there a way to look at the definitions of all builtin functions using GeneralUtilities? – user13892 Feb 04 '19 at 22:40
  • Not sure what you mean. But you can look at many of them using that function. Some don’t expose their definitions. – b3m2a1 Feb 04 '19 at 22:41
  • If i apply this function to say Sin I see that it says <>. Does it mean that it is not written in the wolfram language so is part of the kernel written in C? – user13892 Feb 04 '19 at 22:56
  • @user13892 yeah a huge amount of the functionality in Mathematica is actually implemented in C++ – b3m2a1 Feb 04 '19 at 22:57
  • Also if i create a package in which i give the function the attribute Locked, Protected and ReadProtected and then use this GeneralUtilities`PrintDefinitionsLocal on it. Will i be able to see the definitions? – user13892 Feb 04 '19 at 23:04
  • Nope. Not possible. – b3m2a1 Feb 04 '19 at 23:05
  • If possible provide the general solution where i can evaluate the Cell expression and get the result even for those which are not External Language Cells. – user13892 Feb 05 '19 at 21:56
  • @user13892 not sure what you mean... there don't you just use ExternalEvaluate? – b3m2a1 Feb 05 '19 at 22:08