This repository was archived by the owner on Jun 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy path__init__.py
More file actions
74 lines (61 loc) · 2.27 KB
/
Copy path__init__.py
File metadata and controls
74 lines (61 loc) · 2.27 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
import typing as _t
import pydantic
from .components import AnyComponent
__version__ = '0.9.0'
__all__ = 'AnyComponent', 'FastUI', 'prebuilt_html'
class FastUI(pydantic.RootModel):
"""
The root component of a FastUI application.
"""
root: list[AnyComponent]
@pydantic.field_validator('root', mode='before')
def coerce_to_list(cls, v):
if isinstance(v, list):
return v
else:
return [v]
_PREBUILT_VERSION = '0.0.26'
_PREBUILT_CDN_URL = f'https://cdn.jsdelivr.net/npm/@pydantic/fastui-prebuilt@{_PREBUILT_VERSION}/dist/assets'
def prebuilt_html(
*,
title: str = '',
api_root_url: str | None = None,
api_path_mode: _t.Literal['append', 'query'] | None = None,
api_path_strip: str | None = None,
) -> str:
"""
Returns a simple HTML page which includes the FastUI react frontend, loaded from https://www.jsdelivr.com/.
Arguments:
title: page title
api_root_url: the root URL of the API backend, which will be used to get data, default is '/api'.
api_path_mode: whether to append the page path to the root API request URL, or use it as a query parameter,
default is 'append'.
api_path_strip: string to remove from the start of the page path before making the API request.
Returns:
HTML string which can be returned by an endpoint to serve the FastUI frontend.
"""
meta_extra = []
if api_root_url is not None:
meta_extra.append(f'<meta name="fastui:APIRootUrl" content="{api_root_url}" />')
if api_path_mode is not None:
meta_extra.append(f'<meta name="fastui:APIPathMode" content="{api_path_mode}" />')
if api_path_strip is not None:
meta_extra.append(f'<meta name="fastui:APIPathStrip" content="{api_path_strip}" />')
meta_extra_str = '\n '.join(meta_extra)
# language=HTML
return f"""\
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{title}</title>
<script type="module" crossorigin src="{_PREBUILT_CDN_URL}/index.js"></script>
<link rel="stylesheet" crossorigin href="{_PREBUILT_CDN_URL}/index.css">
{meta_extra_str}
</head>
<body>
<div id="root"></div>
</body>
</html>
"""