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
Related
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have created a GUI in Designer and used 'loadui' to add functionality. So basically, I have a .ui file for design and a .py file which contains the functions. Code in brief is below:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QFileDialog,QMainWindow,QMessageBox
from PyQt5.uic import loadUi
import pymssql
from sqlalchemy import create_engine
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
loadUi("MCQ1.ui",self)
self.BrowDB.clicked.connect(self.load_dbase)
self.SelectDB.clicked.connect(self.select_dbase)
def load_dbase(self):
engine = create_engine('mssql+pymssql://(local)')
query = "select name FROM sys.databases;"
data = pd.read_sql(query,engine)
def select_dbase(self):
global db_value
db_value = self.select_db.currentText()
self.ChosenDB.setText("Chosen DB: "+db_value)
return db_value
app = QApplication(sys.argv)
MainWindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(MainWindow)
widget.setFixedSize(900, 800)
widget.show()
sys.exit(app.exec_())
How do I create an installable (.exe) file with this?
Have a look at fman build system.
With this system, it is possible to create an executable file.
A few additional steps are required:
install fbs
create new fbs project via command line:
fbs startproject
modify the freshly created src/main/python/main.py by implementing your code:
from fbs_runtime.application_context.PyQt5 import ApplicationContext
# start of your code
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QFileDialog,QMainWindow,QMessageBox
from PyQt5.uic import loadUi
import pymssql
from sqlalchemy import create_engine
class MainWindow(QMainWindow):
# [...]
# end of your code
if __name__ == '__main__':
appctxt = ApplicationContext()
# minor modification to the template to start your window
window = MainWindow()
exit_code = appctxt.app.exec()
sys.exit(exit_code)
test if everything is set up correctly via command line:
fbs run
create exectutable via command line:
fbs freeze
Now you should have a new folder "target", which contains an executable of your program.
If you run into any trouble, check out the tutorial and the manual of fbs, which should cover most issues. Additionally, it further illustrates how to create an installer package from the executable file.
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_())
This seems simple. I have a Jupyter Notebook with the following code allowing for a file dialog so the user can select a file to analyze. There are a couple of different files used and it is important the user selects the proper file in the proper order. I am trying to add information to the Window title so the user knows which file the script is looking for.
Here is the Notebook code:
import PCB_utility
f_name = PCB_utility.get_file('Select Bitwise data file')
df_bitwise = pd.read_excel(f_name, sheetname = 'lvtemporary_653630', header=(0))
f_name = PCB_utility.get_file('Select ARINC 429 data file')
df_ARINC = pd.read_csv(f_name, sep = '\t', header=(0))
The file dialog works. The user selects the file, and the program just does what it needs to. I am trying to make it a little more user friendly.
PCB_utility looks like this:
import sys
from PyQt4.QtGui import *
#from PyQt4.QtCore import *
def get_file(Instructions):
filename = QFileDialog.getOpenFileName(None, Instructions, '/')
return(filename)
The problem is the Instructions are NOT making through to the dialog box. I manually entered this function into the IPython section and it works beautifully. It doesn't add the extra text when I call it from the notebook. It just has the standard 'Open File'.
Here is the code that finally worked:
import sys
from PyQt4.QtGui import *
def get_file(Instructions):
# Create an PyQT4 application object.
app = QApplication(sys.argv)
# The QWidget widget is the base class of all user interface objects in PyQt4.
wid = QWidget()
# Get filename using QFileDialog
filename = QFileDialog.getOpenFileName(wid, Instructions, '/')
return(filename)
sys.exit(app.exec_())
I needed to add the PyQt4 application and widget, then close it. I have to admit I don't quite understand why, but it works.
OP already found solution, but for others that looking for the same thing - you can use IPython magic command for enabling IPython GUI event loop integration, to make working external GUI called from IPython (i.e. Jupyter notebook):
# Magic (we must tell IPython that we will be running an external Qt GUI)
%gui qt5
from PyQt5.QtWidgets import QFileDialog
def select_file(directory='./'):
fname = QFileDialog.getOpenFileName(None,
'Select file...',
directory,
filter='All files (*)')
return fname[0]
As original question is for PyQt4, above command %gui qt5 should be replaced with %gui qt4 or only %gui qt.
To do the same thing without magic commands (similar to OP's solution):
import sys
from PyQt5.QtWidgets import QApplication, QFileDialog
def select_file(directory='./'):
app = QApplication(sys.argv)
fname = QFileDialog.getOpenFileName(None,
'Select file...',
directory,
filter='All files (*)')
return fname[0]
A Canopy user here learning about PySide. When I run the demo code below, QApplication complains the event loop is already running.'
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# Create a Qt application
#app = QApplication(sys.argv) #QApplication complains an instance already exists
app = QApplication.instance() #So we just ask for the instance.
#app.aboutToQuit.connect(app.deleteLater)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
So how can I get this simple code to run?
Yes, Pylab is a mode of IPython which starts an event loop for the IPython front end so that you can interact at the IPython command line with your GUI.
Here's an simple example of code which will run with or without Pylab.
import sys
from PySide import QtGui
app = QtGui.QApplication.instance()
standalone = app is None
if standalone:
app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250,150)
wid.setWindowTitle('Simple')
wid.show()
if standalone:
sys.exit(app.exec_())
else:
print "We're back with the Qt window still active"