QApplication (PySide) Django app issue with quitting - python

I have a Django app in which the users can create reports at various places throughout the webapp. As a little nifty feature I would like to make a "send as PDF" feature for these reports, and I'm almost there.
What I do is that before the report is returned as a HttpResponse through Django I send the raw HTML content through a small PySide/QT snippet (as seen below).
The problem is that I can't get the QApplication to quit.
I have tried with the standard QCoreApplication.exit() but with no luck. If I try to convert a new report right after the first one, the console says a "QApplication instance already exists".
I am using Django 1.2.5, Python 2.7, QT 4.8 and PySide 1.1 on OS X 10.7.3 (for testing).
Code:
def makepdf(response,filename):
try:
app = QApplication(sys.argv)
except:
app = QCoreApplication.instance()
web = QWebView()
stylelink = "%s%s" % (media_root,'images/css/reportGenerator.css')
web.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(stylelink))
web.setHtml(response)
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filename)
def convertToPdf():
web.print_(printer)
#webresponse = web.close()
#QObject.disconnect()
#print QCoreApplication.instance().children()[0].interrupt()
#qthread = QCoreApplication.instance().thread()#.cleanup()
#qthread.exit()
QCoreApplication.exit()
QObject.connect(web, SIGNAL("loadFinished(bool)"), convertToPdf())
Code comments:
Currently I have made a try/except clause to be able to keep the code running, by using the current QApplication instance (to avoid the 'instance exists error'), but this just doesn't seem right?. I mean, having a QAppliation running for the duration of my Apache server (which will be the case in production) seems a bit off. Shouldn't it be possible to quit it after the PDF conversion is done?
Other than the QCoreApplication.exit() I have tried to use the sys.exit(app.exec_()) method, as is often seen in examples and snippets. This however just causes an error and makes Python crash and the code works perfectly without this. (well it creates the PDF, but won't quit).
All the lines commented out are the previous attempts I have made to quit the QApplication.
In short: I don't know what it is, but it just won't quit. Can anyone suggest why?
Update: After the latest answer I have edited the code to respond to the input. This is how the final part looks now:
def convertToPdf():
web.print_(printer)
app.exit()
web.loadFinished.connect(convertToPdf)
app.exec_()
I do, however, still get an error:
2012-04-30 00:16:10.791 Python[21241:1803] * Assertion failure in +[NSUndoManager _endTopLevelGroupings], /SourceCache/Foundation/Foundation-833.24/Misc.subproj/NSUndoManager.m:324
Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there.
This error only occurs when I implement app.exec_(). However without app.exec_() it is just the normal issue again, with no quitting.
Any ideas?
Update 2: this is the newest code, fixed in accordance with matas latest suggestion:
app = QApplication(sys.argv)
web = QWebView()
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filename)
def convertToPdf():
web.print_(printer)
app.exit()
web.loadFinished.connect(convertToPdf)
web.setHtml(response)
I still have the same problem however.

what i can say is this:
QObject.connect(web, SIGNAL("loadFinished(bool)"), convertToPdf())
here you call convertToPdf(), if you want to connect the signal, omit the parenthesis!
you could also use this much clearer syntax:
web.loadFinished.connect(convertToPdf)
you may also want to add a parameter to convertToPdf, as it is called with a boolean indicating wheather loading was successful or not.
And using app.exit() should be enough.
oh, and when you use Gui-Components you need to use a QApplication. A QCoreApplication won't do!
edit: it's important to call web.setHtml after you've connected the loadFinished signal! otherwise if the loading already is finished, your function will never be executed!
edit: this works without any problem for me:
#!/usr/bin/env python
from PySide.QtGui import *
from PySide.QtWebKit import *
import sys
from subprocess import Popen, PIPE
def createPdf(html, filename):
app = QApplication(sys.argv)
web = QWebView()
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filename)
def convertToPdf():
web.print_(printer)
app.exit()
app.deleteLater()
web.loadFinished.connect(convertToPdf)
web.setHtml(html)
app.processEvents()
def createPdfInSubprocess(html, filename):
p = Popen(["python", __file__, filename],
stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate(html)
return (p.returncode, out, err)
if __name__ == '__main__':
if len(sys.argv) > 1:
# read html from stdin, filename from cmdline
html = "\n".join(sys.stdin.readlines())
createPdf(html, sys.argv[1])
# print("done")
else:
# test if it's working
ret = createPdfInSubprocess(
"<html><h1>test</h1>it's working...</html>", "test.pdf")
print(ret)
Even without all the calls to app.exit(), app.deleteLater(), app.processEvents(), it still works... but can't hurt to have that.
One more important thing: QApplications have to be created from the main thread! So if it's running inside a django app it's probably not going to work, that's why i added the subprocess stuff...

PySide is designed such that there is only ever one instance of QCoreApplication within a process. The best reference to this (possibly undocumented fact) that I could find was http://bugs.pyside.org/show_bug.cgi?id=855.
Basically, there may be dangling references to the qapplication that prevent it from being reaped by the garbage collector, so even if you tell the application to exit and you delete your reference, there may still be other references lying around.

Related

Redirect debug output of Chromium in Python

I have a pywebview, that launches a Qt window utilizing the QtWebEngine with Chromium on Windows.
When starting the webview in debug mode, the following output is of particular interest for me:
DevTools listening on ws://127.0.0.1:8228/devtools/browser/<GUID>
This line is output by the Chromium engine itself and I want to make the debug port and GUID available in my application. For that, my idea is to redirect all terminal outputs to a StringIO stream.
Here is a minimum example of what I have done so far, to redirect all outputs:
from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
import logging
from PyQt5 import QtCore
import webview
def qt_message_handler(mode, context, message):
# Redirect message to stdout
print(message)
if __name__ == '__main__':
# Let the handler redirect all qt messages to stdout
QtCore.qInstallMessageHandler(qt_message_handler)
stream = StringIO()
# Redirect stdout and stderr to stream
with redirect_stdout(stream), redirect_stderr(stream):
# Redirect all loging outputs to stream
stream_handler = logging.StreamHandler(stream)
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
for logger in loggers:
for handler in logger.handlers:
logger.removeHandler(handler)
logger.addHandler(stream_handler)
# Start the webview window
window = webview.create_window('Webview window')
webview.start(gui='qt', debug=True)
Ths approach redirects all outputs to the stream object as intended except the one line output by Chromium mentioned at the beginning.
So i played around with the QTWEBENGINE_CHROMIUM_FLAGS environment variable. Since pywebview overrides this environment variable, I changed it directly in the pywebview module. However no argument passed to Chromium by this environment variable seems to change the output behaviour of this specific line. Looking at the Chromium source code, this makes sense:
std::string message = base::StringPrintf(
"\nDevTools listening on ws://%s%s\n", ip_address->ToString().c_str(),
browser_guid.c_str());
fprintf(stderr, "%s", message.c_str());
fflush(stderr);
Since the message is directly printed to stderr no Chromium logging flags have an influence. But it's clearly evident, that the message gets printed to stderr, so why does a redirect of stderr in Python have no effect on this message? Is there any way to redirect this message in python?
Bonus question:
A further look into the Chromium source code shows, that the debug port and GUID are also written into a file:
if (!output_directory.empty()) {
base::FilePath path =
output_directory.Append(kDevToolsActivePortFileName);
std::string port_target_string = base::StringPrintf(
"%d\n%s", ip_address->port(), browser_guid.c_str());
if (base::WriteFile(path, port_target_string.c_str(),
static_cast<int>(port_target_string.length())) < 0) {
LOG(ERROR) << "Error writing DevTools active port to file";
}
}
I just can't figure out where Chromium stores this data. Where can this file be located on Windows?
I have made some findings that, while not directly answering the question, do lead to a conclusion:
Why is the stderr of Chromium not redirected? This is because Chromium is launched in a child process by the QtWebEngine. While it would be possible, to get the output of a child process in Python, it would require to mess around with the QtWebEngine. At this point I decided, that this is not worth the effort.
Regarding my bonus question, I found out that the file containting the debug port and GUID should be named DevToolsActivePort. However, this file is not created when using the QtWebEngine and I haven't found out yet, why this is the case. See this question for a possible answer in the future: QtWebEngine: DevToolsActivePort file is missing
How did I solve my issue? I found out, that a query exists which returns the websocket url among other things: http://<debug_server_url>/json/version (see this answer). While you can determine the debug port within the QtWebEngine, I solved it for my pywebview application similar to this:
import requests
from PySide6 import QtCore
import webview
debug_server_url = None
def qt_message_handler(mode, context, message):
# Check if the message is the one containing the debug server url
if message.startswith('Remote debugging server started successfully.'):
# Debug server url is at the end of the message
global debug_server_url
debug_server_url = message.split()[-1]
def on_loaded():
if debug_server_url is not None:
websocket_debugger_url = requests.get(debug_server_url + '/json/version').json()['webSocketDebuggerUrl']
print(websocket_debugger_url)
if __name__ == '__main__':
# Define a custom qt message handler
QtCore.qInstallMessageHandler(qt_message_handler)
window = webview.create_window('Webview window')
window.events.loaded += on_loaded
webview.start(gui='qt', debug=True)

Pywebview blocks flask app unless I open two webviews

I built a python desktop webapp with a fairly simple flask backend which I'm currently serving simply through Chrome. I'm trying to package it using pywebview, but I ran into an intestesting roadblock.
I have code that looks like this in the main file of my package:
import os
import sys
from flasher import app
import httplib
import webview
import threading
def start_prod_server():
runserver(debug=False, reloader=False)
def start_gui():
webview.create_window("MyAppName", "http://localhost:5000", height=1000)
def runserver(debug=True, reloader=False):
port = int(os.environ.get('PORT', 5000))
url = "http://localhost:{}".format(port)
print("ready!")
app.run(host='::', port=port, debug=debug, use_reloader=reloader)
if __name__ == '__main__':
t = threading.Thread(target=start_prod_server)
t.daemon = True
t.start()
# This never works
threading.Timer(1.5, start_gui).start()
sys.exit()
This fails almost systematically, the window shows up blank and the backend doesn't respond to anything (including requests from a separate desktop browser).
I discovered accidentally that if I start two webview threads, it always works:
if __name__ == '__main__':
t = threading.Thread(target=start_prod_server)
t.daemon = True
t.start()
# This works every time
threading.Timer(1.5, start_gui).start()
threading.Timer(1.5, start_gui).start()
sys.exit()
What could be causing this? My understanding of threading in Python is fairly limited, so I'm not sure where to look. Is this likely to be a bug in pywebview, or am I doing something wrong with my threads?
I'm also open to suggestions of alternatives for the webview part, but I want to keep the python/flask part since the app already works fine as it is.
Well, after a bit of digging, I think I found the solution myself.
For whatever reason, webview.create_window() wasn't navigating to the page and getting stuck. Opening up the second window was causing both windows to navigate to the url and allowing the app to continue. (I still haven't understood why)
The problem is solved simply by adding webview.load_url("http://localhost:5000") like so:
if __name__ == '__main__':
t = threading.Thread(target=start_prod_server)
t.daemon = True
t.start()
threading.Timer(1.5, start_gui).start()
webview.load_url("http://localhost:5000")
sys.exit()
Any insight as to why this happens is still welcome though... In the source for pywebview, the two methods are making the exact same call to self.browser.web_browser.Navigate(url)

python parallel processes return exit code

lets see if I can make this clear... I'm a total Python beginner so bear with me, this is my first python program (though I'm familiar with basic scripting in a few other languages). I've been searching around for hours and I'm sure the answer to this is fairly simple but I have yet to get it to work properly.
I'm writing a code that should launch multiple commandline processes, and when each one finishes I want to update a cell in a QTableWidget. The table has a row for each process to run, and each row has a cell for the "status" of the process.
I can run this no problem if I just do a for loop, spawning one process per row using subprocess.call() however this is too linear and I would like to fire them all off at the same time and not hang the program for each loop cycle. I've been digging through the subprocess documentation and am having a really hard time with it. I understand that I need to use subprocess.Popen (which will prevent my program from hanging while the process runs, and thus I can spawn multiple instances). Where I run into trouble is getting the exit code back so that I can update my table, without hanging the program - for instance using subprocess.wait() followed by a subprocess.returncode still just sticks until the process completes. I need a sort of "when process completes, check the exit code and run a function that updates the QTableWidget."
I did find these two posts that seemed to get me going in the right direction, but didn't quite get me there:
Understanding Popen.communicate
How to get exit code when using Python subprocess communicate method?
Hopefully that made sense. Here's a simplified version of my code, I realize it is half-baked and half-broken but I've been screwing around with it for over an hour and I've lost track of a few things...
import os, subprocess
ae_app = 'afterfx'
ae_path = os.path.join('C:/Program Files/Adobe/Adobe After Effects CC 2015/Support Files', ae_app + ".exe")
filename = "E:/Programming/Python/Archive tool/talk.jsx"
commandLine = 'afterfx -noui -r ' + filename
processList = [commandLine]
processes = []
for process in processList:
f = os.tmpfile()
aeProcess = subprocess.Popen(process, executable=ae_path, stdout=f)
processes.append((aeProcess, f))
for aeProcess, f in processes:
# this is where I need serious help...
aeProcess.wait()
print "the line is:"
print aeProcess.returncode
Spencer
You mentioned PyQt, so you can use PyQt's QProcess class.
def start_processes(self, process_list):
for cmd, args in process_list:
proc = QProcess(self)
proc.finished.connect(self.process_finished)
proc.start(cmd, args)
def process_finished(self, code, status):
# Do something
UPDATE: Added fully working example. Works properly for both PyQt4 and PyQt5 (to switch just comment line 3 and uncomment line 4)
sleeper.py
import sys
from time import sleep
from datetime import datetime as dt
if __name__ == '__main__':
x = int(sys.argv[1])
started = dt.now().time()
sleep(x)
ended = dt.now().time()
print('Slept for: {}, started: {}, ended: {}'.format(x, started, ended))
sys.exit(0)
main.py
import sys
from PyQt5 import QtCore, QtWidgets
# from PyQt4 import QtCore, QtGui as QtWidgets
class App(QtWidgets.QMainWindow):
cmd = r'python.exe C:\_work\test\sleeper.py {}'
def __init__(self):
super(App, self).__init__()
self.setGeometry(200, 200, 500, 300)
self.button = QtWidgets.QPushButton('Start processes', self)
self.button.move(20, 20)
self.editor = QtWidgets.QTextEdit(self)
self.editor.setGeometry(20, 60, 460, 200)
self.button.clicked.connect(self.start_proc)
def start_proc(self):
for x in range(5):
proc = QtCore.QProcess(self)
proc.finished.connect(self.finished)
proc.start(self.cmd.format(x))
def finished(self, code, status):
self.editor.append(str(self.sender().readAllStandardOutput()))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
gui = App()
gui.show()
app.exec_()

How to handle console exit and object destruction

Given this code:
from time import sleep
class TemporaryFileCreator(object):
def __init__(self):
print 'create temporary file'
# create_temp_file('temp.txt')
def watch(self):
try:
print 'watching tempoary file'
while True:
# add_a_line_in_temp_file('temp.txt', 'new line')
sleep(4)
except (KeyboardInterrupt, SystemExit), e:
print 'deleting the temporary file..'
# delete_temporary_file('temp.txt')
sleep(3)
print str(e)
t = TemporaryFileCreator()
t.watch()
during the t.watch(), I want to close this application in the console..
I tried using CTRL+C and it works:
However, if I click the exit button:
it doesn't work.. I checked many related questions about this but it seems that I cannot find the right answer..
What I want to do:
The console can be exited while the program is still running.. to handle that, when the exit button is pressed, I want to make a cleanup of the objects (deleting of created temporary files), rollback of temporary changes, etc..
Question:
how can I handle console exit?
how can I integrate it on object destructors (__exit__())
Is it even possible? (how about py2exe?)
Note: code will be compiled on py2exe.. "hopes that the effect is the same"
You may want to have a look at signals. When a *nix terminal is closed with a running process, this process receives a couple signals. For instance this code waits for the SIGHUB hangup signal and writes a final message. This codes works under OSX and Linux. I know you are specifically asking for Windows but you might want to give it a shot or investigate what signals a Windows command prompt is emitting during shutdown.
import signal
import sys
def signal_handler(signal, frame):
with open('./log.log', 'w') as f:
f.write('event received!')
signal.signal(signal.SIGHUP, signal_handler)
print('Waiting for the final blow...')
#signal.pause() # does not work under windows
sleep(10) # so let us just wait here
Quote from the documentation:
On Windows, signal() can only be called with SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM. A ValueError will be raised in any other case.
Update:
Actually, the closest thing in Windows is win32api.setConsoleCtrlHandler (doc). This was already discussed here:
When using win32api.setConsoleCtrlHandler(), I'm able to receive shutdown/logoff/etc events from Windows, and cleanly shut down my app.
And if Daniel's code still works, this might be a nice way to use both (signals and CtrlHandler) for cross-platform purposes:
import os, sys
def set_exit_handler(func):
if os.name == "nt":
try:
import win32api
win32api.SetConsoleCtrlHandler(func, True)
except ImportError:
version = “.”.join(map(str, sys.version_info[:2]))
raise Exception(”pywin32 not installed for Python ” + version)
else:
import signal
signal.signal(signal.SIGTERM, func)
if __name__ == "__main__":
def on_exit(sig, func=None):
print "exit handler triggered"
import time
time.sleep(5)
set_exit_handler(on_exit)
print "Press to quit"
raw_input()
print "quit!"
If you use tempfile to create your temporary file, it will be automatically deleted when the Python process is killed.
Try it with:
>>> foo = tempfile.NamedTemporaryFile()
>>> foo.name
'c:\\users\\blah\\appdata\\local\\temp\\tmpxxxxxx'
Now check that the named file is there. You can write to and read from this file like any other.
Now kill the Python window and check that file is gone (it should be)
You can simply call foo.close() to delete it manually in your code.

python: Process finished with exit code 1 when using PyCharm and PyQt5

I have three Python(3.4.3) scripts. One of them is for controlling the .ui file generated by PyQt5. When I run the GUI program it accepts all the data and everything and when I press the OK button on an InputDialog the window closes and the console displays.
Process finished with exit code 1
When I run the same code on Python IDLE, it shows:
<<<<<<RESTART>>>>>>
This never happenned when I used this same Python(3.4.3 or 2.7) code on Visual Studio. What could be the reason?
Here is the code of the python file controlling the .ui file.
import sys
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from Email import encrypt_email
from Email import decrypt_email
from Email import newuser
qtCreatorFile = "rsegui.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
user, ok = QtWidgets.QInputDialog.getText(self, 'New User',
'Are you a new user?')
user=str(user)
if user in "YESYesyesYy":
email, ok = QtWidgets.QInputDialog.getText(self, 'New User',
'Enter Your Email ID:')
email1=str(email)
self.sender.setText(email)
newuser(email1)
self.encrypt_and_send.clicked.connect(self.EncryptEmail)
self.decrypt.clicked.connect(self.DecryptEmail)
self.clear.clicked.connect(self.ClearEncrypt)
self.clear_2.clicked.connect(self.ClearDecrypt)
self.sender.setPlaceholderText("Your Email ID")
self.receiver.setPlaceholderText("Receivers, Separate them by ';'")
self.subject.setPlaceholderText("Enter Subject")
self.message.setPlaceholderText("Enter Message")
self.sender_2.setPlaceholderText("Your Email ID")
self.message_2.setPlaceholderText("Encrypted Text")
def EncryptEmail(self):
sender = str(self.sender.text())
receiver = str(self.receiver.text())
receivers = receiver.split(';')
subject = str(self.subject.text())
message = str(self.message.text())
password, ok = QtWidgets.QInputDialog.getText(self, 'Password',
'Enter your password:',QtWidgets.QLineEdit.Password)
encrypt_email(sender,receivers,subject,message,password)
def DecryptEmail(self):
email = str(self.sender_2.text())
message = str(self.message_2.text())
self.decrypted.setText(decrypt_email(email,message))
def ClearDecrypt(self):
self.sender_2.clear()
self.message_2.clear()
def ClearEncrypt(self):
self.sender.clear()
self.message.clear()
self.receiver.clear()
self.subject.clear()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
I have dealt with the same problem, and the answer is twofold:
The reason it's crashing could be any number of things. It's probably a programming bug, calling a function that doesn't exist, passing a widget instead of a layout, etc. But since you're not getting useful output you don't know where to look for the culprit. This is caused by:
PyQT raises and catches exceptions, but doesn't pass them along. Instead it just exits with a status of 1 to show an exception was caught.
To catch the exceptions, you need to overwrite the sys exception handler:
# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook
def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)
# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
Then in your execution code, wrap it in a try/catch.
try:
sys.exit(app.exec_())
except:
print("Exiting")
I had the same problem in pycharm, python 3.8, qt5. The stacktrace was never shown for qt errors inside pycharm; running the file from cmd the error was shown correctly instead.
I solved by doing the following:
open Edit Configurations of the file you want to run, scroll down and check the box Emulate terminal in output console.
You have used self.sender.setText(email)
This is probably causing the problem in my opinion because, "sender" is the name in QObject's function and it does not have any setText attribute, so there may be the problem.
You have to specifically call the widget and setText to it.
For this, you can use instances of the py file of the layout creator.
I had the same issue when I was trying to use this self.ui.lineEdit().text()
Here, the problem was -> I was calling the lineEdit function, whereas I had to use it's one attribute.

Categories