Spaces:
Build error
Build error
Separate flet extensions and add type hint
Browse files- .gitignore +2 -1
- app.py +7 -1
- flet.pyi +28 -0
- flet_extensions.py +11 -0
- js_execution.py +0 -9
.gitignore
CHANGED
|
@@ -2,4 +2,5 @@
|
|
| 2 |
playground/
|
| 3 |
uploaded_files/
|
| 4 |
assets/
|
| 5 |
-
__pycache__/
|
|
|
|
|
|
| 2 |
playground/
|
| 3 |
uploaded_files/
|
| 4 |
assets/
|
| 5 |
+
__pycache__/
|
| 6 |
+
**/.DS_Store
|
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from fastapi import FastAPI
|
|
| 5 |
|
| 6 |
from flet_app import main
|
| 7 |
from js_execution import mount_flet_app
|
|
|
|
| 8 |
|
| 9 |
# Set up basic logging configuration
|
| 10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -18,5 +19,10 @@ async def lifespan(app: FastAPI):
|
|
| 18 |
|
| 19 |
app = FastAPI(lifespan=lifespan)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
# Use the new mount_flet_app function with the same arguments
|
| 22 |
-
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from flet_app import main
|
| 7 |
from js_execution import mount_flet_app
|
| 8 |
+
from flet_extensions import extend_flet_page
|
| 9 |
|
| 10 |
# Set up basic logging configuration
|
| 11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 19 |
|
| 20 |
app = FastAPI(lifespan=lifespan)
|
| 21 |
|
| 22 |
+
# Extend the Flet Page class
|
| 23 |
+
extend_flet_page()
|
| 24 |
+
|
| 25 |
# Use the new mount_flet_app function with the same arguments
|
| 26 |
+
flet_app = flet_fastapi.app(main)
|
| 27 |
+
|
| 28 |
+
mount_flet_app(app, "/", flet_app)
|
flet.pyi
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flet_core.page import Page as OriginalPage
|
| 2 |
+
|
| 3 |
+
class Page(OriginalPage):
|
| 4 |
+
def eval_js(self, js_code: str) -> str:
|
| 5 |
+
"""
|
| 6 |
+
Evaluates JavaScript code in the context of the current page.
|
| 7 |
+
|
| 8 |
+
This method allows execution of arbitrary JavaScript code within the
|
| 9 |
+
page's environment and returns the result as a string.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
js_code (str): The JavaScript code to be executed.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: The result of the JavaScript execution as a string.
|
| 16 |
+
|
| 17 |
+
Raises:
|
| 18 |
+
Exception: If there's an error during JavaScript execution.
|
| 19 |
+
|
| 20 |
+
Example:
|
| 21 |
+
result = page.eval_js("return document.title;")
|
| 22 |
+
"""
|
| 23 |
+
...
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Re-export other types from flet to maintain compatibility
|
| 27 |
+
from flet import *
|
| 28 |
+
from flet_runtime import *
|
flet_extensions.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import flet as ft
|
| 2 |
+
from js_execution import js_execution_router
|
| 3 |
+
|
| 4 |
+
def extend_flet_page():
|
| 5 |
+
async def eval_js(self: ft.Page, js_code: str) -> str:
|
| 6 |
+
endpoint, result_future = js_execution_router.create_dynamic_endpoint(js_code)
|
| 7 |
+
self.launch_url(endpoint, web_window_name="hidden-iframe")
|
| 8 |
+
return await result_future
|
| 9 |
+
|
| 10 |
+
if not hasattr(ft.Page, 'eval_js'):
|
| 11 |
+
ft.Page.eval_js = eval_js
|
js_execution.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import flet as ft
|
| 2 |
import asyncio
|
| 3 |
import logging
|
| 4 |
from fastapi import FastAPI, Request
|
|
@@ -71,14 +70,6 @@ class JSExecutionRouter:
|
|
| 71 |
|
| 72 |
js_execution_router = JSExecutionRouter()
|
| 73 |
|
| 74 |
-
# Monkey patch the Page class to add eval_js method
|
| 75 |
-
async def eval_js(self: ft.Page, js_code: str) -> str:
|
| 76 |
-
endpoint, result_future = js_execution_router.create_dynamic_endpoint(js_code)
|
| 77 |
-
self.launch_url(endpoint, web_window_name="hidden-iframe")
|
| 78 |
-
return await result_future
|
| 79 |
-
|
| 80 |
-
ft.Page.eval_js = eval_js # type: ignore
|
| 81 |
-
|
| 82 |
def add_js_execution_routes(app: FastAPI):
|
| 83 |
js_execution_router.init_app(app)
|
| 84 |
|
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import logging
|
| 3 |
from fastapi import FastAPI, Request
|
|
|
|
| 70 |
|
| 71 |
js_execution_router = JSExecutionRouter()
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def add_js_execution_routes(app: FastAPI):
|
| 74 |
js_execution_router.init_app(app)
|
| 75 |
|