Skip to content

Commit 77741fe

Browse files
committed
Add Colab support with colab_run
1 parent 21f95d5 commit 77741fe

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

mesop/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
from mesop.features import page as page
108108
from mesop.key import Key as Key
109109
from mesop.runtime import runtime
110+
from mesop.server.colab_run import colab_run as colab_run
110111

111112
_T = TypeVar("_T")
112113

mesop/cli/cli.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
reset_runtime,
1515
runtime,
1616
)
17+
from mesop.server.constants import EDITOR_PACKAGE_PATH, PROD_PACKAGE_PATH
1718
from mesop.server.flags import port
19+
from mesop.server.logging import log_startup
1820
from mesop.server.server import configure_flask_app
1921
from mesop.server.static_file_serving import configure_static_file_serving
2022

@@ -71,23 +73,16 @@ def main(argv):
7173

7274
configure_static_file_serving(
7375
flask_app,
74-
static_file_runfiles_base="mesop/mesop/web/src/app/prod/web_package"
76+
static_file_runfiles_base=PROD_PACKAGE_PATH
7577
if FLAGS.prod
76-
else "mesop/mesop/web/src/app/editor/web_package",
78+
else EDITOR_PACKAGE_PATH,
7779
livereload_script_url=os.environ.get("IBAZEL_LIVERELOAD_URL"),
7880
)
7981

8082
if FLAGS.verbose:
8183
logging.getLogger("werkzeug").setLevel(logging.INFO)
8284
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()
9186
logging.getLogger("werkzeug").setLevel(logging.WARN)
9287

9388
flask_app.run(host="0.0.0.0", port=port(), use_reloader=False)

mesop/server/colab_run.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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()

mesop/server/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
EDITOR_PACKAGE_PATH = "mesop/mesop/web/src/app/editor/web_package"
2+
PROD_PACKAGE_PATH = "mesop/mesop/web/src/app/prod/web_package"

mesop/server/logging.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
)

mesop/utils/caller.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import mesop.protos.ui_pb2 as pb
44

55

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:
79
current_frame = inspect.currentframe()
810

911
# Walk backwards
@@ -20,7 +22,11 @@ def get_caller_source_code_location(levels: int = 1) -> pb.SourceCodeLocation:
2022
caller_info = inspect.getframeinfo(current_frame)
2123

2224
# 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("/")
2430
segments[len(segments) - 1] = segments[len(segments) - 1][: len(".py") * -1]
2531
module = ".".join(segments)
2632

0 commit comments

Comments
 (0)