From a688393d858b96de28bb4fa9180170cf70375cd3 Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Tue, 8 Jul 2025 13:56:27 -0700 Subject: [PATCH] 0.70.0: Fix web app template (#49) * Fix web template * Update flutter_bootstrap.js * Update pubspec.yaml --- {{cookiecutter.out_dir}}/pubspec.yaml | 2 +- .../web/flutter_bootstrap.js | 21 ++++++---- {{cookiecutter.out_dir}}/web/index.html | 41 +++++++++++-------- {{cookiecutter.out_dir}}/web/python-worker.js | 16 ++++---- {{cookiecutter.out_dir}}/web/python.js | 12 +++--- 5 files changed, 51 insertions(+), 41 deletions(-) diff --git a/{{cookiecutter.out_dir}}/pubspec.yaml b/{{cookiecutter.out_dir}}/pubspec.yaml index 0f8a3ed..d560ac5 100644 --- a/{{cookiecutter.out_dir}}/pubspec.yaml +++ b/{{cookiecutter.out_dir}}/pubspec.yaml @@ -26,7 +26,7 @@ dependency_overrides: flet: git: path: packages/flet - ref: v1 + ref: main url: https://github.com/flet-dev/flet.git # serious_python: diff --git a/{{cookiecutter.out_dir}}/web/flutter_bootstrap.js b/{{cookiecutter.out_dir}}/web/flutter_bootstrap.js index 675cf77..802762a 100644 --- a/{{cookiecutter.out_dir}}/web/flutter_bootstrap.js +++ b/{{cookiecutter.out_dir}}/web/flutter_bootstrap.js @@ -1,21 +1,26 @@ {{ '{{flutter_js}}' }} {{ '{{flutter_build_config}}' }} -var config = {}; -if (globalThis.webRenderer != "auto") { - config.renderer = globalThis.webRenderer; +var flutterConfig = { + multiViewEnabled: flet.multiView, + assetBase: flet.assetBase +}; +if (flet.webRenderer != "auto") { + flutterConfig.renderer = flet.webRenderer; } -if (globalThis.canvasKitBaseUrl) { - config.canvasKitBaseUrl = globalThis.canvasKitBaseUrl; +if (flet.noCdn) { + flutterConfig.canvasKitBaseUrl = flet.canvasKitBaseUrl; + flutterConfig.fontFallbackBaseUrl = flet.fontFallbackBaseUrl; } _flutter.loader.load({ - config: config, + config: flutterConfig, serviceWorkerSettings: { serviceWorkerVersion: {{ '{{flutter_service_worker_version}}' }}, }, onEntrypointLoaded: async function (engineInitializer) { - const appRunner = await engineInitializer.initializeEngine({useColorEmoji: useColorEmoji}); - await appRunner.runApp(); + const engine = await engineInitializer.initializeEngine(flutterConfig); + flet.flutterApp = await engine.runApp(); + flet.flutterAppResolve(flet.flutterApp); } }); \ No newline at end of file diff --git a/{{cookiecutter.out_dir}}/web/index.html b/{{cookiecutter.out_dir}}/web/index.html index 568fc8b..82ba743 100644 --- a/{{cookiecutter.out_dir}}/web/index.html +++ b/{{cookiecutter.out_dir}}/web/index.html @@ -11,33 +11,38 @@ - + - - - - {{ cookiecutter.product_name }} - - + flet.flutterAppLoaded = new Promise((resolve) => { + flet.flutterAppResolve = resolve; + }); + + flet.flutterAppLoaded.then((flutterApp) => { + console.log("Flutter app loaded"); + }); + @@ -45,4 +50,4 @@ - \ No newline at end of file + diff --git a/{{cookiecutter.out_dir}}/web/python-worker.js b/{{cookiecutter.out_dir}}/web/python-worker.js index e2320f6..d73b59f 100644 --- a/{{cookiecutter.out_dir}}/web/python-worker.js +++ b/{{cookiecutter.out_dir}}/web/python-worker.js @@ -24,16 +24,16 @@ self.initPyodide = async function () { if "app_package_url" in py_args: app_package_url = py_args["app_package_url"] - + if app_package_url is None: app_package_url = "assets/app/app.zip" - + if "python_module_name" in py_args: python_module_name = py_args["python_module_name"] - + if python_module_name is None: python_module_name = "main" - + if "micropip_include_pre" in py_args: micropip_include_pre = py_args["micropip_include_pre"] @@ -51,12 +51,12 @@ self.initPyodide = async function () { print("Saving script to a file") with open(f"{python_module_name}.py", "w") as f: f.write(py_args["script"]); - + pkgs_path = "__pypackages__" if os.path.exists(pkgs_path): print(f"Adding {pkgs_path} to sys.path") sys.path.insert(0, pkgs_path) - + if os.path.exists("requirements.txt"): with open("requirements.txt", "r") as f: deps = [line.rstrip() for line in f] @@ -65,7 +65,7 @@ self.initPyodide = async function () { if "dependencies" in py_args: await micropip.install(py_args["dependencies"], pre=micropip_include_pre) - + # Execute app runpy.run_module(python_module_name, run_name="__main__") `); @@ -95,4 +95,4 @@ self.onmessage = async (event) => { // message flet_js.send(event.data); } -}; \ No newline at end of file +}; diff --git a/{{cookiecutter.out_dir}}/web/python.js b/{{cookiecutter.out_dir}}/web/python.js index d63e873..db2e34a 100644 --- a/{{cookiecutter.out_dir}}/web/python.js +++ b/{{cookiecutter.out_dir}}/web/python.js @@ -1,4 +1,4 @@ -const defaultPyodideUrl = "https://cdn.jsdelivr.net/pyodide/v0.27.5/full/pyodide.js"; +const defaultPyodideUrl = "https://cdn.jsdelivr.net/pyodide/v0.27.7/full/pyodide.js"; let _apps = {}; let _documentUrl = document.URL; @@ -30,12 +30,12 @@ globalThis.jsConnect = async function(appId, args, dartOnMessage) { // initialize worker app.worker.postMessage({ - pyodideUrl: globalThis.pyodideUrl ?? defaultPyodideUrl, + pyodideUrl: flet.noCdn ? flet.pyodideUrl : defaultPyodideUrl, args: args, documentUrl: _documentUrl, - appPackageUrl: globalThis.appPackageUrl, - micropipIncludePre: globalThis.micropipIncludePre, - pythonModuleName: globalThis.pythonModuleName + appPackageUrl: flet.appPackageUrl, + micropipIncludePre: flet.micropipIncludePre, + pythonModuleName: flet.pythonModuleName }); await pythonInitialized; @@ -67,4 +67,4 @@ globalThis.jsDisconnect = async function(appId) { app.worker.onmessage = null; app.worker.onerror = null; } -} \ No newline at end of file +}