File tree Expand file tree Collapse file tree 6 files changed +57
-12
lines changed Expand file tree Collapse file tree 6 files changed +57
-12
lines changed Original file line number Diff line number Diff line change 107
107
from mesop .features import page as page
108
108
from mesop .key import Key as Key
109
109
from mesop .runtime import runtime
110
+ from mesop .server .colab_run import colab_run as colab_run
110
111
111
112
_T = TypeVar ("_T" )
112
113
Original file line number Diff line number Diff line change 14
14
reset_runtime ,
15
15
runtime ,
16
16
)
17
+ from mesop .server .constants import EDITOR_PACKAGE_PATH , PROD_PACKAGE_PATH
17
18
from mesop .server .flags import port
19
+ from mesop .server .logging import log_startup
18
20
from mesop .server .server import configure_flask_app
19
21
from mesop .server .static_file_serving import configure_static_file_serving
20
22
@@ -71,23 +73,16 @@ def main(argv):
71
73
72
74
configure_static_file_serving (
73
75
flask_app ,
74
- static_file_runfiles_base = "mesop/mesop/web/src/app/prod/web_package"
76
+ static_file_runfiles_base = PROD_PACKAGE_PATH
75
77
if FLAGS .prod
76
- else "mesop/mesop/web/src/app/editor/web_package" ,
78
+ else EDITOR_PACKAGE_PATH ,
77
79
livereload_script_url = os .environ .get ("IBAZEL_LIVERELOAD_URL" ),
78
80
)
79
81
80
82
if FLAGS .verbose :
81
83
logging .getLogger ("werkzeug" ).setLevel (logging .INFO )
82
84
else :
83
- # Log basic information about where the server is running since
84
- # we don't get it printed by werkzeurg:
85
- # https://github.com/pallets/werkzeug/blob/eafbed0ce2a6bdf60e62de82bf4a8365188ac334/src/werkzeug/serving.py#L820C9-L820C17
86
- FgGreen = "\x1b [32m"
87
- Reset = "\x1b [0m"
88
- print (
89
- f"\n { FgGreen } Running server on: http://localhost:{ port ()} { Reset } " ,
90
- )
85
+ log_startup ()
91
86
logging .getLogger ("werkzeug" ).setLevel (logging .WARN )
92
87
93
88
flask_app .run (host = "0.0.0.0" , port = port (), use_reloader = False )
Original file line number Diff line number Diff line change
1
+ import threading
2
+
3
+ from mesop .runtime import enable_debug_mode
4
+ from mesop .server .constants import EDITOR_PACKAGE_PATH
5
+ from mesop .server .flags import port
6
+ from mesop .server .logging import log_startup
7
+ from mesop .server .server import configure_flask_app
8
+ from mesop .server .static_file_serving import configure_static_file_serving
9
+
10
+
11
+ def colab_run ():
12
+ """
13
+ Typically only used for Colab/Jupyter notebooks, otherwise you'll use the CLI
14
+ to execute a Mesop application.
15
+ """
16
+ flask_app = configure_flask_app ()
17
+ enable_debug_mode ()
18
+ configure_static_file_serving (
19
+ flask_app , static_file_runfiles_base = EDITOR_PACKAGE_PATH
20
+ )
21
+
22
+ log_startup ()
23
+
24
+ def run_flask_app ():
25
+ flask_app .run (host = "0.0.0.0" , port = port (), use_reloader = False )
26
+
27
+ # Launch Flask in background thread so we don't hog up the main thread
28
+ # for regular Colab usage.
29
+ threading .Thread (target = run_flask_app ).start ()
Original file line number Diff line number Diff line change
1
+ EDITOR_PACKAGE_PATH = "mesop/mesop/web/src/app/editor/web_package"
2
+ PROD_PACKAGE_PATH = "mesop/mesop/web/src/app/prod/web_package"
Original file line number Diff line number Diff line change
1
+ from mesop .server .flags import port
2
+
3
+
4
+ def log_startup ():
5
+ # Log basic information about where the server is running since
6
+ # we don't get it printed by werkzeurg:
7
+ # https://github.com/pallets/werkzeug/blob/eafbed0ce2a6bdf60e62de82bf4a8365188ac334/src/werkzeug/serving.py#L820C9-L820C17
8
+ FgGreen = "\x1b [32m"
9
+ Reset = "\x1b [0m"
10
+ print (
11
+ f"\n { FgGreen } Running server on: http://localhost:{ port ()} { Reset } " ,
12
+ )
Original file line number Diff line number Diff line change 3
3
import mesop .protos .ui_pb2 as pb
4
4
5
5
6
- def get_caller_source_code_location (levels : int = 1 ) -> pb .SourceCodeLocation :
6
+ def get_caller_source_code_location (
7
+ levels : int = 1 ,
8
+ ) -> pb .SourceCodeLocation | None :
7
9
current_frame = inspect .currentframe ()
8
10
9
11
# Walk backwards
@@ -20,7 +22,11 @@ def get_caller_source_code_location(levels: int = 1) -> pb.SourceCodeLocation:
20
22
caller_info = inspect .getframeinfo (current_frame )
21
23
22
24
# Get module from filepath
23
- segments = caller_info .filename .split ("runfiles/" )[1 ].split ("/" )
25
+ splitted_file = caller_info .filename .split ("runfiles/" )
26
+ # Can't always look up from runfiles (e.g. colab).
27
+ if len (splitted_file ) < 2 :
28
+ return None
29
+ segments = splitted_file [1 ].split ("/" )
24
30
segments [len (segments ) - 1 ] = segments [len (segments ) - 1 ][: len (".py" ) * - 1 ]
25
31
module = "." .join (segments )
26
32
You can’t perform that action at this time.
0 commit comments