for a school project, we were supposed to make a simple calculator. The project's goal was to teach github, teamwork, testing, documentation, and all that jazz. It is not really about the calculator. However, my team and I decided to take it a step further and we wanted to create a calculator that displays mathematical expressions in real time.
I managed to find a way: using PyQt5 and its QWebEngineView, I was able to display a simple webpage, and, using javascript, I was able to display an equation in real time using MathJax. However, there are some problems:
Upon entering any number, first, a statusbar appears (for a split second) on the lower part of the display like this:
Bu that is not all - before the final number appears, first, a raw latex code is shown for a fraction of a second, then a HTMLPreview version of the fraction, and only after all this does the final render appear.
This is the python x javascript part:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QFont
import sys
import time
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Ui_s_cals(object):
def __init__(self):
self.content=["10","+","10"]
def setupUi(self, s_cals):
s_cals.setObjectName("s_cals")
s_cals.resize(300, 500)
s_cals.setWindowTitle("Calculator")
self.display = QWebEngineView(s_cals)
self.display.setGeometry(QtCore.QRect(20, 10, 300, 60))
self.pushButton_n0 = QtWidgets.QPushButton(s_cals)
self.pushButton_n0.setGeometry(QtCore.QRect(80, 400, 61, 61))
self.pushButton_n0.setObjectName("pushButton_n0")
self.pushButton_n0.setText("0")
self.pushButton_n0.clicked.connect(lambda:self.print("0"))
def print(self,number):
self.content.append(number)
print(self.content)
#self.output1.setText(''.join(self.content))
temp=''.join(self.content)
self.expression=temp
self.pageSource = '''
<html><head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script></script><script>MathJax.Hub.Config({{
jax: ["input/TeX","output/HTML-CSS"],
displayAlign: "left"
}});</script>
</head>
<body>
<p><mathjax style="font-size:1em">$${expression}$$</mathjax></p>
</body></html>
'''.format(expression=self.expression)
self.display.setHtml(self.pageSource)
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_s_cals()
self.ui.setupUi(self)
self.show()
def main():
calc = QApplication(sys.argv)
instance = Main()
sys.exit(calc.exec_())
if __name__ == '__main__':
start = time.time()
main()
print(time.time() - start)
I was looking for answers for hours. I tried setting the visibility of body to hidden, and then making it visible by a script after mathjax is done rendering, but it didn't work.
I tried all the answers on stack overflow, but none of them counted on me using this inside a python script, which might be the problem. if you know a different way of showing equations in real time.
