-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support and test qt + solara + pyinstaller
This way we know we can support standalone binaries using pyinstaller and qt.
- Loading branch information
1 parent
864939d
commit 13ba849
Showing
9 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tests/qtapp/render_test.vue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tests/qtapp/solara-qt-test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- mode: python ; coding: utf-8 -*- | ||
import sys | ||
from pathlib import Path | ||
import os | ||
|
||
from PyInstaller.building.build_main import Analysis | ||
from PyInstaller.building.api import COLLECT, EXE, PYZ | ||
from PyInstaller.building.osx import BUNDLE | ||
|
||
import solara | ||
# see https://github.com/spacetelescope/jdaviz/blob/main/.github/workflows/standalone.yml | ||
# for an example of how to sign the app for macOS | ||
codesign_identity = os.environ.get("DEVELOPER_ID_APPLICATION") | ||
|
||
# this copies over the nbextensions enabling json and the js assets | ||
# for all the widgets | ||
datas = [ | ||
(Path(sys.prefix) / "share" / "jupyter", "./share/jupyter"), | ||
(Path(sys.prefix) / "etc" / "jupyter", "./etc/jupyter"), | ||
("render_test.vue", "."), | ||
] | ||
|
||
block_cipher = None | ||
|
||
|
||
a = Analysis( | ||
["solara-qt-test.py"], | ||
pathex=[], | ||
binaries=[], | ||
datas=datas, | ||
hiddenimports=["rich.logging"], | ||
hookspath=[], | ||
hooksconfig={}, | ||
runtime_hooks=[], | ||
excludes=[], | ||
win_no_prefer_redirects=False, | ||
win_private_assemblies=False, | ||
cipher=block_cipher, | ||
noarchive=True, | ||
module_collection_mode={ | ||
"test_app": "pyz+py" | ||
}, | ||
) | ||
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) | ||
|
||
exe = EXE( | ||
pyz, | ||
a.scripts, | ||
[], | ||
exclude_binaries=True, | ||
name="solara-qt", | ||
debug=False, | ||
bootloader_ignore_signals=False, | ||
strip=False, | ||
upx=True, | ||
console=False, # with True, PySide very often does not show the window | ||
disable_windowed_traceback=False, | ||
argv_emulation=False, | ||
target_arch=None, | ||
codesign_identity=codesign_identity, | ||
entitlements_file="../entitlements.plist", | ||
) | ||
coll = COLLECT( | ||
exe, | ||
a.binaries, | ||
a.zipfiles, | ||
a.datas, | ||
strip=False, | ||
upx=True, | ||
upx_exclude=[], | ||
# directory name: dist/solara-qt | ||
name="solara-qt", | ||
) | ||
app = BUNDLE( | ||
exe, | ||
coll, | ||
name="solara-qt.app", | ||
icon="../solara.icns", | ||
entitlements_file="../entitlements.plist", | ||
bundle_identifier="com.widgetti.solara", | ||
version=solara.__version__, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tests/qtapp/test_app.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<template> | ||
<div>If you see this, it is all working. The Qt app should automatically close after a second.</div> | ||
</template> | ||
<script> | ||
module.exports = { | ||
mounted() { | ||
console.log("mounted") | ||
setTimeout(() => { | ||
this.rendered() | ||
}, 1000) | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import sys | ||
import threading | ||
from time import sleep | ||
from pathlib import Path | ||
|
||
import click | ||
import os | ||
|
||
# make sure you use pyside when distributing your app without having to use a GPL license | ||
from qtpy.QtWidgets import QApplication | ||
from qtpy.QtWebEngineWidgets import QWebEngineView | ||
from qtpy import QtCore | ||
|
||
|
||
HERE = Path(__file__).parent | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--port", | ||
default=int(os.environ.get("PORT", 0)), | ||
help="Port to run the server on, 0 for a random free port", | ||
) | ||
def run(port: int): | ||
sys.path.append(str(HERE)) | ||
os.environ["SOLARA_APP"] = "test_app" | ||
import test_app | ||
|
||
import solara.server.starlette | ||
|
||
server = solara.server.starlette.ServerStarlette(host="localhost", port=port) | ||
print(f"Starting server on {server.base_url}") | ||
server.serve_threaded() | ||
server.wait_until_serving() | ||
|
||
def test_success(value): | ||
print("test output", value) | ||
# calling app.quit seems to fail on windows and linux | ||
# possibly because we are in a non-qt-thread (solara) | ||
# app.quit() | ||
QtCore.QMetaObject.invokeMethod(app, "quit", QtCore.Qt.QueuedConnection) | ||
server.stop_serving() | ||
|
||
test_app.callback = test_success # type: ignore | ||
|
||
failed = False | ||
|
||
def fail_guard(): | ||
sleep(10) | ||
nonlocal failed | ||
print("failed") | ||
# similar as above | ||
QtCore.QMetaObject.invokeMethod(app, "quit", QtCore.Qt.QueuedConnection) | ||
failed = True | ||
|
||
app = QApplication([""]) | ||
web = QWebEngineView() | ||
web.setUrl(QtCore.QUrl(server.base_url)) | ||
web.show() | ||
|
||
threading.Thread(target=fail_guard, daemon=True).start() | ||
app.exec_() | ||
if failed: | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import solara | ||
import solara.lab | ||
|
||
|
||
def callback(event): | ||
print("Event received:", event) | ||
|
||
|
||
@solara.component_vue("render_test.vue") | ||
def RenderTest(event_rendered): | ||
pass | ||
|
||
|
||
@solara.component | ||
def Page(): | ||
RenderTest(event_rendered=callback) | ||
# make sure vue components of solara are working | ||
solara.lab.ThemeToggle() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters