# How to Evaluate a Cell written in code and store the result in a variable

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

• Why not just use ExternalEvalute? Feb 4 '19 at 22:17
• 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. Feb 4 '19 at 22:23
• Ah just look into the CellEvaluationFunction then. I'll post an answer. Feb 4 '19 at 22:24
• See also WFR SetLanguageCellSession as used in 236298/61736 May 19 at 14:05

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

FrontEndPrivateExternalLanguageCellEvaluationFunction


Then look at the DownValue:

GeneralUtilitiesPrintDefinitionsLocal@
FrontEndPrivateExternalLanguageCellEvaluationFunction


And we see it just calls this:

ExternalEvaluateFEExternalCellEvaluate


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

ExternalEvaluateFE$CellSessions  And then we use that like so: ExternalEvaluate[ExternalEvaluateFE$CellSessions["Python"], "1"]


And now you can programmatically access .nb definitions.

• Beautiful ... is there a way to look at the definitions of all builtin functions using GeneralUtilities? Feb 4 '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. Feb 4 '19 at 22:41
• 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? Feb 4 '19 at 22:56
• @user13892 yeah a huge amount of the functionality in Mathematica is actually implemented in C++ Feb 4 '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 GeneralUtilitiesPrintDefinitionsLocal on it. Will i be able to see the definitions? Feb 4 '19 at 23:04