Your imports and module structure seems like an incredible mess. Yet your application only contains 3 files. Let's try and improve that.
Value your namespaces
import this
Preferably in an interactive session, will give you an insight of how you should approach problems in Python. In your case, it is the last line Namespaces are one honking great idea -- let's do more of those!
which is at issue.
The core idea is to control where symbols can be found without risk of overriding them (or doing so knowingly). As such, from <something> import *
is a bad practice and should either be:
import <something>
or
from <something> import <required_name1>, ..., <required_nameN>
This also means that you should removed unnecessary imports.
Avoid circular imports
I'm talking about having from main import *
in routes.py
and from routes import *
in main.py
.
One of the major issue with circular import is that one of them might bring an half-loaded module, (mainly) leading to NameError
s.
Why do you need to import routes
in the first place? Nothing is used from that in main.py
, so you can remove it.
Why do you need to import main
in the first place? To be able to use render_template
and the app
object? Well, you should import render_template
in routes.py
and create app
in there instead.
Don't over-split your code
Modules are good since they allow you to differentiate purposes for pieces of codes. But too much modules makes the code unmanageable.
At this stage of development you could still have all your code in a single file without loosing readability. But I imagine that you'll want to make your application grow and I understand your need to split it into different modules.
You made a good job identifying purposes:
- a module to manage your webapp (
routes.py
);
- a module to manage your GUI (in case you'll want to get something better than a
QWebView
in the future).
But I think you made a mistake making your GUI module "secondary". The GUI is the entry point of the application for your users, I would have made it the main module.
Then you can put routes.py
into its own webapp
(or whatever name fits its purpose more) folder. This will allow you to keep everything related to the "Web" part of your application in a single place. Try to at least separate your routes from your utilities functions.
You should also try to name your modules relative to what they do overall. main.py
is not a good name.
New layout
pyfladesk.py
import sys
from PyQt4.QtCore import QThread, QUrl
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView
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 provide_GUI_for(application):
qtapp = QApplication(sys.argv)
webapp = FlaskThread(application)
webapp.start()
qtapp.aboutToQuit.connect(webapp.terminate)
webview = QWebView()
webview.load(QUrl(ROOT_URL))
webview.show()
return qtapp.exec_()
if __name__ == '__main__':
from webapp.routes import app
sys.exit(provide_GUI_for(app))
webapp/routes.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
The few more changes I made contains:
- improved naming a bit (not so much, you might come up with better names too);
- converting magic values to constants so they can easily be spotted and changed;
- used the callable
webapp.terminate
directly instead of creating a lambda
.