1 parent
04c76ac0f0
commit
c8cab28daf
1 file changed
+37
-5
@@ -4,14 +4,41 @@ import json
|
||||
from typing import Any
|
||||
|
||||
from bec_lib.logger import bec_logger
|
||||
from bec_lib.messages import RawMessage
|
||||
from bec_widgets.utils.bec_connector import ConnectionConfig
|
||||
from bec_widgets.utils.bec_widget import BECWidget
|
||||
from PySide6.QtWidgets import QVBoxLayout
|
||||
from PySide6.QtWidgets import QScrollArea, QVBoxLayout
|
||||
from qtpy.QtWidgets import QLabel, QWidget
|
||||
|
||||
logger = bec_logger.logger
|
||||
|
||||
|
||||
def dicts_to_html_table(data: list[dict]) -> str:
|
||||
if not data:
|
||||
return "<p>No data available</p>"
|
||||
|
||||
# 1. Extract unique keys for the header
|
||||
headers = list(set(k for item in data for k in item.keys()))
|
||||
|
||||
# 2. Build the HTML Header Row
|
||||
html = "<table border='1'>\n <thead>\n <tr>\n"
|
||||
for header in headers:
|
||||
html += f" <th>{header}</th>\n"
|
||||
html += " </tr>\n </thead>\n <tbody>\n"
|
||||
|
||||
# 3. Build the Data Rows
|
||||
for row in data:
|
||||
html += " <tr>\n"
|
||||
for header in headers:
|
||||
# .get() handles missing keys safely
|
||||
val = row.get(header, "")
|
||||
html += f" <td>{val}</td>\n"
|
||||
html += " </tr>\n"
|
||||
|
||||
html += " </tbody>\n</table>"
|
||||
return html
|
||||
|
||||
|
||||
class SampleSpreadsheetDisplay(BECWidget, QWidget):
|
||||
"""
|
||||
Display information from the sample spreadsheet
|
||||
@@ -32,11 +59,16 @@ class SampleSpreadsheetDisplay(BECWidget, QWidget):
|
||||
self._data: dict[str, Any]
|
||||
self._layout = QVBoxLayout(self)
|
||||
self._layout.addWidget(QLabel("<b> Sample spreadhseet data: </b>"))
|
||||
dict_data = [{}]
|
||||
try:
|
||||
data: str = self.client.connector._managed_connection.get("x10sa:sample_spreadsheet")
|
||||
if data is not None:
|
||||
dict_data = json.loads(data)
|
||||
data = self.client.connector._managed_connection.get("x10sa:sample_spreadsheet")
|
||||
if isinstance(data, RawMessage):
|
||||
dict_data: list[dict[str, Any]] = data.data["s"]
|
||||
else:
|
||||
dict_data = [{"text": str(data)}]
|
||||
except Exception as e:
|
||||
self._layout.addWidget(QLabel(f"Failed to load spreadsheet data: {e}"))
|
||||
return
|
||||
self._layout.addWidget(QLabel(json.dumps(dict_data, indent=2)))
|
||||
self._layout_area = QScrollArea()
|
||||
self._layout.addWidget(self._layout_area)
|
||||
self._layout_area.setWidget(QLabel(dicts_to_html_table(dict_data)))
|
||||
Reference in new issue
Block a user