diff --git a/mathbot/bot.py b/mathbot/bot.py index ac7e1e5..14b438e 100644 --- a/mathbot/bot.py +++ b/mathbot/bot.py @@ -2,12 +2,16 @@ from telegram.ext import RegexHandler from .rendering import get_png from .static import static_content +import logging + +logger = logging.getLogger(__name__) + def handle_exception_in_get_png(bot, update, exception, text): update.message.reply_text("**Error** while processing '__{}__'.\n\nException was: **{}**.".format(text, exception), parse_mode = "Markdown") def render_math(bot, update): text = update.message.text[len("/rmath"):] - tex = "${}$".format(text) + tex = text try: png = get_png(tex) @@ -27,7 +31,7 @@ def render_math_caption(bot, update): caption = text[text.index("<") + 1:text.index(">")] text = text[text.index(">") + 1:] - tex = "${}$".format(text) + tex = text try: png = get_png(tex) diff --git a/mathbot/rendering.py b/mathbot/rendering.py index a41a0f1..1cc34b4 100644 --- a/mathbot/rendering.py +++ b/mathbot/rendering.py @@ -1,27 +1,19 @@ -from sympy import preview from io import BytesIO -import cairosvg, time, os +import matplotlib.pyplot as plt def get_png(tex): buf = BytesIO() - preview(tex, output = "svg", viewer = "BytesIO", outputbuffer = buf) - buf.seek(0) - fname = "/tmp/{}".format(time.time()) + plt.text(0, 0.6, r"$%s$" % tex, fontsize = 50) + fig = plt.gca() + fig.axes.get_xaxis().set_visible(False) + fig.axes.get_yaxis().set_visible(False) - cairosvg.svg2png(bytestring=buf.read(), parent_widht=500, parent_height=500, write_to=fname) + plt.savefig(buf) buf.seek(0) - with open(fname, "rb") as fin: - buf.write(fin.read()) - with open(fname, "rb") as fin: - with open("test.png", "wb") as fout: - fout.write(fin.read()) - - os.unlink(fname) - - buf.seek(0) return buf +