Skip to content
Snippets Groups Projects
main.py 3.91 KiB
Newer Older
Vanessa Stehr's avatar
Vanessa Stehr committed
import base64
import io
import json
import tarfile
import warnings
import bokehCssPTB
Vanessa Stehr's avatar
Vanessa Stehr committed
from dsiParser import dsiParser
from bokeh.plotting import curdoc,figure
Vanessa Stehr's avatar
Vanessa Stehr committed
from bokeh.layouts import column, row
Vanessa Stehr's avatar
Vanessa Stehr committed
from bokeh.models import FileInput, Div, CustomJS, Button, TabPanel, Tabs, Dropdown, TextInput, Button, MathText, Label
Vanessa Stehr's avatar
Vanessa Stehr committed
from bokeh.events import ValueSubmit

VERSION = "0.1.0"
class dsiparserInput():
Vanessa Stehr's avatar
Vanessa Stehr committed

    def __init__(self):
        self.dsiInput = TextInput(value="", title="DSI unit string:", width=500)
Vanessa Stehr's avatar
Vanessa Stehr committed
        self.dsiInput.on_event(ValueSubmit, self.parseInput)
        self.dsiSubmitButton = Button(label="Convert", button_type="primary")
        self.dsiSubmitButton.on_click(self.parseInput)
        self.inputRow = row(children = [self.dsiInput, self.dsiSubmitButton], css_classes = ["textInputRow"])
Vanessa Stehr's avatar
Vanessa Stehr committed
        self.results = column(children = [])
        self.widget = column(children=[self.inputRow, self.results], css_classes=["textlikeColumn"])
Vanessa Stehr's avatar
Vanessa Stehr committed
    def parseInput(self):
Vanessa Stehr's avatar
Vanessa Stehr committed
        self.results.children = []
        input = self.dsiInput.value
        p = dsiParser()
        resultTree = p.parse(input)
Vanessa Stehr's avatar
Vanessa Stehr committed
        parsingMessages = []
        if resultTree.valid:
            parsingMessages.append(
                Div(
                    text = "DSI string parsed without warnings",
                    css_classes = ["msg-positive"],
                )
            )
        else:
            for message in resultTree.warnings:
                parsingMessages.append(
                    Div(
                        text = message,
                        css_classes = ["msg-negative"]
                    )
                )
        self.resultErrors = column(children = parsingMessages)

        # latexOutput = TextInput(value=resultTree.toLatex(), title="DSI unit string:")
        # latexOutput.js_on_change()
        latexOutput = row(children=[
            Div(text = "$$\mathrm{\LaTeX{}}$$ code:"),
            Div(text = "<pre><code>"+resultTree.toLatex()+"</code></pre>", disable_math=True)
        ])

        p = figure(width=500, height=100, x_range=(0, 1), y_range=(0, 1), tools="save")
        p.xaxis.visible = False
        p.yaxis.visible = False
        p.grid.visible = False

        p.add_layout(Label(
            x=0.5, y=0.5, text=resultTree.toLatex(), text_font_size="12px", text_baseline="middle", text_align="center",
        ))

        imageOutput = row(children = [
            Div(text = "$$\mathrm{\LaTeX{}}$$ output:"),
            p
        ], css_classes = ["latexImageRow"])
Vanessa Stehr's avatar
Vanessa Stehr committed
        self.results.children = [self.resultErrors, latexOutput, imageOutput]
        self.dsiTree = resultTree
Vanessa Stehr's avatar
Vanessa Stehr committed

class page():
    def __init__(self):
        curdoc().template_variables["VERSION"] = VERSION
        curdoc().title = "DSI to Latex"
        curdoc().add_root(bokehCssPTB.getStyleDiv())
        curdoc().theme = bokehCssPTB.getTheme()
        self.dsiInput1 = dsiparserInput()
        self.dsiInput2 = dsiparserInput()
        self.inputs=row([self.dsiInput1.widget, self.dsiInput2.widget])

        curdoc().add_root(self.inputs)
        self.comapreButton = Button(label="Compare", button_type="primary")
        self.comapreButton.on_click(self.compare)
        self.compaReresult = Div(text = "", css_classes = ["msg-positive"])
        self.compareRow = row(children = [self.comapreButton,self.compaReresult], css_classes = ["textInputRow"])
        curdoc().add_root(self.compareRow)

    def compare(self):
        self.dsiInput1.parseInput()
        self.dsiInput2.parseInput()
        scalfactor,baseUnit=self.dsiInput1.dsiTree.isScalablyEqualTo(self.dsiInput2.dsiTree)
        if scalfactor:
            self.compaReresult.text = "The two units are equal up to a scaling factor of "+str(scalfactor)+" and a base unit of "+str(baseUnit)
            self.compaReresult.css_classes=["msg-positive"]
        else:
            self.compaReresult.text = "The two units are not equal"
            self.compaReresult.css_classes = ["msg-negative"]
Vanessa Stehr's avatar
Vanessa Stehr committed
thisPage = page()