Visualize latex / math with PySide

I have a small program that outputs a typed equation on the fly using SymPy's "pretty-printing" tool. It works great, but doesn't look very professional. Since Sympy will produce latex or mml, I was wondering if they can be displayed graphically using the PySide widget? I obviously would need to change the "QTextBrowser ()", but I'm not sure. I know that Nokia provides QtMmlWidget, but I'm not sure if PySide can use it.

Many thanks and best wishes.

from __future__ import division
import sys
import sympy

from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtXml import *

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.browser.setCurrentFont(QFont("Courier New",10,QFont.Bold))
        self.lineedit = QLineEdit("please type an expression")
        self.lineedit.selectAll()
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        self.setLayout(layout)
        self.lineedit.setFocus()
        self.connect(self.lineedit, SIGNAL("textChanged (const QString&)"),self.updateUi)

    def updateUi(self):
        text = unicode(self.lineedit.text())
        for z in range(0,9):
            text = text.replace('x'+str(z),'x^'+str(z))
            text = text.replace(')'+str(z),')^'+str(z))
            text = text.replace(str(z)+'x',str(z)+'*x')
            text = text.replace(str(z)+'(',str(z)+'*(')

        try:
            self.browser.append(sympy.printing.pretty(sympy.sympify(text)))
            self.browser.clear()
            self.browser.append(sympy.printing.pretty(sympy.sympify(text)))
        except Exception:
            if text=='': self.browser.clear()

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
+3
source share
1 answer

- QWebiew, - MathJax

, SVGMath MathML SVG, QSvgWidget

0

Source: https://habr.com/ru/post/1796490/


All Articles