I have the following program that uses sympy and svgmath to display a custom algebraic expression. It almost works, but there are a few problems:
- svg is not actually created until the program exits, so obviously it cannot be diplayed.
- Is there a way to improve performance (without looking at "svgmath.xml" every time, etc.)?
- Should the actual svg file be created? Can svgmath pass output directly to QSvgWidget?
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 * from PySide.QtSvg import * from xml import sax from xml.sax.saxutils import XMLGenerator from svgmath.tools.saxtools import XMLGenerator, ContentFilter from svgmath.mathhandler import MathHandler, MathNS from sympy.printing.mathml import mathml from sympy.abc import x from sympy.utilities.mathml import c2p import libxml2 import StringIO 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() self.svgwidget = QSvgWidget() layout = QVBoxLayout() layout.addWidget(self.browser) layout.addWidget(self.lineedit) layout.addWidget(self.svgwidget) 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: prettytext = sympy.printing.pretty(sympy.sympify(text)) self.browser.clear() self.browser.append(prettytext)
source share