-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SymPy equation rendering support such that the ouput is on parity with Jupyter's rendering when using SymPy. #1412
Comments
In the meantime, you can do this: from sympy import latex
import marimo as mo
def print_sympy(obj):
return mo.md(f"""
\[
{latex(obj)}
\]
""") Playground: https://marimo.app/l/n2kkic |
Adding on to the solution by @mscolnick, you can try adding the ...
from sympy import Symbol as SymbolSympy
from sympy import Integral as IntegralSympy
class Symbol(SymbolSympy):
def _mime_(self) -> tuple[str, str]:
return ("text/html", print_sympy(self).text)
class Integral(IntegralSympy):
def _mime_(self) -> tuple[str, str]:
return ("text/html", print_sympy(self).text)
...
x, y, z = symbols('x y z', cls=Symbol)
... Playground: https://marimo.app/l/t1huwf Updating to add that if you would rather like to use monkey-patching for adding the rendering method, you could change def patch_sympy(*objs):
"""adds the _mime_() method to the sympy objects
e.g.
Symbol._mime_ = sympy_html
example:
patch_sympy(Symbol, Integral)
"""
for obj in objs:
# the lambda below is our sympy_html
obj._mime_ = lambda obj: ("text/html", mo.md(f"""\[{latex(obj)}\]""").text)
...
from sympy import Symbol, Integral
patch_sympy(Symbol, Integral)
...
x, y, z = symbols('x y z') |
@mscolnick Thanks! It turns out that patching |
Description
There is currently no way to render equations properly when using SymPy. In Jupyter notebook one usually just calls init_printing() at the beginning of a notebook to allow all subsequent output to be rendered in mathematical notation.
Importing SymPy in and calling init_printing() has absolutely no effect and equations are simple output as text.
To see the issue just follow the SymPy documentation for setting up this feature https://docs.sympy.org/latest/tutorials/intro-tutorial/printing.html
Suggested solution
Since SymPy has multiple fallbacks on how to render the output, it may be a case of initially capturing the resulting MathJax and rendering using Marimo's md function, just a guess.
Alternative
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: