From 3e47135b4e8240bfc460769a6fbca5d7f66e7bc8 Mon Sep 17 00:00:00 2001 From: Robert Spencer Date: Tue, 18 Nov 2025 19:39:48 +0800 Subject: [PATCH] add served_headers() to support GUI app use --- monsterui/_modidx.py | 2 ++ monsterui/core.py | 22 +++++++++++++++++++--- nbs/01_core.ipynb | 25 ++++++++++++++++++++----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/monsterui/_modidx.py b/monsterui/_modidx.py index 5260dc6..7fb56d9 100644 --- a/monsterui/_modidx.py +++ b/monsterui/_modidx.py @@ -13,11 +13,13 @@ 'monsterui/core.py'), 'monsterui.core.Theme.headers': ('core.html#theme.headers', 'monsterui/core.py'), 'monsterui.core.Theme.local_headers': ('core.html#theme.local_headers', 'monsterui/core.py'), + 'monsterui.core.Theme.served_headers': ('core.html#theme.served_headers', 'monsterui/core.py'), 'monsterui.core.ThemeFont': ('core.html#themefont', 'monsterui/core.py'), 'monsterui.core.ThemeRadii': ('core.html#themeradii', 'monsterui/core.py'), 'monsterui.core.ThemeShadows': ('core.html#themeshadows', 'monsterui/core.py'), 'monsterui.core._download_resource': ('core.html#_download_resource', 'monsterui/core.py'), 'monsterui.core._headers_theme': ('core.html#_headers_theme', 'monsterui/core.py'), + 'monsterui.core._resource_filename': ('core.html#_resource_filename', 'monsterui/core.py'), 'monsterui.core.fast_app': ('core.html#fast_app', 'monsterui/core.py')}, 'monsterui.daisy': { 'monsterui.daisy.Alert': ('daisy.html#alert', 'monsterui/daisy.py'), 'monsterui.daisy.AlertT': ('daisy.html#alertt', 'monsterui/daisy.py'), diff --git a/monsterui/core.py b/monsterui/core.py index 71fb9b7..911372a 100644 --- a/monsterui/core.py +++ b/monsterui/core.py @@ -12,6 +12,7 @@ from fastcore.all import * import httpx from pathlib import Path +import re # %% ../nbs/01_core.ipynb @delegates(fh.fast_app, but=['pico']) @@ -98,12 +99,19 @@ def _headers_theme(color, mode='auto', radii=ThemeRadii.sm, shadows=ThemeShadows } def _download_resource(url, static_dir): - "Download a single resource and return its local path" + "Download a single resource and return its name" static = Path(static_dir) - fname = static/f"{url[0]}.{'js' if 'js' in url[1] else 'css'}" + fname = static / _resource_filename(url) content = httpx.get(url[1], follow_redirects=True).content fname.write_bytes(content) - return (url[0], f"/{static_dir}/{fname.name}") + return (url[0], fname.name) + +def _resource_filename(url): + "Return a filename for the resource" + if url[0] == 'tailwind': + return 'tailwind.js' + return f"{url[0]}.{'js' if re.search(r'\bjs\b', url[1]) else 'css'}" + # %% ../nbs/01_core.ipynb daisy_styles = Style(""" @@ -167,6 +175,9 @@ def _download_resource(url, static_dir): ''') # %% ../nbs/01_core.ipynb +from typing import Any + + class Theme(Enum): "Selector to choose theme and get all headers needed for app. Includes frankenui + tailwind + daisyui + highlight.js options" def _generate_next_value_(name, start, count, last_values): return name @@ -262,3 +273,8 @@ def local_headers(self, mode='auto', static_dir='static', icons=True, daisy=True Path(static_dir).mkdir(exist_ok=True) local_urls = dict([_download_resource(url, static_dir) for url in HEADER_URLS.items()]) return self._create_headers(local_urls, mode=mode, icons=icons, daisy=daisy, highlightjs=highlightjs, katex=katex, apex_charts=apex_charts, radii=radii, shadows=shadows, font=font) + + def served_headers(self, mode='auto', serve_from='/static', icons=True, daisy=True, highlightjs=False, katex=False, apex_charts=False, radii='md', shadows='sm', font='sm'): + "Create headers with url or route to static files (previously downloaded)" + urls = dict([(url[0], f"{serve_from}/{_resource_filename(url)}") for url in HEADER_URLS.items()]) + return self._create_headers(urls, mode=mode, icons=icons, daisy=daisy, highlightjs=highlightjs, katex=katex, apex_charts=apex_charts, radii=radii, shadows=shadows, font=font) diff --git a/nbs/01_core.ipynb b/nbs/01_core.ipynb index 6f6f614..2895db3 100644 --- a/nbs/01_core.ipynb +++ b/nbs/01_core.ipynb @@ -38,7 +38,8 @@ "from enum import Enum, auto\n", "from fastcore.all import *\n", "import httpx\n", - "from pathlib import Path" + "from pathlib import Path\n", + "import re" ] }, { @@ -229,12 +230,18 @@ "}\n", "\n", "def _download_resource(url, static_dir):\n", - " \"Download a single resource and return its local path\"\n", + " \"Download a single resource and return its name\"\n", " static = Path(static_dir)\n", - " fname = static/f\"{url[0]}.{'js' if 'js' in url[1] else 'css'}\"\n", + " fname = static / _resource_filename(url)\n", " content = httpx.get(url[1], follow_redirects=True).content\n", " fname.write_bytes(content)\n", - " return (url[0], f\"/{static_dir}/{fname.name}\")" + " return (url[0], fname.name)\n", + "\n", + "def _resource_filename(url):\n", + " \"Return a filename for the resource\"\n", + " if url[0] == 'tailwind':\n", + " return 'tailwind.js'\n", + " return f\"{url[0]}.{'js' if re.search(r'\\bjs\\b', url[1]) else 'css'}\"\n" ] }, { @@ -318,6 +325,9 @@ "outputs": [], "source": [ "#| export\n", + "from typing import Any\n", + "\n", + "\n", "class Theme(Enum):\n", " \"Selector to choose theme and get all headers needed for app. Includes frankenui + tailwind + daisyui + highlight.js options\"\n", " def _generate_next_value_(name, start, count, last_values): return name\n", @@ -412,7 +422,12 @@ " \"Create headers using local files downloaded from CDNs\"\n", " Path(static_dir).mkdir(exist_ok=True)\n", " local_urls = dict([_download_resource(url, static_dir) for url in HEADER_URLS.items()])\n", - " return self._create_headers(local_urls, mode=mode, icons=icons, daisy=daisy, highlightjs=highlightjs, katex=katex, apex_charts=apex_charts, radii=radii, shadows=shadows, font=font)" + " return self._create_headers(local_urls, mode=mode, icons=icons, daisy=daisy, highlightjs=highlightjs, katex=katex, apex_charts=apex_charts, radii=radii, shadows=shadows, font=font)\n", + "\n", + " def served_headers(self, mode='auto', serve_from='/static', icons=True, daisy=True, highlightjs=False, katex=False, apex_charts=False, radii='md', shadows='sm', font='sm'):\n", + " \"Create headers with url or route to static files (previously downloaded)\"\n", + " urls = dict([(url[0], f\"{serve_from}/{_resource_filename(url)}\") for url in HEADER_URLS.items()])\n", + " return self._create_headers(urls, mode=mode, icons=icons, daisy=daisy, highlightjs=highlightjs, katex=katex, apex_charts=apex_charts, radii=radii, shadows=shadows, font=font)" ] }, {