Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Set the default external session used by external language cells
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. |
| "Language" | "Python" | specifies which external language to use as a String (e.g. "Shell", "Python", etc.) |
| "Clean" | False | whether to close existing sessions and reregister before running |
Create an external session and set it as the default for "ExternalLanguage" cells:
| In[1]:= |
|
| Out[1]= |
|
Set a value in the session:
| In[2]:= |
|
Check that the "ExternalLanguage" cell uses the same session:
|
x
|
| Out[3]= |
|
Reading and writing values works bidirectionally:
|
x = 2.71828
|
| In[4]:= |
|
| Out[4]= |
|
Start a new session for the "ExternalLanguage" cell with a specific python executable:
| In[5]:= |
|
| Out[2]= |
|
| In[6]:= |
|
| Out[6]= |
|
Now the python external language cell session will have the specified environment:
|
import numpy
x = numpy.ones([10,10])
|
| In[7]:= |
|
| Out[7]= |
|
Bind an existing session object to the "ExternalLanguage" cell:
| In[8]:= |
|
| Out[8]= |
|
| In[9]:= |
|
| Out[9]= |
|
| In[10]:= |
|
| Out[10]= |
|
Verify the external language cell is running the same session:
|
x
|
| Out[11]= |
|
Remove prior external sessions and start anew:
| In[12]:= |
|
| Out[12]= |
|
Now there is a single session:
| In[13]:= |
|
| Out[13]= |
|
Verify with the external language cell:
| In[14]:= |
|
|
x
|
| Out[15]= |
|
The "Language" option may be set to any properly configured external evaluation system:
| In[16]:= |
|
| Out[16]= |
|
| In[17]:= |
|
| Out[17]= |
|
Verify the external language cell is running the newly created Julia session:
|
x = sqrt(144)
|
| Out[21]= |
|
| In[22]:= |
|
| Out[22]= |
|
Change the executable of the "Shell" session:
| In[23]:= |
|
| Out[23]= |
|
Now it uses ZSH instead of BASH:
|
echo $0
|
| Out[24]= |
|
You can export an environmental variable in the session:
| In[25]:= |
|
| Out[25]= |
|
And verify that the "Shell" external cell uses that same session:
|
echo $VAL
|
| Out[26]= |
|
When you have one or more different sessions, you can explicitly access them individually with ExternalEvaluate:
| In[27]:= |
|
| 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]:= |
|
|
name
|
| Out[12]= |
|
This work is licensed under a Creative Commons Attribution 4.0 International License