webview.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. import g4f.version
  12. import g4f.debug
  13. def run_webview(
  14. debug: bool = False,
  15. http_port: int = None,
  16. ssl: bool = True,
  17. storage_path: str = None,
  18. gui: str = None
  19. ):
  20. if getattr(sys, 'frozen', False):
  21. dirname = sys._MEIPASS
  22. else:
  23. dirname = os.path.dirname(__file__)
  24. webview.settings['OPEN_EXTERNAL_LINKS_IN_BROWSER'] = True
  25. webview.settings['ALLOW_DOWNLOADS'] = True
  26. webview.create_window(
  27. f"g4f - {g4f.version.utils.current_version}",
  28. os.path.join(dirname, "client/index.html"),
  29. text_select=True,
  30. )
  31. if has_platformdirs and storage_path is None:
  32. storage_path = user_config_dir("g4f-webview")
  33. webview.start(
  34. private_mode=False,
  35. storage_path=storage_path,
  36. debug=debug,
  37. http_port=http_port,
  38. ssl=ssl,
  39. gui=gui
  40. )
  41. if __name__ == "__main__":
  42. parser = gui_parser()
  43. args = parser.parse_args()
  44. if args.debug:
  45. g4f.debug.logging = True
  46. run_webview(args.debug, args.port)