add ExWindow.py, but cannot use plotly at 2 instances?

This commit is contained in:
Ryan@Home 2024-11-04 22:05:17 -05:00
parent 80be0f059b
commit 0b2e0ee0cd
2 changed files with 169 additions and 0 deletions

114
PyGUIQt6/ExWindow.py Normal file
View File

@ -0,0 +1,114 @@
#!/usr/bin/python3
import os
import time
from PyQt6.QtWidgets import (
QVBoxLayout, QWidget, QCheckBox
)
from PyQt6.QtCore import QUrl
from PyQt6.QtWebEngineWidgets import QWebEngineView
import plotly.graph_objects as go
from IAEANuclearData import IsotopeClass
class ExWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Ex Plot")
self.setGeometry(100, 100, 400, 800)
self.ASym = ""
self.maxEx = 0
self.data = None
self.Iso = IsotopeClass()
self.html_file = None
self.web_view = QWebEngineView()
layout = QVBoxLayout(self)
layout.addWidget(self.web_view)
# self.plot_Ex_graph()
def GetEx(self, ASym :str, maxEx :float):
self.ASym = ASym
self.maxEx = maxEx
self.data = self.Iso.GetExList(ASym, maxEx)
def plot_Ex_graph(self):
if self.html_file and os.path.exists(self.html_file):
os.remove(self.html_file)
xShift = 0
fontSize = 14
plotHeight = 700
plotWidth = 350
yMin = -1
# Create a Plotly figure
fig = go.Figure()
ex=self.data['energy']/1000.
jp=self.data['jp']
fig = go.Figure()
fig.update_layout(plot_bgcolor='white', width=plotWidth, height = plotHeight, margin=dict(l=0, r=0, t=0, b=0))
fig.update_layout(showlegend=False)
fig.update_xaxes(showline=False, visible= False, range=[-1, 3])
fig.update_yaxes(showline=True, visible= True, range=[yMin, self.maxEx+1])
l=ex.last_valid_index()
fontSizeMeV=fontSize/plotHeight*(self.maxEx+1-yMin)
#print(fontSizeMeV)
#adjust text label y-pos
ypos = ex.copy()
noOverlap = False
loop = 0
while noOverlap == False and loop < 2*l :
#print("================= %d" % loop)
for i in range(1, l+1) :
diff = ypos[i] - ypos[i-1]
#print("%2d | %.3f, %.3f | %.4f" % (i, ypos[i], ypos[i-1], diff))
if diff < fontSizeMeV :
ypos[i-1] += (diff - fontSizeMeV)/2
ypos[i] += (fontSizeMeV - diff)/2
if( ypos[i-1] < yMin + fontSizeMeV/2) :
ypos[i-1] = yMin + fontSizeMeV/2
ypos[i] = ypos[i-1] + fontSizeMeV
#print(" | %.3f, %.3f" % (ypos[i], ypos[i-1]))
#print(ypos)
###=======inspection
count = 0
for i in range(1, l+1) :
diff = ypos[i] - ypos[i-1]
if diff > fontSizeMeV :
count = count +1
if count == l :
noOverlap = True
loop += 1
for i in range(0,l+1):
fig.add_trace(go.Scatter(x=[xShift,1 + xShift], y=[ex[i],ex[i]],mode='lines',line=dict(color='black', width=1)))
fig.add_trace(go.Scatter(x=[1.03 + xShift,1.1 + xShift, 1.19 + xShift], y=[ex[i],ypos[i],ypos[i]],mode='lines',line=dict(color='gray', width=1)))
fig.add_annotation(x=1.2 + xShift, y=ypos[i], text=("%.3f, %s" % (ex[i], jp[i])), xanchor='left', font=dict(size=fontSize), showarrow=False)
fig.add_annotation(x=0.5 + xShift, y=-0.5, text=self.ASym, font=dict(size=1.5*fontSize), showarrow=False)
# Save the plot as an HTML file in a temporary location
timestamp = int(time.time() * 1000) # Unique timestamp in milliseconds
html_file = f"/tmp/Exwindow_{timestamp}.html"
fig.write_html(html_file)
self.html_file = html_file # Store for cleanup
self.web_view.setUrl(QUrl.fromLocalFile(html_file))
def __del__(self):
if os.path.exists(self.html_file):
os.remove(self.html_file)

View File

@ -14,6 +14,7 @@ from PyQt6.QtGui import QFont
from ExtractXsecPy import extract_xsec
from PlotWindow import PlotWindow
from ExWindow import ExWindow
################################################## MainWindow
class MyWindow(QMainWindow):
@ -27,6 +28,7 @@ class MyWindow(QMainWindow):
self.DWBAFileName = "DWBA"
self.bashResult = ""
self.plot_window = None
self.Ex_window = None
# Set up Group Box for DWBA Control
self.gbDWBA = QGroupBox("DWBA")
@ -101,6 +103,37 @@ class MyWindow(QMainWindow):
group_layout.addWidget(self.bnCalDWBA, 12, 0, 1, 2)
# Ex Group
# self.gbEx = QGroupBox("Ex")
# Ex_layout = QGridLayout()
# Ex_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
# self.gbEx.setLayout(Ex_layout)
# lbName = QLabel("Isotop :")
# lbName.setAlignment(Qt.AlignmentFlag.AlignRight)
# self.leName = QLineEdit()
# self.leName.setText("12C")
# lbMaxEx = QLabel("Max Ex [MeV]:")
# lbMaxEx.setAlignment(Qt.AlignmentFlag.AlignRight)
# self.sbMaXEx = QDoubleSpinBox()
# self.sbMaXEx.setMinimum(0)
# self.sbMaXEx.setMaximum(20)
# self.sbMaXEx.setDecimals(1)
# self.sbMaXEx.setValue(10)
# buEx = QPushButton("Get & Plot Ex")
# buEx.setFixedHeight(40)
# buEx.clicked.connect(self.open_Ex_window)
# Ex_layout.addWidget(lbName, 0, 0)
# Ex_layout.addWidget(self.leName, 0, 1)
# Ex_layout.addWidget(lbMaxEx, 1, 0)
# Ex_layout.addWidget(self.sbMaXEx, 1, 1)
# Ex_layout.addWidget(buEx, 2, 0, 1, 2)
# Set up the Right Side
self.bnOpenDWBASource = QPushButton("Open DWBA Source")
@ -126,6 +159,8 @@ class MyWindow(QMainWindow):
# Set up the layout
layout = QGridLayout()
layout.addWidget(self.gbDWBA, 0, 0, 7, 1)
# layout.addWidget(self.gbDWBA, 0, 0, 5, 1)
# layout.addWidget(self.gbEx, 5, 0, 2, 1)
layout.addWidget(self.bnOpenDWBASource, 0, 1)
layout.addWidget(self.leFileName, 0, 2, 1, 3)
@ -133,6 +168,9 @@ class MyWindow(QMainWindow):
layout.addWidget(self.text_edit, 1, 1, 5, 5)
layout.addWidget(self.leStatus, 6, 1, 1, 5)
layout.setColumnStretch(0, 1)
layout.setColumnStretch(1, 3)
# Set up the container and layout
container = QWidget()
container.setLayout(layout)
@ -228,10 +266,27 @@ class MyWindow(QMainWindow):
self.plot_window.plot_plotly_graph()
self.plot_window.show()
# def open_Ex_window(self):
# if self.plot_window:
# self.plot_window.close()
# self.plot_window.__del__()
# if self.Ex_window is None :
# self.Ex_window = ExWindow()
# self.Ex_window.GetEx(self.leName.text(), self.sbMaXEx.value())
# self.Ex_window.plot_Ex_graph()
# self.Ex_window.show()
def closeEvent(self, event):
if self.plot_window:
self.plot_window.close() # Close the PlotWindow when MainWindow closes
self.plot_window.__del__()
if self.Ex_window:
self.Ex_window.close() # Close the PlotWindow when MainWindow closes
self.Ex_window.__del__()
event.accept() # Accept the event to proceed with closing the main window