|
|
import typing as t |
|
|
from collections import OrderedDict |
|
|
from marimo import Html, create_asgi_app, md, 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() |
|
|
|
|
|
def build_nav(self) -> Html: |
|
|
return nav_menu(marimo_apps.pages, orientation="vertical").style( |
|
|
font_size="20px" |
|
|
) |
|
|
|
|
|
def build_details(self) -> Html: |
|
|
from duckdb import __version__ as duckdb_version |
|
|
from fastapi import __version__ as fastapi_version |
|
|
from marimo import __version__ as marimo_version |
|
|
from pandas import __version__ as pandas_version |
|
|
from sys import version as python_version |
|
|
|
|
|
versions = md(f"""/// details | Build details |
|
|
marimo: {marimo_version} |
|
|
FastAPI: {fastapi_version} |
|
|
DuckDB: {duckdb_version} |
|
|
pandas: {pandas_version} |
|
|
python: {python_version} |
|
|
/// |
|
|
""").style(position="fixed", bottom="24px", left="12px", width="100%") |
|
|
|
|
|
return versions |
|
|
|
|
|
|
|
|
marimo_apps = MarimoApps() |
|
|
|
|
|
|
|
|
|
|
|
class UI: |
|
|
TITLE = "Marimo on FastAPI" |
|
|
NAV = marimo_apps.build_nav() |
|
|
BUILD_DETAILS = marimo_apps.build_details() |
|
|
|