12
$\begingroup$

How do I configure new External Language Input cell (that appears when pressing >) to use my instance of Python? I'm aware of ExternalEvaluate, but is there a way to attach ExternalSessionObject to this cell:

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ @Edmund can you use a specific ExternalSessionObject in the "ExternalLanguage" cell? If it just uses ExternalEvaluate["Python", ...] then it won't use a cached session by default. $\endgroup$
    – b3m2a1
    Mar 10 '18 at 21:26
  • 1
    $\begingroup$ @b3m2a1 I think you are correct. It is not clear how to attach a specific ExternalSessionObject to the notebook cell. Withdrawing my close vote. $\endgroup$
    – Edmund
    Mar 11 '18 at 1:10
  • $\begingroup$ @Coolwater this isn't a duplicate as this is specifically how to use the new-in-11.3 feature of external input cells with a specific ExternalSessionObject. $\endgroup$
    – b3m2a1
    Mar 11 '18 at 10:10
  • $\begingroup$ Possible duplicate of run python with package numpy in mathematica $\endgroup$ Mar 11 '18 at 11:17
12
$\begingroup$

This has to be done at the stylesheet level and here's a way to do it. First we'll make a new CellEvaluationFunction that uses the TaggingRules to bind the ExternalSessionObject. Then we'll attach that to the "ExternalLanguage" cell. Here's the function:

currentCellExternalSessionEvaluate =
  Function[
   With[{ExternalEvaluate`FE`Private`cell = EvaluationCell[]},
    With[{ExternalEvaluate`FE`Private`sessions =
       Replace[
        CurrentValue[
         ExternalEvaluate`FE`Private`cell,
         {
          TaggingRules,
          "ExternalSessions"
           }
         ],
        Except[_?OptionQ] :>
         CurrentValue[
          EvaluationNotebook[],
          {
           TaggingRules,
           "ExternalSessions"
            },
          Replace[ExternalEvaluate`FE`$CellSessions,
           Except[_?OptionQ] -> <||>
           ]
          ]
        ]
      },
     Block[{ExternalEvaluate`FE`$CellSessions = 
        Association@ExternalEvaluate`FE`Private`sessions},
      FrontEnd`Private`ExternalLanguageCellEvaluationFunction[##]
      ]
     ]
    ]
   ];

Then we can add this to our stylesheet like so:

Get["http://raw.githubusercontent.com/b3m2a1/mathematica-tools/master/StylesheetEdit.wl"]

StyleSheetEdit["ExternalLanguage", 
 CellEvaluationFunction -> currentCellExternalSessionEvaluate,
 "MakeCell" -> True
 ]

And finally we'll start a session and bind it to our notebook:

myPy = FileNameJoin@{$UserDocumentsDirectory, "Python", "config", 
    "python3.4", "bin", "python3"};

CurrentValue[EvaluationNotebook[],
  {TaggingRules, "ExternalSessions", "Python"}
  ] = StartExternalSession[{"Python", "Executable" -> myPy}]

Then I can just call:

import sys
print(sys.version)

3.4.4 (v3.4.4:737efcadf5a6, Dec 19 2015, 20:38:52) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

(somehow my ExternalSessionObject can't parse any results, but this is just proof of concept)

Then I can remove the binding:

CurrentValue[EvaluationNotebook[],
   {TaggingRules, "ExternalSessions", "Python"}
   ] = Inherited;

And it all works as normal:

import sys
sys.version

2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]

Note that the TaggingRules may also be applied to specific cells to make this more fine-tuned.

$\endgroup$
5
  • $\begingroup$ That's pretty sophisticated, but very cool! $\endgroup$
    – swish
    Mar 11 '18 at 2:51
  • 2
    $\begingroup$ This once worked but seems to have stopped. Somehow the currentCellExternalSessionEvaluate now shows a syntax error... $\endgroup$
    – M.R.
    Nov 30 '20 at 19:58
  • $\begingroup$ @b3m2a1 This is a very valuable answer, could you please update it when you can, I'm still sure why it's failing now in 12 $\endgroup$
    – M.R.
    Nov 30 '20 at 20:33
  • 2
    $\begingroup$ @M.R. This seems to now be implemented via EvaluationMode -> "ExternalLanguage". It's not clear to me that this is accessible anymore. I'd contact WRI to tell them to tell John Fultz that he fracked up a good thing. $\endgroup$
    – b3m2a1
    Nov 30 '20 at 20:57
  • $\begingroup$ @b3m2a1 See this answer which works in 12.2 $\endgroup$
    – M.R.
    Dec 11 '20 at 1:23

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.