From 6af2d6bb6def6e12defdf64f845eb01bc79eba30 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Thu, 11 Aug 2022 20:59:57 +0300 Subject: [PATCH] Add Flag to Start App without Native GUI --- src/main.py | 49 ++++++++++++++++++++++++++++-------------------- src/utils/cli.py | 1 + 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/main.py b/src/main.py index ecef329e..bace607f 100644 --- a/src/main.py +++ b/src/main.py @@ -29,25 +29,30 @@ def run(): args = cli(state.cli_args) set_state(args) - # Setup GUI - gui = QtWidgets.QApplication([]) - gui.setQuitOnLastWindowClosed(False) - configure_screen = ConfigureScreen(args.config_file) - tray = create_system_tray(gui, configure_screen) - tray.show() + if args.no_gui: + # Start Server + configure_server(args, required=True) + start_server(app, host=args.host, port=args.port, socket=args.socket) + else: + # Setup GUI + gui = QtWidgets.QApplication([]) + gui.setQuitOnLastWindowClosed(False) + configure_screen = ConfigureScreen(args.config_file) + tray = create_system_tray(gui, configure_screen) + tray.show() - # Setup Server - configure_server(args, required=False) - server = ServerThread(app, args.host, args.port, args.socket) + # Setup Server + configure_server(args, required=False) + server = ServerThread(app, args.host, args.port, args.socket) - # Trigger First Run Experience, if required - if args.config is None: - configure_screen.show() + # Trigger First Run Experience, if required + if args.config is None: + configure_screen.show() - # Start Application - server.start() - gui.aboutToQuit.connect(server.terminate) - gui.exec() + # Start Application + server.start() + gui.aboutToQuit.connect(server.terminate) + gui.exec() def set_state(args): @@ -56,6 +61,13 @@ def set_state(args): state.verbose = args.verbose +def start_server(app, host=None, port=None, socket=None): + if socket: + uvicorn.run(app, proxy_headers=True, uds=socket) + else: + uvicorn.run(app, host=host, port=port) + + class ServerThread(QThread): def __init__(self, app, host=None, port=None, socket=None): super(ServerThread, self).__init__() @@ -68,10 +80,7 @@ class ServerThread(QThread): self.wait() def run(self): - if self.socket: - uvicorn.run(app, proxy_headers=True, uds=self.socket) - else: - uvicorn.run(app, host=self.host, port=self.port) + start_server(self.app, self.host, self.port, self.socket) if __name__ == '__main__': diff --git a/src/utils/cli.py b/src/utils/cli.py index a77b35a7..0e8a61ea 100644 --- a/src/utils/cli.py +++ b/src/utils/cli.py @@ -11,6 +11,7 @@ def cli(args=None): # Setup Argument Parser for the Commandline Interface parser = argparse.ArgumentParser(description="Start Khoj; A Natural Language Search Engine for your personal Notes, Transactions and Photos") parser.add_argument('--config-file', '-c', default='~/.khoj/khoj.yml', type=pathlib.Path, help="YAML file to configure Khoj") + parser.add_argument('--no-gui', action='store_true', default=False, help="Do not show native desktop GUI. Default: false") parser.add_argument('--regenerate', action='store_true', default=False, help="Regenerate model embeddings from source files. Default: false") parser.add_argument('--verbose', '-v', action='count', default=0, help="Show verbose conversion logs. Default: 0") parser.add_argument('--host', type=str, default='127.0.0.1', help="Host address of the server. Default: 127.0.0.1")