How can I use the sympy integration in a normal Python session?

Hi all,

Is there a reason why there are individual modes for matplotlib, sympy and normal “Python”? It appears that they all run in different Python processes, so I cannot get both plotting and sympy pretty printing.

Any help would be appreciated.

No fundamental reason, but I guess they implemented different ways to communicate multimedia content to TeXmacs. You should try to use one to do all you need, so you can share data. Any hint on how to improve the current state of affairs is appreciated.

I actually figured this out (partially). A diff for the tmpy plugin:

diff --git a/plugins/tmpy/session/tm_python.py b/plugins/tmpy/session/tm_python.py
index 91ec1a279..2b9f91eae 100755
--- a/plugins/tmpy/session/tm_python.py
+++ b/plugins/tmpy/session/tm_python.py
@@ -41,6 +41,16 @@ if py_ver == 2:
 # import logging as log
 # log.basicConfig(filename='/tmp/tm_python.log',level=log.INFO)
 
+have_sympy = False
+try:
+    from sympy.printing import latex
+    from sympy import Basic, MatrixBase
+    import sympy
+    have_sympy = True
+except ModuleNotFoundError:
+    flush_verbatim ("Please install sympy first, for example:\n")
+    flush_verbatim ("    pip install sympy")
+
 def flush_output (data):
     """Do some parsing on the output according to its type.
     
@@ -59,6 +69,8 @@ def flush_output (data):
             flush_verbatim ("")
         else:
             flush_file (data.content)
+    elif have_sympy and isinstance (data, Basic) or isinstance (data, MatrixBase):
+        flush_latex (latex (data))
     else:
         flush_verbatim (str(data).strip())
 

It would be great if this can be upstreamed.

what is the problem in using the sympy plugin instead of the vanilla python one? I’m not sure what is the correct things to do to improve the situation. A single random patch would not help much, I’m afraid. @darcy suggestions on how to clarify the situation with these different plugins?

It is a good idea to enable sympy integration in a normal python session.

I’m planning to rewrite the python plugin later. For the past few months, I’m working on

And just learned a lot from Chang on python programming.

I have to say I’m not a big fan of auto-loading packages in the standard Python session. I agree that sympy is a wonderful package, but then why not load matplotlib, numpy, pandas, etc. etc.? How do you make those choices? Why not leave the choice to the user? Besides, it can slow down startup.

I agree with @jeroen. I do not think that we need to autoload anything in the standard python session, apart from some basic way to send structured output to TeXmacs (images and so on). But we maybe can come up with a way to merge all the various plugins in a single entry point (e.g. by definiting some python commands which then will load any supported library like matplotlib and symply and install appropriate interfaces to TeXmacs).

From my user’s point of view and for what it worth, I tend also to disagree on auto-loading python modules even if they’re that great.
Perhaps a scientific python plugin including all these stuff is better suited for this ?

I’ve checked what Jupyter does. There you get pretty printing without any additional configuration.

It seems that Jupyter checks if an object has a _repr_latex_() function. Also pandas has _repr_latex() and _repr_html_() fucntions. Perhaps we can leverage these to circumvent having to specify for each module how they should be flushed to TeXmacs.

I would say that it is probably good to have a global data structure that allows dynamic injection of flushers. For example, sympy provides init_printing() which registers its LaTeX formatter (although only for Jupyter).

We can have a function called load_sympy in tmpy that imports sympy and registers the sympy LaTeX printer. That would be very easy to implement, and also very extensible. @jeroen @darcy

I would think both approaches could be implemented in a complementary way.

Checking for _latex_repr_ is quick and easy. It would give us latex printing with literally 2 lines of code and practically no overhead if sympy is not installed:
https://codeberg.org/woutersj/tm-plugins/commit/6f365c86cfcd2e7be9a945d949415d2af81a6398

An extensible approach as you suggest could still be added on top, allowing for more flexibility from the user and to implement pretty printing for modules that don’t have _repr_latex.

Well, it is time to turn a plan into a Github repo. And here is my solution for sympy:

1 Like