File size: 1,277 Bytes
36724cf
 
 
ccea67f
 
 
 
 
 
 
 
 
 
 
36724cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccea67f
 
 
36724cf
 
ccea67f
36724cf
 
 
 
 
 
 
 
 
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
import typing as t
from collections import OrderedDict
from marimo import create_asgi_app, nav_menu
from pathlib import Path

from .log import logger

NB_FOLDER = Path(__file__).parent.parent / "nb"


logger.info(f"NB_FOLDER: {NB_FOLDER}")


class MarimoApps:
    def __init__(self, folder: Path = NB_FOLDER):
        self.folder = folder
        self.files: t.Dict[str, Path] = {}
        for file in self.folder.glob("prod_*.py"):
            logger.debug(f"Mounting Marimo app from: {file}")
            endpoint = "/" + file.stem.split("_")[-1]
            path = file.absolute()
            self.files[endpoint] = path

    @property
    def pages(self) -> OrderedDict[str, str]:
        _pages = OrderedDict({"/home": "Home"})
        for endpoint in self.files:
            page_name = endpoint[1:].capitalize()
            _pages[endpoint] = page_name
        return _pages

    def build(self):
        apps = create_asgi_app(include_code=True)
        for endpoint, path in self.files.items():
            apps = apps.with_app(path=endpoint, root=path)
        return apps.build()


marimo_apps = MarimoApps()
# print(marimo_apps.pages)


class UI:
    TITLE = "Marimo on FastAPI"
    NAV = nav_menu(marimo_apps.pages, orientation="vertical").style(font_size="20px")