Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions monsterui/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
22 changes: 19 additions & 3 deletions monsterui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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("""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
25 changes: 20 additions & 5 deletions nbs/01_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
{
Expand Down