Skip to content
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

Scrapper for Sympy output #1276

Open
kakila opened this issue Mar 20, 2024 · 6 comments
Open

Scrapper for Sympy output #1276

kakila opened this issue Mar 20, 2024 · 6 comments

Comments

@kakila
Copy link

kakila commented Mar 20, 2024

Is there an scrapper for Sympy output?
Any hints?

Thanks!

@lucyleeow
Copy link
Contributor

What is the output for Sympy? Does it output html? If so https://sphinx-gallery.github.io/stable/configuration.html#capture-repr may be useful?

@kakila
Copy link
Author

kakila commented May 9, 2024

Thanks for the answer and sorry for my delayed response.
Sympy can produce mathML output which I was trying to use for a repr_html method.
I have poseted here my current status
https://stackoverflow.com/questions/78384241/capturing-sympy-math-output-in-sphinx-gallery
Can you make somethig out of that ?

@lucyleeow
Copy link
Contributor

Can you write a scraper for Sympy and just add the math directive to latex markup?

https://sphinx-gallery.github.io/stable/advanced.html#write-a-custom-image-scraper

@kakila
Copy link
Author

kakila commented May 9, 2024

I am not sure how to do this. Mainly because the doc in scrapper is for images. How to go about capturing some mathML or latex output?
Would you use the code inside the block argument to call sympy.latex on it and then insert the result on a ..math: directive?
It isn't clear for me how to do this robustly.

I thought of using something like the _repr_html_ output capture and insert the mathML output from sympy on it. But somehow is not working (see stackoverflow post). It is not working because the sympy mathML has this <apply> command that seems to not work on sphinx/rst.
Or maybe it is the encoding of the string, I am not sure. I couldn't make it work.
The idea is to be able to generate math that is rendered on the browser, so no latex installation is needed (e.g. by generating images).

@kakila
Copy link
Author

kakila commented May 16, 2024

Ok, I found a way to make mathjax render latex code.

"""
Testing sympy mathml
====================
"""
# %%
# Should show this
#
# .. math::
#     \dot{x} = x y^2 - \frac{\sqrt{x}}{y}
#
from sympy import latex
from sympy.core.evalf import EvalfMixin


def print_html(expr):
    return f'{latex(expr, mode="equation*", itex=True)}'


EvalfMixin._repr_html_ = print_html

from sympy import symbols, Symbol, Eq, sqrt

x, y = symbols("x y")

expr = Eq(Symbol(r"\dot{x}"), x * y ** 2 - sqrt(x) / y)
expr

However, this however will not work with , e.g. mathimg extension (svg math).
Any idea is welcomed.

Could any give me a head start on how to produce a more robust solution (e.g. instead of monkey patching)?

UPDATE: for gettgin matrices also rendered another class must be monkey patched

from sympy.matrices.repmatrix import MutableRepMatrix

MutableRepMatrix._repr_html_ = print_html

@lucyleeow
Copy link
Contributor

An image scraper is originally intended for images but essentially its a function that is run at the end of each code block and should return ReST that gets added to the example, after the code block. It could be made to work for Sympy.

If you look at the matplotlib scrapper:

for fig_num, image_path in zip(plt.get_fignums(), image_path_iterator):
image_path = PurePosixPath(image_path)
if "format" in kwargs:
image_path = image_path.with_suffix("." + kwargs["format"])
# Convert figure number to Figure.
fig = plt.figure(fig_num)

it iterates through all the figures via plt and saves them. Potentially there is a way to do something similar for equations with Sympy?

Alternatively I have found this comment: #1138 (comment), maybe Sympy uses mimebundle?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants