Function Repository Resource:

SetLanguageCellSession

Source Notebook

Set the default external session used by external language cells

Contributed by: Michael Sollami

ResourceFunction["SetLanguageCellSession"][]

creates the external session which "ExternalLanguage" cells will use.

ResourceFunction["SetLanguageCellSession"]["target"]

uses the executable "target" for the external session.

ResourceFunction["SetLanguageCellSession"][session]

uses an existing ExternalSessionObject.

Details and Options

By default, ResourceFunction["SetLanguageCellSession"] uses "Python".
The argument "target" must be a string file path to a registered executable, e.g. the value for key "Target" in StartExternalSession[assn].
ResourceFunction["SetLanguageCellSession"] returns the active ExternalSessionObject that it binds to the "ExternalLanguage" cell for the specified system.
ResourceFunction["SetLanguageCellSession"] takes two options:
"Language""Python"specifies which external language to use as a String (e.g. "Shell", "Python", etc.)
"Clean"Falsewhether to close existing sessions and reregister before running
ResourceFunction["SetLanguageCellSession"] works with any system supported by ExternalEvaluate.
The key combination can be used to create a new "ExternalLanguage" cell.

Examples

Basic Examples (3) 

Create an external session and set it as the default for "ExternalLanguage" cells:

In[1]:=
s = ResourceFunction["SetLanguageCellSession"][]
Out[1]=

Set a value in the session:

In[2]:=
ExternalEvaluate[s, "x = 3.14159"]

Check that the "ExternalLanguage" cell uses the same session:

x
Out[3]=

Reading and writing values works bidirectionally:

x = 2.71828
In[4]:=
ExternalEvaluate[s, "x"]
Out[4]=

Start a new session for the "ExternalLanguage" cell with a specific python executable:

In[5]:=
pyEnv = FindExternalEvaluators["Python"][1, "Target"]
Out[2]=
In[6]:=
session = ResourceFunction["SetLanguageCellSession"][pyEnv]
Out[6]=

Now the python external language cell session will have the specified environment:

import numpy x = numpy.ones([10,10])
In[7]:=
ExternalEvaluate[session, "x"]
Out[7]=

Bind an existing session object to the "ExternalLanguage" cell:

In[8]:=
s = StartExternalSession["Python"]
Out[8]=
In[9]:=
ExternalEvaluate[s, "x=5**2; x"]
Out[9]=
In[10]:=
ResourceFunction["SetLanguageCellSession"][s]
Out[10]=

Verify the external language cell is running the same session:

x
Out[11]=

Options (3) 

Remove prior external sessions and start anew:

In[12]:=
s = ResourceFunction["SetLanguageCellSession"]["Clean" -> True]
Out[12]=

Now there is a single session:

In[13]:=
Length@ExternalSessions[]
Out[13]=

Verify with the external language cell:

In[14]:=
ExternalEvaluate[s, "x = 1.61803"]
x
Out[15]=

The "Language" option may be set to any properly configured external evaluation system:

In[16]:=
FindExternalEvaluators["Julia"]
Out[16]=
In[17]:=
s = ResourceFunction["SetLanguageCellSession"]["Language" -> "Julia"]
Out[17]=

Verify the external language cell is running the newly created Julia session:

x = sqrt(144)
Out[21]=
In[22]:=
ExternalEvaluate[s, "x"]
Out[22]=

Change the executable of the "Shell" session:

In[23]:=
s = ResourceFunction["SetLanguageCellSession"]["/bin/zsh", "Language" -> "Shell"]
Out[23]=

Now it uses ZSH instead of BASH:

echo $0
Out[24]=

You can export an environmental variable in the session:

In[25]:=
ExternalEvaluate[s, "export VAL=1"]
Out[25]=

And verify that the "Shell" external cell uses that same session:

echo $VAL
Out[26]=

Applications (3) 

When you have one or more different sessions, you can explicitly access them individually with ExternalEvaluate:

In[27]:=
sessions = Table[StartExternalSession["Python"], 9];
ExternalEvaluate[sessions[[7]], "name = 'Seven of Nine'; name"]
Out[10]=

However, "ExternalLanguge" cells are much more convenient for multiline code, but simply creating such a cell (shift + >) will start a new and inaccessible session:

name
Out[28]=

Using SetLanguageCellSession allows you toggle between them:

In[29]:=
ResourceFunction["SetLanguageCellSession"][sessions[[7]]];
name
Out[12]=

Publisher

Michael Sollami

Version History

  • 1.0.1 – 09 August 2021
  • 1.0.0 – 11 December 2020

Related Resources

License Information