Skip to content

Commit

Permalink
MESOP_STATIC_FILES_BASE_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed Oct 26, 2024
1 parent 2a0e9e5 commit 7ffd6bf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ By default, this is not enabled. You can enable this by setting it to `true`.

This uses WebSockets instead of HTTP Server-Sent Events (SSE) as the transport protocol for UI updates. If you set this environment variable to `true`, then [`MESOP_CONCURRENT_UPDATES_ENABLED`](#MESOP_CONCURRENT_UPDATES_ENABLED) will automatically be enabled as well.

### MESOP_STATIC_FILES_BASE_PATH

This is the base path for serving static files. This is rarely needed because the default of using os.getcwd() is usually sufficient.

## Usage Examples

### One-liner
Expand Down
4 changes: 4 additions & 0 deletions mesop/env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"MESOP_AI_SERVICE_BASE_URL", "http://localhost:43234"
)

MESOP_STATIC_FILES_BASE_PATH = os.environ.get(
"MESOP_STATIC_FILES_BASE_PATH", ""
)

MESOP_WEBSOCKETS_ENABLED = (
os.environ.get("MESOP_WEBSOCKETS_ENABLED", "false").lower() == "true"
)
Expand Down
3 changes: 3 additions & 0 deletions mesop/labs/web_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import wraps
from typing import Any, Callable, TypeVar, cast

from mesop.env.env import MESOP_STATIC_FILES_BASE_PATH
from mesop.runtime import runtime
from mesop.utils.validate import validate

Expand Down Expand Up @@ -52,6 +53,8 @@ def format_filename(filename: str) -> str:
if ".runfiles" in filename:
# Handle Bazel case
return filename.split(".runfiles", 1)[1]
elif MESOP_STATIC_FILES_BASE_PATH:
return os.path.relpath(filename, MESOP_STATIC_FILES_BASE_PATH)
else:
# Handle pip CLI case
return os.path.relpath(filename, os.getcwd())
4 changes: 4 additions & 0 deletions mesop/server/static_file_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from flask import Flask, Response, g, make_response, request, send_file
from werkzeug.security import safe_join

from mesop.env.env import MESOP_STATIC_FILES_BASE_PATH
from mesop.exceptions import MesopException
from mesop.runtime import runtime
from mesop.server.constants import WEB_COMPONENTS_PATH_SEGMENT
Expand Down Expand Up @@ -99,6 +100,9 @@ def serve_web_components(path: str):
else safe_join(os.getcwd(), path)
)

if MESOP_STATIC_FILES_BASE_PATH:
serving_path = safe_join(MESOP_STATIC_FILES_BASE_PATH, path)

file_name = os.path.basename(path)
file_extension = os.path.splitext(file_name)[1].lower()
allowed_extensions = {".js", ".css"}
Expand Down
2 changes: 1 addition & 1 deletion mesop/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Contains the version string."""

VERSION = "0.12.7"
VERSION = "0.12.8beta1"

if __name__ == "__main__":
print(VERSION)

0 comments on commit 7ffd6bf

Please sign in to comment.