How to embed Tensorboard to Gui app in PyQt5 - python

I tried to embed TensorBoard to my GUI app with PyQt5, but I got always the same issue: the app won't display the SVG graphs it; shows only the HTML.
I want to know if there is a module or function in PyQt5 that can show SVG images in webpage using their URL(s).
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://tensorboard.dev/experiment/EDZb7XgKSBKo6Gznh3i8hg/#scalars"))
web.show()
sys.exit(app.exec_())

Related

The kernel 'base (Python 3.9.7)' died when using PyQt5

I am using Jupyter Notebook on Vscode and I'm new to PyQt5. When I run these lines of code, I got this Error message
I tried restarting the kernel and it only works once each time it but doesn't display the widget fully either. Here's a screenshot of the output
PS, I am using Anaconda 3 and Python 3 on Windows 10 16 GB RAM 64 bit
And here's my code:
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QWidget, QApplication, QLineEdit, QTableWidget, QStackedWidget
from PyQt5.QtCore import *
class welcomescreen(QDialog):
def __init__(self):
super(welcomescreen,self).__init__()
loadUi("welcome.ui", self)
# main
app = QApplication(sys.argv)
if app is None:
app = QtWidgets.QApplication(sys.argv)
acceuil=welcomescreen() #object from the class above
widget=QStackedWidget()
widget.addWidget(acceuil)
widget.setFixedHeight(850)
widget.setFixedWidth(1200)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")

How to show a button in a pop up message in Python and how to interact?

I'd like to make a button in a pop up message. I use PyQt5 module and a message in pop up. But when I make a button in the message, my pop up message is in for
import sys
import io
import folium # pip install folium
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView # pip install PyQtWebEngine
import numpy as np
import pandas as pd
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
****some codes***
for c in range (len(flight_df)):
tooltip1 =str(flight_df['x'][c])+"*** "+str(flight_df['y'][c])+"*** "+str(flight_df['name'][c])
folium.Marker([flight_df['x'][c],flight_df['y'][c]], popup=flight_df['x'],tooltip=tooltip1).add_to(m)
data = io.BytesIO()
m.save(data, close_file=False)
webView = QWebEngineView()
webView.setHtml(data.getvalue().decode())
layout.addWidget(webView)
****some other codes****
My code is above. Can you help me how can I add a follow button in pop up screen and how can I see if button work
PyQt comes with a message box dialog built into it for these purposes.
The following code worked for me
import sys
from PyQt5 import Qt
from PyQt5.Qt import QMessageBox
a = Qt.QApplication(sys.argv)
window = Qt.QWidget()
window.resize(320, 240)
window.setWindowTitle("Hello World!")
result = QMessageBox.question(window, 'TITLE', 'MESSAGE', QMessageBox.Yes | QMessageBox.No)
window.show()
sys.exit(a.exec_())

PyQt embed QWebEngineView in Main Window

I'm coming from a tkinter background where everything can be put in a frame.
How can I get my current working codes result (which launches a WebEngine View of a page, google in this instance) to sit inside a main window like shown in the image? Going by the image I want the WebEngine to be housed in the "Green" Box for example.
Working code including all versions used
"""
Python version - 3.7.3
PyQt5 5.15.3
PyQt5-Qt 5.15.2
PyQt5-sip 12.8.1
PyQtWebEngine 5.15.3
PyQtWebEngine-Qt 5.15.2
"""
import sys
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
url = 'https://google.com'
app = QApplication(sys.argv)
# QWebEngineView
browser = QWebEngineView()
browser.load(QUrl(url))
browser.show()
sys.exit(app.exec_())
You have to use a QGridLayout:
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QMainWindow, QTextEdit, QWidget
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
url = "https://google.com"
app = QApplication(sys.argv)
w = QMainWindow()
browser = QWebEngineView()
browser.load(QUrl(url))
central_widget = QWidget()
w.setCentralWidget(central_widget)
lay = QGridLayout(central_widget)
lay.addWidget(browser, 0, 0, 2, 1)
lay.addWidget(QTextEdit(), 0, 1)
lay.addWidget(QTextEdit(), 1, 1)
lay.setColumnStretch(0, 1)
lay.setColumnStretch(1, 1)
lay.setRowStretch(0, 1)
lay.setRowStretch(1, 1)
w.show()
sys.exit(app.exec_())
You could use a vertical layout to hold the two red boxes. And then use a horizontal layout to hold the QWebEngineView and the vertical layout. Zetcode has a good tutorial for layouts. Code heavily borrowed from #eyllanesc:
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow,
QHBoxLayout, QVBoxLayout,
QTextEdit, QWidget)
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
url = "https://google.com"
app = QApplication(sys.argv)
w = QMainWindow()
browser = QWebEngineView()
browser.load(QUrl(url))
central_widget = QWidget()
w.setCentralWidget(central_widget)
vertical = QVBoxLayout()
vertical.addWidget(QTextEdit())
vertical.addWidget(QTextEdit())
horizontal = QHBoxLayout(central_widget)
horizontal.addWidget(browser)
horizontal.addLayout(vertical)
w.show()
sys.exit(app.exec_())

PyQt with Flask dont show the MainWindow

i'm trayin to create a gui that run a flask application using this example https://github.com/onur2677/PyQt-Flask
in this exemple they use pyqt 4
i'm trying to use pyqt 5 so i made some changes
but the mainwindow open and close in the same time
this is my code for the gui
import sys
from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import psycopg2
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import *
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
PORT = 5000
ROOT_URL = 'http://localhost:{}'.format(PORT)
class FlaskThread(QThread):
def __init__(self, application):
QThread.__init__(self)
self.application = application
def __del__(self):
self.wait()
def run(self):
self.application.run(port=PORT)
def createGuiFor(application):
qtapp = QApplication(sys.argv)
webapp = FlaskThread(application)
webapp.start()
qtapp.aboutToQuit.connect(webapp.terminate)
webview = QWebEngineView()
webview.load(QUrl(ROOT_URL))
webview.show()
webview.setWindowTitle("MyApp")
webview.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
return qtapp.exec_()
if __name__ == '__main__':
from MyWebApp import app
app = QApplication(sys.argv)
sys.exit(app.exec_())
sys.exit(createGuiFor(app))
and this is the flask application that will run in the gui
from flask import Flask ,render_template
app = Flask(__name__)
#app.route("/")
def hello():
return render_template("index.html")
if __name__ == "__main__":
app.run()
for any one ho want to use flask and pyqt there is an example
here https://github.com/smoqadam/PyFladesk
that work for me perfectly
EDIT:
Here is the same code with PySide6
https://github.com/eliaweiss/PySide6Fladesk.git

Python 3.6 | PyQt5 | QLabel doesn't display QPixmap

I'm trying to display a PNG image in a Widget using QLabel. I built the Widget using Qt Designer and the label shows the image properly. When I load the .ui into mi .py and execute it, everything shows up well, except for the label. I tried loading the QPixmap manually on the python code, but it didn't work either.
from PyQt5.QtWidgets import (QApplication, QLabel)
from PyQt5.QtGui import (QPixmap)
from PyQt5 import uic
import sys
import os
form = uic.loadUiType('gui/Files/Login.ui')
class LoginWindow(form[0], form[1]):
def __init__(self):
super().__init__()
self.setupUi(self)
self.logoLabel.setPixmap(QPixmap(os.path.abspath(os.path.join(os.path.dirname(__file__), 'logo.png'))))
self.logoLabel.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LoginWindow()
win.show()
app.exec_()
Any idea what am I doing wrong? Any sugestions on how could I display the image other than with QLabel?

Categories

Resources