Coverage for fastagency/ui/mesop/timer.py: 69%

13 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-19 12:16 +0000

1from pathlib import Path 1abc

2from typing import Any, Callable, Optional 1abc

3 

4import mesop.labs as mel 1abc

5import mesop.server.static_file_serving 1abc

6import mesop.server.wsgi_app 1abc

7from flask import Flask, Response 1abc

8from mesop.server.static_file_serving import ( 1abc

9 WEB_COMPONENTS_PATH_SEGMENT, 

10 noop, 

11 send_file_compressed, 

12) 

13from mesop.server.static_file_serving import ( 1abc

14 configure_static_file_serving as configure_static_file_serving_original, 

15) 

16 

17from ...logging import get_logger 1abc

18 

19logger = get_logger(__name__) 1abc

20 

21 

22def configure_static_file_serving( 1abc

23 app: Flask, 

24 static_file_runfiles_base: str, 

25 livereload_script_url: Optional[str] = None, 

26 preprocess_request: Callable[[], None] = noop, 

27 disable_gzip_cache: bool = False, 

28 default_allowed_iframe_parents: str = "'self'", 

29) -> None: 

30 logger.info("Configuring static file serving with patched method") 1d

31 

32 configure_static_file_serving_original( 1d

33 app=app, 

34 static_file_runfiles_base=static_file_runfiles_base, 

35 livereload_script_url=livereload_script_url, 

36 preprocess_request=preprocess_request, 

37 disable_gzip_cache=disable_gzip_cache, 

38 default_allowed_iframe_parents=default_allowed_iframe_parents, 

39 ) 

40 

41 @app.route(f"/{WEB_COMPONENTS_PATH_SEGMENT}/__fast_agency_internal__/<path:path>") # type: ignore[misc] 1d

42 def serve_web_components_fast_agency(path: str) -> Response: 1d

43 logger.info(f"Serve web components fast agency: {path}") 

44 

45 root = Path(__file__).parents[3].resolve() 

46 serving_path = f"{root}/{path}" 

47 

48 return send_file_compressed( # type: ignore[no-any-return] 

49 serving_path, 

50 disable_gzip_cache=disable_gzip_cache, 

51 ) 

52 

53 

54logger.info("Patching static file serving in Mesop") 1abc

55mesop.server.wsgi_app.configure_static_file_serving = configure_static_file_serving 1abc

56 

57 

58MEL_WEB_COMPONENT_PATH = "/__fast_agency_internal__/javascript/wakeup_component.js" 1abc

59 

60 

61@mel.web_component(path=MEL_WEB_COMPONENT_PATH) # type: ignore[misc] 1abc

62def wakeup_component( 1abc

63 *, 

64 on_wakeup: Callable[[mel.WebEvent], Any], 

65 key: Optional[str] = None, 

66) -> Any: 

67 return mel.insert_web_component( 

68 name="wakeup-component", 

69 key=key, 

70 events={ 

71 "wakeupEvent": on_wakeup, 

72 }, 

73 properties={}, 

74 )