marimo
marimo copied to clipboard
Sage syntax support
Description
Sagemath's syntax is a superset of Python (with some changes too, for example ^ means exponentiation).
It would be nice to support this syntax in some way.
Suggested solution
The sage preparse function could be used (if the user wants) on every cell before marimo processes it.
from sage.all import preparse
preparse("K.<a> = Numberfield(5)")
Moreover, to have the standard sage functions defined, something like from sage.all import * could be used (maybe hidden from the user).
Alternative
No response
Additional context
No response
Also, function? could be an alias for mo.doc(function). In sage there is also function?? which shows the implementation.
We've thought about supporting other syntaxes that have a Python wrapper / executable. Similar to how we do with mo.md() - for example, you can convert the editor to markdown to get markdown syntax highlighting and autocomplete.
We could do the same with Sage, but don't have it quite set up to be pluggable (ideally you can install "marimo[sage]"). However, this is likely this is a bit of ways out
mmm I think that the mo.md() trick is good for markdown, but not for code in genereal.
Sage files have a .sage extension. Maybe a first step could be to allow files with .sage extension too and in that case, all cells can use sage's syntax.
Different file-extensions that enforce a different syntax would be quite difficult. And I am not sure marimo would still work - it would not track dependencies well and depending if mo.ui. would still work, it would not be interactive.
Using the python library (from sage.all import) and marimo, are you using the reactive / interactive features? can you share a marimo + sage notebook you've created?
Different file-extensions that enforce a different syntax would be quite difficult
I don't think so... All the syntax and function decorators that marimo uses will just work in sagemath.
Notebook working now:
Ideally in a sagemath's notebook I would write
sinc(x)=sin(x)/x
And marimo would use preparse("sinc(x)=sin(x)/x")) to convert it to valid python code
__tmp__ = var("x")
sinc = symbolic_expression(sin(x) / x).function(x)
before doing anything else.
By default sage has x already defined. There is a script somewhere that defines it, and it also defines a lot of other functions (from sage.all import *). Having all of this defined by default would be nice, but optional.
Edit: I updated the file.
Different file-extensions that enforce a different syntax would be quite difficult I don't think so... All the syntax and function decorators that marimo uses will just work in sagemath.
marimo does a lot with AST reading in order to track dependencies. the Python AST parser won't likely work with sage, and thus would need to be rebuilt
I don't think it is needed to run the AST parser on the actual cells, but running it on preparse(cell_content) (which is just python code) would be enough for now.
The parser AST won't work for sage, and it is not trivial to implement... see https://github.com/sagemath/sage/issues/30760
By the way, x and such are defined by default using from sage.all_cmdline import *.
@mscolnick I tried to implement it in my fork: https://github.com/marimo-team/marimo/compare/main...Eloitor:marimo:sage_support
I'm sure this in not enough and more things need to be done....
Unfortunately I think there is a lot more, and it does diverge from our vision of a reactive notebook
I agree with @mscolnick. Sagemath is very big system. If some functionality in such a system is not envisaged from the beginning, it would be a pain (or imposible) to implement it by "patches" and afterthoughts later. Sympy could be used as lightweight CAS here.
Going to close out this issue.
Unfortunately, we don't intend to add support for other languages. marimo is very tightly coupled to Python, which lets us make marimo extra useful for Python developers, but makes it difficult to support arbitrary languages.