Skip to content
Snippets Groups Projects
Commit a8a86263 authored by Vanessa Stehr's avatar Vanessa Stehr
Browse files

MultiVector creation is working now

parent 60e32af4
Branches
No related tags found
No related merge requests found
bokeh~=3.3.0
pytest~=7.4.2
pytest-cov~=4.1.0
\ No newline at end of file
pytest-cov~=4.1.0
-e git+https://gitlab1.ptb.de/digitaldynamicmeasurement/py-dsi-vectors-and-tables.git@release#egg=dsiVectorsAndTables
\ No newline at end of file
import base64
import json
from bokeh.plotting import curdoc, figure
from bokeh.layouts import column
from bokeh.models import FileInput, Div, CustomJS, Button, TabPanel, Tabs
from pyDsiVectorsAndTables.pyDsiVectorsAndTables import dsiVector, dsiMultiVector, getNDTablesFromDCC, getFromDict
from pyDsiVectorsAndTables.dsiMultiVectorPlot import dsiMultiVectorPlot
import numpy as np
VERSION = "0.1.0"
class page:
def __init__(self):
self.fileUploader = fileUploader(self.readFile)
self.fileDownloader = fileDownloader(b"Initial binary content here", filename="example.txt")
self.fileDownloader = fileDownloader(
b"Initial binary content here", filename="example.txt"
)
self.tabs = []
self.dataView = dataView(self.tabs)
......@@ -24,67 +31,157 @@ class page:
# Make this the new download file
self.fileDownloader.update(self.fileData, new)
# Generate some tabs (atm: placeholders, todo: from the data)
self.p1 = figure(width=300, height=300)
self.p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
self.tabs.append(TabPanel(child=self.p1, title="circle"))
self.t2 = Div(text="This is a second placeholder", width=200, height=100)
self.tabs.append(TabPanel(child=self.t2, title="Tab2"))
# Parse the XML
self.dcc = dcc(self.fileData)
# Generate some tabs from the dcc data
self.tabs = self.dcc.dataToTabs()
self.fileUploader.setMessage(
"File processed successfully!", css_classes=["msg-positive"]
)
self.dataView.update_tabs(self.tabs)
class dcc:
def __init__(self, xmlFileData) -> None:
self.xmlFileData = xmlFileData
self.data = self._xmlToJson()
self.multiVectorPlots = {}
def _xmlToJson(self):
"""Takes the content of an xml file, returns the corresponding dict
The xml file data is parsed by a server into json, which is then parsed into a dict and returned.
!! This is currently a dummy that always reads the same file regardless of input parameters!!
Args:
xmlFileData (_type_): contents of the xml file to parse
Returns:
dict: representation of the file contents
"""
# TODO: Replace this placeholder with connection to the server
f = open("../sample-data/sample_dcc_server.json")
return json.load(f)
def dataToTabs(self):
# TODO, this is a placeholder
tabs = []
# vectors = []
# dataNode = self.data[
# "dcc:digitalCalibrationCertificate"][
# "dcc:measurementResults"][
# "dcc:measurementResult"][
# "dcc:results"][
# "dcc:result"][
# "dcc:data"][
# "dcc:list"][
# "dcc:quantity"
# ]
# for item in dataNode:
# vector = dsiVector(np.ndarray())
# vectors.append(item)
#mv = dsiMultiVector.fromDCCXMLJSON(self.data)
self.createdMultiVectors = {}
self.NDTablePathsFromXML = getNDTablesFromDCC(self.data)
for path in self.NDTablePathsFromXML:
self.mvDict = getFromDict(self.data, path[0][:-1])
mv = dsiMultiVector.fromDCCXMLJSON(self.mvDict)
self.createdMultiVectors[mv.name] = mv
self.multiVectorPlots[mv.name] = dsiMultiVectorPlot(mv)
# with open("../../py-dsi-vectors-and-tables/tests/test_multiVectorJSONDumpingAndLoadingFromFile.json",encoding='utf-8') as JSONFile:
# mv=dsiMultiVector.fromJson(JSONFile.read())
for plotName, plot in self.multiVectorPlots.items():
for figure in plot.figures:
tabs.append(TabPanel(child = figure, title = plotName))
# print(self.createdMultiVectors)
# p1 = figure(width=300, height=300)
# p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
# tabs.append(TabPanel(child=p1, title="circle"))
# t2 = Div(text="This is a second placeholder", width=200, height=100)
# tabs.append(TabPanel(child=t2, title="Tab2"))
return tabs
class dataView:
def __init__(self, tabs: list, name: str = "dataView"):
self.name = name
self.tabs = tabs
self.layout = Tabs(tabs=self.tabs,name=self.name)
self.layout = Tabs(tabs=self.tabs, name=self.name)
def update_tabs(self, tabs: list):
self.tabs = tabs
self.layout.tabs = tabs
class fileUploader:
def __init__(self, callback, name: str = "fileUploader"):
self.name=name
self.fileInput = FileInput(name="fileInput", title="Select files:", accept=".xml")
self.fileInputMessage = Div(name="fileInputMessage", text="No file uploaded yet!",css_classes=["msg-neutral"])
jsCallback = CustomJS(args=dict(div=self.fileInputMessage, file_input=self.fileInput), code="""
div.text = "Processing file…"
div.css_classes = ["msg-positive"]
""")
self.fileInput.js_on_change('filename', jsCallback)
self.fileInput.on_change('filename', callback)
self.layout = column(children=[self.fileInput, self.fileInputMessage],name=self.name)
self.name = name
self.fileInput = FileInput(
name="fileInput", title="Select files:", accept=".xml"
)
self.fileInputMessage = Div(
name="fileInputMessage",
text="No file uploaded yet!",
css_classes=["msg-neutral"],
)
jsCallback = CustomJS(
args=dict(div=self.fileInputMessage, file_input=self.fileInput),
code="""
div.text = "Processing file…"
div.css_classes = ["msg-positive"]
""",
)
self.fileInput.js_on_change("filename", jsCallback)
self.fileInput.on_change("filename", callback)
self.layout = column(
children=[self.fileInput, self.fileInputMessage], name=self.name
)
def setMessage(self, text: str, css_classes: list = ["msg-neutral"]):
self.fileInputMessage.text = text
self.fileInputMessage.css_classes = css_classes
class fileDownloader:
def __init__(self, byte_object=None, filename: str = "downloaded_file", name: str = "fileDownloader"):
def __init__(
self,
byte_object=None,
filename: str = "downloaded_file",
name: str = "fileDownloader",
):
self.name = name
self.byte_object = byte_object
self.filename = filename
self.download_button = Button(label="Download", width=150)
self.set_callback()
self.layout = column(self.download_button,name=self.name)
self.layout = column(self.download_button, name=self.name)
def set_callback(self):
self.download_button.on_click(self.prepare_download)
def prepare_download(self):
base64_string = base64.b64encode(self.byte_object).decode('utf-8')
download_js = CustomJS(args=dict(filename=self.filename, file_contents=base64_string),
code="""
var filename = filename;
var file_contents = "data:application/octet-stream;base64," + file_contents;
var link = document.createElement('a');
link.href = file_contents;
link.download = filename;
link.click();
""")
base64_string = base64.b64encode(self.byte_object).decode("utf-8")
download_js = CustomJS(
args=dict(filename=self.filename, file_contents=base64_string),
code="""
var filename = filename;
var file_contents = "data:application/octet-stream;base64," + file_contents;
var link = document.createElement('a');
link.href = file_contents;
link.download = filename;
link.click();
""",
)
self.download_button.js_on_click(download_js)
def update(self, new_byte_object, new_filename: str):
self.byte_object = new_byte_object
self.filename = new_filename
self.set_callback()
thisPage = page()
\ No newline at end of file
thisPage = page()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment