2
$\begingroup$

We can type > and select python to insert python external language cell into notebook. Then we can write python code inside it and evaluate it by pressing Enter.

Now I want to do a similar thing. Make my own cell that would work similar as external language cell, but instead of sending content of the cell to python kernel it would send its content into Mathematica own function.

Say, the function is:

myowncellevaluator[x_] := 
 Module[{}, Print["Hello"]; ToExpression[StringSplit[x, "\n"]]]

And the cell could look for example like this:

n=2
7*n^2

Then by pressing Enter in the cell I want to send its content in the form of string into function myowncellevaluator - meaning I would get output of myowncellevaluator["n=2\n7*n^2"] in output cell, which would be:

"Hello"
{2, 28}

How to create such a cell?

The cell could look like this after evaluation:

enter image description here

$\endgroup$
5
  • $\begingroup$ Can you explain what the goal of this would be? How would this need to be different from a regular input cell and perhaps the $Pre and $Post directives? $\endgroup$
    – MarcoB
    Sep 10, 2022 at 17:10
  • $\begingroup$ I am working on external evaluate for Sagemath inside Mathematica. I can do it in the form sage[code], code is string that contains Sagemath code. So I have to use escape characters inside string like \", \\n, \'... If I can write Sagemath code in cell like it is in cell for Python it would be more elegant. $\endgroup$ Sep 10, 2022 at 17:30
  • $\begingroup$ @MarcoB: Basically I would like to copy exactly the same properties as Python cell have just with one exception. Instead of sending code to Python kernel it would send it to my function sage which in turn would send it to Sagemath kernel. $\endgroup$ Sep 10, 2022 at 17:33
  • $\begingroup$ I think this might be an XY problem as formulated. Your underlying problem is how to run Sage code from within Mathematica. Perhaps you should focus your question on that topic, rather than asking about your potential solution. You might also be interested in How to extend External Language Input?. The topic has come up before (see Calling Sage functions from Mathematica but unfortunately there was no answer at that time. $\endgroup$
    – MarcoB
    Sep 10, 2022 at 17:52
  • $\begingroup$ I already know how to run Sage code in Mathematica. So this is not my question. I can run Sage code when the code is written inside string, but I would like also the way when the code would be written inside cell instead of string. $\endgroup$ Sep 10, 2022 at 18:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.