![]() |
[PyQt] PyQt5 window closing when trying to display a graph - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] PyQt5 window closing when trying to display a graph (/thread-40530.html) |
PyQt5 window closing when trying to display a graph - bianca - Aug-12-2023 I am developing a GUI Application using PyQt5. There is a tab with charts and the idea is when clicking a button, it displays a chart. The following code works just fine: from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets import plotly.graph_objects as go import pandas as pd class CandlestickTab(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.button = QtWidgets.QPushButton('Plot', self) self.browser = QtWebEngineWidgets.QWebEngineView(self) vlayout = QtWidgets.QVBoxLayout(self) vlayout.addWidget(self.button, alignment=QtCore.Qt.AlignHCenter) vlayout.addWidget(self.browser) self.button.clicked.connect(self.show_graph) self.resize(1000,800) def show_graph(self): df = px.data.tips() fig = px.box(df, x="day", y="total_bill", color="smoker") fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))I have a csv file with data about crypto such as timestamp, open, high, low, close, volume and I wanna display a CandleStick. I modified the code keeping same structure as the one above and everytime I click the button, my application is closing and I receive this error: Process finished with exit code -1073740791 (0xC0000409) from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets import plotly.graph_objects as go import pandas as pd class CandlestickTab(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.button = QtWidgets.QPushButton('Show Candlestick Chart', self) self.browser = QtWebEngineWidgets.QWebEngineView(self) vlayout = QtWidgets.QVBoxLayout(self) vlayout.addWidget(self.button, alignment=QtCore.Qt.AlignHCenter) vlayout.addWidget(self.browser) self.button.clicked.connect(self.show_candlestick_chart) self.resize(1000, 800) def show_candlestick_chart(self): df = pd.read_csv('crypto.csv') if not df.empty: fig = go.Figure(data=[go.Candlestick(x=df['timestamp'], open=df['open'], high=df['high'], low=df['low'], close=df['close'])]) self.browser.setHtml(fig.to_html(include_plotlyjs='cdn')) else: self.browser.setHtml("<p>No data available for plotting.</p>")This is the main: import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget from home import HomeTab from candlestick import CandlestickTab from analiza_tehnica import AnalizaTehnicaTab from sentiment_analysis import SentimentAnalysisTab from alpha_get_data import AlphaGetData # from news import NewsTab class MainApp(QMainWindow): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setWindowTitle('Aplicația Mea cu Tab-uri') self.setGeometry(100, 100, 800, 600) tabs = QTabWidget() tabs.addTab(HomeTab(),"Home") tabs.addTab(AnalizaTehnicaTab(), "Analiza Tehnica") tabs.addTab(CandlestickTab(),"CandleStick") tabs.addTab(SentimentAnalysisTab(), "Sentiment Analysis") self.setCentralWidget(tabs) if __name__ == '__main__': app = QApplication(sys.argv) window = MainApp() window.show() sys.exit(app.exec_()) app.setQuitOnLastWindowClosed(False)Can someone help me with this, please? RE: PyQt5 window closing when trying to display a graph - deanhystad - Aug-12-2023 Process finished with exit code -1073740791 (0xC0000409) means "Program crashed, here is an error code to help you understand why." Didn't you get any kind of error trace? If so, please post. Divide and conquer. First verify that you can generate html that you can view in a browser. import plotly.graph_objects as go import pandas as pd df = pd.read_csv('crypto.csv') fig = go.Figure(data=[go.Candlestick( x=df['timestamp'], open=df['open'], high=df['high'], low=df['low'], close=df['close'])]) fig.write_html("candlestick.html")If opening "canclestick.html" in chrome or whatever displays the expected plot, the problem is in the Qt world. If not, there is something going on with how you are using plotly. RE: PyQt5 window closing when trying to display a graph - bianca - Aug-12-2023 If I create a new project and run your code, it works normally. It returns 0 and creates a html file in the folder which I can open and visualise. In my project, If I try replacing this line self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))with this fig.write_html("candlestick.html")My app crashed with same error. I tried adding a trace like this : def show_candlestick_chart(self): print("show_candlestick_chart: Entering function") # Print to indicate function start if self.df is not None and not self.df.empty: print("show_candlestick_chart: Generating Candlestick Chart") # Print before generating chart try: fig = go.Figure(data=[go.Candlestick(x=self.df['timestamp'], open=self.df['open'], high=self.df['high'], low=self.df['low'], close=self.df['close'])]) print("show_candlestick_chart: Chart generated successfully") # Print if chart generated try: html_content = fig.to_html(include_plotlyjs='cdn') print("show_candlestick_chart: HTML content generated") # Print if HTML content generated try: self.browser.setHtml(html_content) print("show_candlestick_chart: HTML content set in browser") # Print if content set in browser except Exception as set_html_exception: print("show_candlestick_chart: Error setting HTML content in browser:", set_html_exception) except Exception as html_exception: print("show_candlestick_chart: Error generating HTML content:", html_exception) except Exception as chart_exception: print("show_candlestick_chart: Error generating chart:", chart_exception) else: print("show_candlestick_chart: No data to plot") # Print if no data self.browser.setHtml("<p>No data available for plotting.</p>") print("show_candlestick_chart: Exiting function") # Print to indicate function endBut it's not helpful because my application is crashing as soon as I am clicking on the specific tab and I get no trace. I am not sure if I should move to Tkinter or not because I dont know if there is a issue with my system or not RE: PyQt5 window closing when trying to display a graph - deanhystad - Aug-12-2023 So the crash is as soon as you open the tab? Not when you try to plot? Step 2 of Divide and Conquer. Try running outside the context of the main application. from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets import plotly.graph_objects as go import pandas as pd class CandlestickTab(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.button = QtWidgets.QPushButton('Show Candlestick Chart', self) self.browser = QtWebEngineWidgets.QWebEngineView(self) vlayout = QtWidgets.QVBoxLayout(self) vlayout.addWidget(self.button, alignment=QtCore.Qt.AlignHCenter) vlayout.addWidget(self.browser) self.button.clicked.connect(self.show_candlestick_chart) self.resize(1000, 800) def show_candlestick_chart(self): df = pd.read_csv('crypto.csv') if not df.empty: fig = go.Figure(data=[go.Candlestick(x=df['timestamp'], open=df['open'], high=df['high'], low=df['low'], close=df['close'])]) self.browser.setHtml(fig.to_html(include_plotlyjs='cdn')) else: self.browser.setHtml("<p>No data available for plotting.</p>") app = QtWidgets.QApplication(sys.argv) window = CandlestickTab() window.show() sys.exit(app.exec_()) RE: PyQt5 window closing when trying to display a graph - bianca - Aug-12-2023 I actually solved it. The problem has on the csv import, a column had a different name. Thanks so much for your help! |