I created a dashboard with Streamlit as a standalone application with the PySide6 library, however, I need to download a file generated in Streamlit through a button (st.download_button), but when it is clicked, nothing happens. Could you help on how to download Streamlit files with PySide6?
Here's my code:
app.py (PySide6 connector)
import atexit
import subprocess as sp
import os
from PySide6 import QtCore, QtWebEngineWidgets, QtWidgets
from PySide6.QtGui import QIcon
def kill_server(s):
if os.name == 'nt':
sp.call(['taskkill', '/F', '/T', '/PID', str(s.pid)])
elif os.name == 'posix':
s.taskkill()
else:
pass
if __name__ == '__main__':
cmd = f'streamlit run rpbr.py --server.headless=True'
p = sp.Popen(cmd.split(), stdout=sp.DEVNULL)
atexit.register(kill_server, p)
hostname = 'localhost'
port = 8501
app = QtWidgets.QApplication()
my_icon = QIcon()
my_icon.addFile('app.ico')
app.setWindowIcon(my_icon)
view = QtWebEngineWidgets.QWebEngineView()
view.setWindowTitle("Plataform")
view.load(QtCore.QUrl(f'http://{hostname}:{port}'))
view.show()
app.exec()
rpbr.py (Streamlit - code area with download button)
run = st.download_button(label="Download", data=stats_rpbr(typ, pa, fab, brand, apr, start, end),
file_name='Statistics.xlsx')
I tried to find solutions by flagging the file download directly in PySide6, but nothing.
Related
I want to insert streamlit dashboard inside the pyqt5 application local host URL within the python code.
The problem is, every time I have to run the code and see the dashboard, I need to run the streamlit run app.py in command prompt. After running in the command prompt the dashboard is showing in the pyqt application.
Are there any alternate ways to stop using the command prompt.
Below is my code:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import os
import subprocess
import sys
def dashboardUi(self):
self.mainLayout = QVBoxLayout()
dashboardcmd = "py -m streamlit run Dashboard2.py"
jk = os.system(dashboardcmd)
self.browserView = QWebEngineView()
self.browserView.setUrl(QUrl("http://localhost:8501"))
self.browserView.setUrl(QUrl("http://192.168.68.108:8501"))
self.mainLayout.addWidget(self.browserView)
self.setCentralWidget(self.browserView)
main = QWidget()
main.setLayout(self.mainLayout)
return main
I'm learning how to use pyqt, I'm using Windows 10 and python 3.10 and i'm testing a code to open pdf files, this allows you to select the file and then opens it on a new window. It works almost correctly except that doesn't show zoom buttons, or zoom percentage.
This is the code:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
def main():
print(
f"PyQt5 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
)
app = QtWidgets.QApplication(sys.argv)
filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
if not filename:
print("please select the .pdf file")
sys.exit(0)
view = QtWebEngineWidgets.QWebEngineView()
settings = view.settings()
settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
url = QtCore.QUrl.fromLocalFile(filename)
view.load(url)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I was wondering if I need to pass an extra setting to display zoom buttons on pdf viewer. Anyone can help me? I highlighted where buttons/actions supposed to display in the image below.
That problem is because Qt version, if you test with the most recent version, PyQt6 you will see that works.
You need to install PyQt6:
pip install pyqt6
You also need to install webengine for pyqt6:
pip install PyQt6-WebEngine
And you need to change your code to this:
import sys
from PyQt6 import QtCore, QtWidgets, QtWebEngineWidgets
def main():
print(
f"PyQt6 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
)
app = QtWidgets.QApplication(sys.argv)
filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
if not filename:
print("please select the .pdf file")
sys.exit(0)
view = QtWebEngineWidgets.QWebEngineView()
settings = view.settings()
settings.setAttribute(view.settings().WebAttribute.PluginsEnabled, True)
url = QtCore.QUrl.fromLocalFile(filename)
view.load(url)
view.resize(640, 480)
view.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
And now should look like this:
How do I record an unlimited length audio track and save it to a PySide/PyQt file?
I need to record an unlimited length audio track in PySide. In future, recording will stop by pressing the button. I tried running the code from the official documentation QMediaRecorder.
from PySide6.QtCore import QUrl, QDir
from PySide6.QtMultimedia import QMediaCaptureSession, QAudioInput, QMediaRecorder, QMediaFormat
session = QMediaCaptureSession()
audioInput = QAudioInput()
session.setAudioInput(audioInput)
recorder = QMediaRecorder()
session.setRecorder(recorder)
recorder.setMediaFormat(QMediaFormat.MP3)
recorder.setQuality(QMediaRecorder.HighQuality)
file_location = QUrl(QDir.currentPath() + '/test.mp3')
recorder.setOutputLocation(file_location)
recorder.record()
The program runs for about 2-3 seconds, after which it terminates with code 0. The file does not appear in the current directory. I was trying to run this on Windows.
You have to run your script in a Qt eventloop:
import os
from pathlib import Path
from PySide6.QtCore import QDir, QUrl
from PySide6.QtMultimedia import (
QMediaCaptureSession,
QAudioInput,
QMediaRecorder,
QMediaFormat,
)
from PySide6.QtWidgets import QApplication, QPushButton
def main():
app = QApplication([])
session = QMediaCaptureSession()
audioInput = QAudioInput()
session.setAudioInput(audioInput)
recorder = QMediaRecorder()
session.setRecorder(recorder)
recorder.setMediaFormat(QMediaFormat.MP3)
recorder.setQuality(QMediaRecorder.HighQuality)
filename = Path(QDir.currentPath()) / "test.mp3"
url = QUrl.fromLocalFile(os.fspath(filename))
recorder.setOutputLocation(url)
recorder.record()
button = QPushButton("Stop")
button.show()
button.clicked.connect(recorder.stop)
app.exec()
if __name__ == "__main__":
main()
I was learning Qt6, and I wrote a demo putting a local html file into it to test the QWebEngineView Widget. However, the web page shows the info:
Your file counldn't be accessed
It may have been moved, edited, or deleted.
ERR_FILE_NOT_FOUND
Here is my test.py source code:
import sys
from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout)
from PySide6 import QtCore
from PySide6.QtWebEngineWidgets import QWebEngineView
class webView(QWidget):
def __init__(self):
super(webView, self).__init__()
self.layout = QVBoxLayout(self)
self.webV = QWebEngineView()
self.fileDir = QtCore.QFileInfo("./docs.html").absoluteFilePath()
print(self.fileDir)
self.webV.load(QtCore.QUrl("file:///" + self.fileDir))
self.layout.addWidget(self.webV)
if __name__ == "__main__":
app = QApplication([])
web = webView()
web.show()
sys.exit(app.exec())
In Addition, the docs.html has been put into the same directory as the test.py file. And when I print the web.fileDir, the result is correct.
You are hardcoded the url and the path may be wrong, in these cases the url is better step by step. I assume that the html is next to the .py then the solution is:
import os
from pathlib import Path
import sys
from PySide6.QtCore import QUrl
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
from PySide6.QtWebEngineWidgets import QWebEngineView
CURRENT_DIRECTORY = Path(__file__).resolve().parent
class webView(QWidget):
def __init__(self):
super(webView, self).__init__()
filename = os.fspath(CURRENT_DIRECTORY / "docs.html")
url = QUrl.fromLocalFile(filename)
self.webV = QWebEngineView()
self.webV.load(url)
layout = QVBoxLayout(self)
layout.addWidget(self.webV)
if __name__ == "__main__":
app = QApplication([])
web = webView()
web.show()
sys.exit(app.exec())
In Qt in general is strongly preferred to use the qrc files, and the Qt resource management system. Here: Can QWebView load images from Qt resource files? is a small yet, neat example of something that is similar to your problem. You may also view the official PySide6 resource usage :
https://doc.qt.io/qtforpython/tutorials/basictutorial/qrcfiles.html
I wrote below code
import sys,time
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
sys.path.append(r"C:\Users\hpaalm\Desktop")
a=QPushButton()
a.setIcon(QIcon('1.png'))
a.show()
app.exec_()
when i run it in IDE, it show my icon, but when run it in CMD it not show icon. what is problem?
python C:\Users\hpaalm\Desktop\a.py
sys.path contains a list of paths where python imports the modules, this does not serve to import files, icons or similar resources. Instead it is best to create a function that binds the directory path with the filename and return the full path of the icon:
import os
import sys
from PyQt5 import QtGui, QtWidgets
ICON_DIR = r"C:\Users\hpaalm\Desktop"
def get_path_icon(filename):
return os.path.join(ICON_DIR, filename)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
a = QtWidgets.QPushButton()
a.setIcon(QtGui.QIcon(get_path_icon('1.png')))
a.show()
sys.exit(app.exec_())