2
$\begingroup$

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}.

$\endgroup$
4
  • $\begingroup$ Why not just use ExternalEvalute? $\endgroup$
    – b3m2a1
    Feb 4 '19 at 22:17
  • $\begingroup$ Because it is currently not connected with ExternalLanguage Cells. If you define some function in ExternalLanguage Cells and use ExternalEvaluate["Python","..."] then this does not have access to those definitions since they both use different kernel session of python. This is a bug which is confirmed to be fixed in 12.0 but i want some workaround now. $\endgroup$
    – user13892
    Feb 4 '19 at 22:23
  • $\begingroup$ Ah just look into the CellEvaluationFunction then. I'll post an answer. $\endgroup$
    – b3m2a1
    Feb 4 '19 at 22:24
  • $\begingroup$ See also WFR SetLanguageCellSession as used in 236298/61736 $\endgroup$
    – Sterling
    May 19 at 14:05
4
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ Beautiful ... is there a way to look at the definitions of all builtin functions using GeneralUtilities? $\endgroup$
    – user13892
    Feb 4 '19 at 22:40
  • $\begingroup$ Not sure what you mean. But you can look at many of them using that function. Some don’t expose their definitions. $\endgroup$
    – b3m2a1
    Feb 4 '19 at 22:41
  • $\begingroup$ If i apply this function to say Sin I see that it says <<kernel function>>. Does it mean that it is not written in the wolfram language so is part of the kernel written in C? $\endgroup$
    – user13892
    Feb 4 '19 at 22:56
  • $\begingroup$ @user13892 yeah a huge amount of the functionality in Mathematica is actually implemented in C++ $\endgroup$
    – b3m2a1
    Feb 4 '19 at 22:57
  • $\begingroup$ 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? $\endgroup$
    – user13892
    Feb 4 '19 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.