Skip to content

Instantly share code, notes, and snippets.

@rodnaxel
Created July 28, 2021 19:55
Show Gist options
  • Save rodnaxel/7abfd578b546de60e60ba6427c5f3b79 to your computer and use it in GitHub Desktop.
Save rodnaxel/7abfd578b546de60e60ba6427c5f3b79 to your computer and use it in GitHub Desktop.
Simple example plotly application with QWebEngine
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QDir, QUrl
import plotly
import plotly.graph_objects as go
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle(f'Plotly Demo')
self.setMinimumSize(800, 600)
self.setupUi()
self.data = data = list(range(100))
self.buttons['update'].clicked.connect(self.update_)
#self.updatePlotly(data)
def update_(self):
self.updatePlotly(self.data)
def updatePlotly(self, data):
fig = go.Figure(data=go.Scatter(y=data))
fig.write_html('first_figure.html', auto_open=False)
path = QDir.current().filePath('first_figure.html')
url = QUrl.fromLocalFile(path)
self.view.load(url)
self.status.showMessage(path)
def setupUi(self):
self.view = view = QWebEngineView()
self.setCentralWidget(view)
self.buttons = {}
button = QPushButton("Update", self)
self.buttons['update'] = button
self.status = self.statusBar()
if __name__ == "__main__":
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment