Created
August 31, 2017 23:16
-
-
Save ocomsoft/ea581b172b72b23a1bcede21d9c76ee7 to your computer and use it in GitHub Desktop.
Run Django in pywebview and CherryPy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Inspired by https://gist.github.com/nigma/4349257 and https://github.com/r0x0r/pywebview/blob/master/examples/flask_app/src/backend/main.py | |
import os | |
import webbrowser | |
from time import sleep | |
import django | |
from threading import Timer, Thread | |
import logging | |
import webview | |
os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production" | |
import cherrypy | |
django.setup() | |
from django.conf import settings | |
from django.core.handlers.wsgi import WSGIHandler | |
logger = logging.getLogger(__name__) | |
class DjangoApplication(object): | |
HOST = "127.0.0.1" | |
PORT = 8001 | |
def mount_static(self, url, root): | |
""" | |
:param url: Relative url | |
:param root: Path to static files root | |
""" | |
config = { | |
'tools.staticdir.on': True, | |
'tools.staticdir.dir': root, | |
'tools.expires.on': True, | |
'tools.expires.secs': 86400 | |
} | |
cherrypy.tree.mount(None, url, {'/': config}) | |
def open_browser(self): | |
# Wait for server to be UP! | |
while not self.url_ok(self.HOST, self.PORT): | |
sleep (0.1) | |
#Timer(3, webbrowser.open, ("http://%s:%s" % (self.HOST, self.PORT),)).start() | |
webview.create_window("Ocom App", "http://%s:%s" % (self.HOST, self.PORT), min_size=(800, 600)) | |
def url_ok(self, url, port): | |
# Use httplib on Python 2 | |
try: | |
from http.client import HTTPConnection | |
except ImportError: | |
from httplib import HTTPConnection | |
try: | |
conn = HTTPConnection(url, port) | |
conn.request("GET", "/dockerStatus/") | |
r = conn.getresponse() | |
return r.status == 200 | |
except: | |
logger.exception("Server not started") | |
return False | |
def run(self): | |
cherrypy.config.update({ | |
'server.socket_host': self.HOST, | |
'server.socket_port': self.PORT, | |
'engine.autoreload_on': False, | |
'log.screen': True | |
}) | |
self.mount_static(settings.STATIC_URL, settings.STATIC_ROOT) | |
self.mount_static(settings.MEDIA_URL, settings.MEDIA_ROOT) | |
cherrypy.log("Loading and serving Django application") | |
cherrypy.tree.graft(WSGIHandler()) | |
cherrypy.engine.start() | |
t = Thread(target=self.open_browser) | |
t.daemon = True | |
t.start() | |
cherrypy.engine.block() | |
if __name__ == "__main__": | |
DjangoApplication().run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Add to Requirements.txt | |
pywebview[qt5]==1.7 # On Linux.. | |
CherryPy==11.0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following works with Django 4.2. In DEBUG mode Django servers the static files. Otherwise one would need CherryPy.