webview.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import annotations
  2. import sys
  3. import os.path
  4. import webview
  5. try:
  6. from platformdirs import user_config_dir
  7. has_platformdirs = True
  8. except ImportError:
  9. has_platformdirs = False
  10. from g4f.gui.gui_parser import gui_parser
  11. from g4f.gui.server.js_api import JsApi
  12. import g4f.version
  13. import g4f.debug
  14. def run_webview(
  15. debug: bool = False,
  16. http_port: int = None,
  17. ssl: bool = True,
  18. storage_path: str = None,
  19. gui: str = None
  20. ):
  21. if getattr(sys, 'frozen', False):
  22. dirname = sys._MEIPASS
  23. else:
  24. dirname = os.path.dirname(__file__)
  25. webview.settings['OPEN_EXTERNAL_LINKS_IN_BROWSER'] = True
  26. webview.settings['ALLOW_DOWNLOADS'] = True
  27. webview.create_window(
  28. f"g4f - {g4f.version.utils.current_version}",
  29. os.path.join(dirname, "client/index.html"),
  30. text_select=True,
  31. js_api=JsApi(),
  32. )
  33. if has_platformdirs and storage_path is None:
  34. storage_path = user_config_dir("g4f-webview")
  35. webview.start(
  36. private_mode=False,
  37. storage_path=storage_path,
  38. debug=debug,
  39. http_port=http_port,
  40. ssl=ssl,
  41. gui=gui
  42. )
  43. if __name__ == "__main__":
  44. parser = gui_parser()
  45. args = parser.parse_args()
  46. if args.debug:
  47. g4f.debug.logging = True
  48. run_webview(args.debug, args.port)