forked from marimo-team/marimo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
108 lines (82 loc) · 2.77 KB
/
main.py
File metadata and controls
108 lines (82 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "fastapi",
# "marimo",
# "starlette",
# "requests",
# "pydantic",
# "jinja2",
# ]
# ///
import tempfile
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import marimo
import os
import logging
import requests
from pathlib import Path
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Constants
GITHUB_REPO = os.environ.get("GITHUB_REPO", "marimo-team/marimo")
ROOT_DIR = os.environ.get("ROOT_DIR", "examples/ui")
templates_dir = os.path.join(os.path.dirname(__file__), "templates")
# Set up templates
templates = Jinja2Templates(directory=templates_dir)
def download_github_files(repo: str, path: str = "") -> list[tuple[str, str]]:
"""Download files from GitHub repo, returns list of (file_path, content)"""
api_url = f"https://api.github.com/repos/{repo}/contents/{path}"
response = requests.get(api_url)
response.raise_for_status()
files: list[tuple[str, str]] = []
for item in response.json():
print(item)
if item["type"] == "file" and item["name"].endswith(".py"):
content_response = requests.get(item["download_url"])
files.append(
(os.path.join(path, item["name"]), content_response.text)
)
elif item["type"] == "dir":
files.extend(
download_github_files(repo, os.path.join(path, item["name"]))
)
return files
tmp_dir = tempfile.TemporaryDirectory()
def setup_apps():
"""Download and setup marimo apps from GitHub"""
files = download_github_files(GITHUB_REPO, ROOT_DIR)
server = marimo.create_asgi_app()
app_names: list[str] = []
for file_path, content in files:
app_name = Path(file_path).stem
local_path = Path(tmp_dir.name) / file_path
# Create directories if they don't exist
local_path.parent.mkdir(parents=True, exist_ok=True)
# Write file content
local_path.write_text(content)
# Add to marimo server
server = server.with_app(path=f"/{app_name}", root=str(local_path))
app_names.append(app_name)
logger.info(f"Added app: {app_name} from {file_path}")
return server, app_names
# Create a FastAPI app
app = FastAPI()
# Setup marimo apps
server, app_names = setup_apps()
@app.get("/")
async def home(request: Request):
return templates.TemplateResponse(
"home.html", {"request": request, "app_names": app_names}
)
@app.get("/ping")
async def root():
return {"message": "pong"}
# Mount the marimo server
app.mount("/", server.build())
# Run the server
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000, log_level="info")