-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.py
175 lines (152 loc) · 7.66 KB
/
main.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from fasthtml.common import *
import configparser, os
from pathlib import Path
from utils import *
from importlib import import_module
from monsterui.all import *
from starlette.responses import HTMLResponse
def get_route(p): return '/'.join(Path(p).parts[1:])
def get_module_path(p,base_dir): return f'{base_dir}.{".".join(Path(p).parts[1:])}.app'
application_routes = [Mount(f"/app/{get_route(root)}", import_module(get_module_path(root,'examples')).app) for root, dirs, files in os.walk('examples') if 'app.py' in files]
site_title = 'FastHTML Gallery'
descr = 'A gallery of FastHTML components showing common patterns in FastHTML apps, including chat bubbles, cascading dropdowns, interactive charts, etc.'
ghub_link = A(UkIcon("github"), title="FastHTML Gallery on Github",
href="https://github.com/AnswerDotAI/FastHTML-Gallery"),
hdrs = (
*Socials(title=site_title, description=descr, site_name='gallery.fastht.ml', twitter_site='@isaac_flath', image=f'/social.png', url=''),
toggle_script,
*Theme.blue.headers(highlightjs=True),
Link(rel='icon', type='image/x-ico', href="/files/gallery.ico"))
def HTML_404_PAGE():
return Container(
H1("404 Not Found"),
P("The page you are looking for does not exist."),
A("Back to Gallery", href="/", cls=AT.primary))
app = FastHTML(
exception_handlers={404: HTML_404_PAGE},
routes=application_routes+ [Mount('/files', StaticFiles(directory='.')),], hdrs=hdrs, pico=False)
def GalleryNavBar(dir_path, info=True, active=''):
nav_items = [
A("Back to Gallery", href="/"),
A("Split", href=f"/split/{dir_path.parts[1]}/{dir_path.parts[2]}", cls='uk-active' if active == 'split' else ''),
A("Code", href=f"/code/{dir_path.parts[1]}/{dir_path.parts[2]}", cls='uk-active' if active == 'code' else ''),
A("App", href=f"/app/{dir_path.parts[1]}/{dir_path.parts[2]}", cls='uk-active' if active == 'app' else '')]
if info: nav_items.insert(1, A("Info", href=f"/info/{dir_path.parts[1]}/{dir_path.parts[2]}", cls='uk-active' if active == 'info' else ''))
return NavBar(*nav_items, brand=H1(f"{dir_path.name.replace('_',' ').title()}"), cls='mb-6')
@app.get('/split/{category}/{project}')
def split_view(category: str, project: str):
try:
dir_path = Path('examples')/category/project
code_text = (dir_path/'app.py').read_text().strip()
info = (dir_path/'info.md').exists()
except:
return Response(status_code=404)
return Container(
GalleryNavBar(dir_path, info=info, active='split'),
Title(f"{dir_path.name} - Split View"),
Grid(Div(Pre(Code(code_text, cls='language-python'))),
Div(Iframe(src=f"/app/{category}/{project}/",style="width: 100%; height: 100%; border: none;")),
cols_sm=1, cols_md=1, cols_lg=2))
@app.get('/code/{category}/{project}')
def application_code(category:str, project:str):
try:
dir_path = Path('examples')/category/project
code_text = (dir_path/'app.py').read_text().strip()
info = (dir_path/'info.md').exists()
except:
return Response(status_code=404)
return Container(GalleryNavBar(dir_path, info=info, active='code'), Title(f"{dir_path.name} - Code"), Container(Pre(Code(code_text, cls='language-python'))))
@app.get('/info/{category}/{project}')
def application_info(category:str, project:str):
try:
dir_path = Path('examples')/category/project
md_text = (dir_path/'info.md').read_text()
except:
return Response(status_code=404)
return Container(GalleryNavBar(dir_path, info=True, active='info'), Title(f"{dir_path.name} - Info"), Container(render_md(md_text)))
def ImageCard(dir_path):
metadata = configparser.ConfigParser()
metadata.read(dir_path/'metadata.ini')
meta = metadata['REQUIRED']
dpath = dir_path.parts[1]+'/'+dir_path.parts[2]
text_md_exists = (dir_path/'info.md').exists()
return Card(
A(Img(
src=f"{'/files'/dir_path/'card_thumbnail.gif'}", alt=meta['ImageAltText'],
style="width: 100%; height: 350px; object-fit: cover;",
data_png=f"{'/files'/dir_path/'card_thumbnail.png'}",
loading="lazy",
cls="card-img-top"),
href=f"/split/{dpath}"),
Div(P(meta['ComponentName'], cls=(TextT.bold, TextT.lg)),
render_md(P(meta['ComponentDescription'])),#, cls=(TextT.muted, TextT.lg)),
style="height: 150px; overflow: auto;"),
footer=DivFullySpaced(
A(Button("Split", cls=ButtonT.primary), href=f"/split/{dpath}"),
A(Button("Code", cls=ButtonT.secondary), href=f"/code/{dpath}"),
A(Button("App", ), href=f"/app/{dpath}"),
A(Button("Info"), href=f"/info/{dpath}") if text_md_exists else None))
directories = [Path(f"examples/{x}") for x in [
'dynamic_user_interface_(htmx)',
'visualizations',
'widgets',
'svg',
'todo_series',
'applications']]
def is_example_dir(d):
return d.is_dir() and not d.name.startswith('_') and (d/'metadata.ini').exists()
@app.get("/")
def homepage():
all_cards = []
for section in directories:
all_cards.append(
Section(Details(
Summary(H1(section.name.replace('_',' ').title(), cls='mt-6 mb-4 pb-2 text-center text-3xl font-bold border-b-2 border-gray-300')),
Grid(*[ImageCard(dir) for dir in sorted(section.iterdir()) if is_example_dir(dir)],
cols_min=1, cols_sm=1, cols_md=2, cols_lg=3, cols_xl=3),
cls='pt-6', open=True)))
return Container(NavBar(
# Right side items
Button("Toggle Animations", onclick="toggleAnimations()", cls=ButtonT.ghost),
A("Table View", href="/table"),
# Brand/title on left
brand=DivLAligned(H1("FastHTML Gallery"), UkIcon('rocket',height=30,width=30))),
Container(*all_cards))
def TableRow(dir_path):
metadata = configparser.ConfigParser()
metadata.read(dir_path/'metadata.ini')
meta = metadata['REQUIRED']
dpath = dir_path.parts[1]+'/'+dir_path.parts[2]
text_md_exists = (dir_path/'info.md').exists()
return Tr(
Td(meta['ComponentName']),
Td(render_md(meta['ComponentDescription'])),
Td(DivLAligned(
A(Button("Split", cls=ButtonT.primary), href=f"/split/{dpath}"),
A(Button("Code", cls=ButtonT.secondary), href=f"/code/{dpath}"),
A(Button("App"), href=f"/app/{dpath}"),
A(Button("Info"), href=f"/info/{dpath}") if text_md_exists else None),
cls='uk-table-shrink'))
def SectionTable(section):
section_id = f"section-{section.name}"
return Section(Details(
Summary(H1(section.name.replace('_',' ').title(),
cls='mt-6 mb-4 pb-2 text-center text-3xl font-bold border-b-2 border-gray-300 cursor-pointer')),
Table(
Thead(Tr(map(Th, ("Component", "Description", "Actions")))),
Tbody(*[TableRow(dir) for dir in sorted(section.iterdir())
if is_example_dir(dir)]),
cls=(TableT.middle, TableT.divider, TableT.hover, TableT.sm)),
# open=True,
id=section_id),
cls='py-2')
@app.get("/table")
def table_view():
return Container(NavBar(
# Right side items
Button("Toggle Animations", onclick="toggleAnimations()", cls=ButtonT.ghost),
A("Card View", href="/"),
# Brand/title on left
brand=DivLAligned(H1("FastHTML Gallery Table View"), UkIcon('rocket',height=30,width=30))),
Container(*[SectionTable(section) for section in directories]))
serve()