From 19548bb21acae93265a9704377e32d300233971b Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Tue, 10 Sep 2024 16:09:32 +0200 Subject: [PATCH] feat: add --browser=qt to use an embedded browser instead of system one This also restore the --browser feature that was previously broken. --- jdaviz/cli.py | 39 ++++++++++++++++++++++++++++++++------- pyproject.toml | 4 ++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/jdaviz/cli.py b/jdaviz/cli.py index 669a3d3c3b..e5d44f3f08 100644 --- a/jdaviz/cli.py +++ b/jdaviz/cli.py @@ -62,7 +62,6 @@ def main(filepaths=None, layout='default', instrument=None, browser='default', # easily accessed e.g. in the file load dialog. os.environ['JDAVIZ_START_DIR'] = os.path.abspath('.') - from solara.__main__ import cli from jdaviz import solara solara.config = layout.capitalize() solara.data_list = file_list @@ -76,11 +75,37 @@ def main(filepaths=None, layout='default', instrument=None, browser='default', args += ['--auto-restart'] else: args += ['--production'] - cli(['run', 'jdaviz.solara', - '--theme-loader', 'plain', - '--theme-variant', theme, - '--host', host, - '--port', port] + args) + run_solara(host=host, port=port, theme=theme, browser=browser) + + +def run_solara(host, port, theme, browser): + os.environ["SOLARA_APP"] = "jdaviz.solara" + import solara.server.starlette + import solara.server.settings + solara.server.settings.theme.variant = theme + solara.server.settings.theme.loader = "plain" + + server = solara.server.starlette.ServerStarlette(host="localhost", port=port) + print(f"Starting server on {server.base_url}") + server.serve_threaded() + server.wait_until_serving() + if browser == "qt": + from qtpy.QtWidgets import QApplication + from qtpy.QtWebEngineWidgets import QWebEngineView + from qtpy import QtCore + + app = QApplication([""]) + web = QWebEngineView() + web.resize(1000, 1000) + web.setUrl(QtCore.QUrl(server.base_url)) + web.show() + + app.exec_() + else: + import webbrowser + controller = webbrowser.get(None if browser == 'default' else browser) + controller.open(server.base_url) + server.join() def _main(config=None): @@ -100,7 +125,7 @@ def _main(config=None): parser.add_argument('--instrument', type=str, default='nirspec', help='Manually specifies which instrument parser to use, for Mosviz') parser.add_argument('--browser', type=str, default='default', - help='Browser to use for application.') + help='Browser to use for application (use qt for embedded qt browser).') parser.add_argument('--theme', choices=['light', 'dark'], default='light', help='Theme to use for application.') parser.add_argument('--verbosity', choices=_verbosity_levels, default='info', diff --git a/pyproject.toml b/pyproject.toml index 39a7adfc47..a7a7daa9bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,6 +82,10 @@ docs = [ roman = [ "roman_datamodels>=0.17.1", ] +qt = [ + "qtpy", + "PySide6" +] [build-system] requires = [