app.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # @Author: Bachir Soussi Chiadmi <bach>
  4. # @Date: 23-05-2017
  5. # @Email: bachir@figureslibres.io
  6. # @Last modified by: bach
  7. # @Last modified time: 21-04-2017
  8. # @License: GPL-V3
  9. import sys, os
  10. from socket import socket
  11. import socketserver
  12. import http.server
  13. import threading
  14. from PyQt5.QtCore import QFile, QIODevice, Qt, QTextStream, QUrl, QFileSystemWatcher
  15. from PyQt5.QtWidgets import (QAction, QApplication, QLineEdit, QMainWindow, QSizePolicy, QStyle, QTextEdit)
  16. from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkRequest
  17. from PyQt5.QtWebKitWidgets import QWebPage, QWebView
  18. import sass
  19. _PORT=0
  20. def main():
  21. server()
  22. compiler()
  23. app()
  24. def server():
  25. # find free port
  26. sock = socket()
  27. sock.bind(('', 0))
  28. global _PORT
  29. _PORT = sock.getsockname()[1]
  30. sock.close()
  31. # PORT = 8000
  32. # web werver
  33. # Handler = http.server.SimpleHTTPRequestHandler
  34. # httpd = socketserver.TCPServer(("", PORT), Handler)
  35. # httpd.serve_forever()
  36. httpd = http.server.HTTPServer(('', _PORT), http.server.SimpleHTTPRequestHandler)
  37. thread = threading.Thread(target=httpd.serve_forever)
  38. thread.start()
  39. print("serving at port", _PORT)
  40. def compiler():
  41. fs_watcher = QFileSystemWatcher(['assets/scss'])
  42. fs_watcher.directoryChanged.connect(compile_scss)
  43. compile_scss()
  44. def compile_scss():
  45. # src = open( 'assets/scss/main.scss' ).read()
  46. scss = sass.compile_file(b'assets/scss/main.scss')
  47. with open('assets/scss/main.css', 'w') as fp:
  48. fp.write(scss.decode('utf8'))
  49. def app():
  50. # QT webkit
  51. app = QApplication(sys.argv)
  52. browser = QWebView()
  53. # browser.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  54. global _PORT
  55. browser.load(QUrl('http://localhost:'+str(_PORT)))
  56. browser.show()
  57. sys.exit(app.exec_())
  58. if __name__ == "__main__":
  59. main()