2024-03-19 13:48:32 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-03-17 05:51:08 -04:00
|
|
|
import sys
|
|
|
|
import os.path
|
2024-03-13 00:27:54 -04:00
|
|
|
import webview
|
2024-03-15 09:55:53 -04:00
|
|
|
try:
|
|
|
|
from platformdirs import user_config_dir
|
|
|
|
has_platformdirs = True
|
|
|
|
except ImportError:
|
|
|
|
has_platformdirs = False
|
2024-03-13 00:27:54 -04:00
|
|
|
|
2024-04-06 10:30:35 -04:00
|
|
|
from g4f.gui.gui_parser import gui_parser
|
2024-04-06 10:58:15 -04:00
|
|
|
from g4f.gui.server.js_api import JsApi
|
2024-03-13 00:27:54 -04:00
|
|
|
import g4f.version
|
|
|
|
import g4f.debug
|
|
|
|
|
2024-03-15 09:55:53 -04:00
|
|
|
def run_webview(
|
|
|
|
debug: bool = False,
|
2024-04-06 10:37:19 -04:00
|
|
|
http_port: int = None,
|
2024-03-22 07:48:59 -04:00
|
|
|
ssl: bool = True,
|
2024-04-06 04:47:43 -04:00
|
|
|
storage_path: str = None,
|
|
|
|
gui: str = None
|
2024-03-15 09:55:53 -04:00
|
|
|
):
|
2024-03-17 05:51:08 -04:00
|
|
|
if getattr(sys, 'frozen', False):
|
|
|
|
dirname = sys._MEIPASS
|
|
|
|
else:
|
|
|
|
dirname = os.path.dirname(__file__)
|
2024-04-06 04:47:43 -04:00
|
|
|
webview.settings['OPEN_EXTERNAL_LINKS_IN_BROWSER'] = True
|
2024-03-19 13:48:32 -04:00
|
|
|
webview.settings['ALLOW_DOWNLOADS'] = True
|
2024-03-15 09:55:53 -04:00
|
|
|
webview.create_window(
|
|
|
|
f"g4f - {g4f.version.utils.current_version}",
|
2024-03-17 05:51:08 -04:00
|
|
|
os.path.join(dirname, "client/index.html"),
|
2024-03-16 05:48:37 -04:00
|
|
|
text_select=True,
|
2024-04-07 04:36:13 -04:00
|
|
|
js_api=JsApi(),
|
2024-03-15 09:55:53 -04:00
|
|
|
)
|
|
|
|
if has_platformdirs and storage_path is None:
|
|
|
|
storage_path = user_config_dir("g4f-webview")
|
2024-03-13 00:27:54 -04:00
|
|
|
webview.start(
|
|
|
|
private_mode=False,
|
2024-03-15 09:55:53 -04:00
|
|
|
storage_path=storage_path,
|
2024-03-16 05:48:37 -04:00
|
|
|
debug=debug,
|
2024-04-06 10:37:19 -04:00
|
|
|
http_port=http_port,
|
2024-04-06 04:47:43 -04:00
|
|
|
ssl=ssl,
|
|
|
|
gui=gui
|
2024-03-13 00:27:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = gui_parser()
|
|
|
|
args = parser.parse_args()
|
2024-03-15 09:55:53 -04:00
|
|
|
if args.debug:
|
|
|
|
g4f.debug.logging = True
|
2024-04-06 10:40:14 -04:00
|
|
|
run_webview(args.debug, args.port)
|