Skip to content

Commit f77915a

Browse files
committed
Update main.py
1 parent 296d5eb commit f77915a

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

main.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from utils import *
55
from importlib import import_module
66
from monsterui.all import *
7+
from starlette.responses import HTMLResponse
78

89
def get_route(p): return '/'.join(Path(p).parts[1:])
910
def get_module_path(p,base_dir): return f'{base_dir}.{".".join(Path(p).parts[1:])}.app'
@@ -21,7 +22,16 @@ def get_module_path(p,base_dir): return f'{base_dir}.{".".join(Path(p).parts[1:]
2122
*Theme.blue.headers(highlightjs=True),
2223
Link(rel='icon', type='image/x-ico', href="/files/gallery.ico"))
2324

24-
app = FastHTML(routes=application_routes+ [Mount('/files', StaticFiles(directory='.')),], hdrs=hdrs, pico=False)
25+
def HTML_404_PAGE():
26+
return Container(
27+
H1("404 Not Found"),
28+
P("The page you are looking for does not exist."),
29+
A("Back to Gallery", href="/", cls=AT.primary))
30+
31+
app = FastHTML(
32+
exception_handlers={404: HTML_404_PAGE},
33+
routes=application_routes+ [Mount('/files', StaticFiles(directory='.')),], hdrs=hdrs, pico=False)
34+
2535

2636
def GalleryNavBar(dir_path, info=True, active=''):
2737
nav_items = [
@@ -36,9 +46,12 @@ def GalleryNavBar(dir_path, info=True, active=''):
3646

3747
@app.get('/split/{category}/{project}')
3848
def split_view(category: str, project: str):
39-
dir_path = Path('examples')/category/project
40-
code_text = (dir_path/'app.py').read_text().strip()
41-
info = (dir_path/'info.md').exists()
49+
try:
50+
dir_path = Path('examples')/category/project
51+
code_text = (dir_path/'app.py').read_text().strip()
52+
info = (dir_path/'info.md').exists()
53+
except:
54+
return Response(status_code=404)
4255
return Container(
4356
GalleryNavBar(dir_path, info=info, active='split'),
4457

@@ -49,15 +62,21 @@ def split_view(category: str, project: str):
4962

5063
@app.get('/code/{category}/{project}')
5164
def application_code(category:str, project:str):
52-
dir_path = Path('examples')/category/project
53-
code_text = (dir_path/'app.py').read_text().strip()
54-
info = (dir_path/'info.md').exists()
65+
try:
66+
dir_path = Path('examples')/category/project
67+
code_text = (dir_path/'app.py').read_text().strip()
68+
info = (dir_path/'info.md').exists()
69+
except:
70+
return Response(status_code=404)
5571
return Container(GalleryNavBar(dir_path, info=info, active='code'), Title(f"{dir_path.name} - Code"), Container(Pre(Code(code_text, cls='language-python'))))
5672

5773
@app.get('/info/{category}/{project}')
5874
def application_info(category:str, project:str):
59-
dir_path = Path('examples')/category/project
60-
md_text = (dir_path/'info.md').read_text()
75+
try:
76+
dir_path = Path('examples')/category/project
77+
md_text = (dir_path/'info.md').read_text()
78+
except:
79+
return Response(status_code=404)
6180
return Container(GalleryNavBar(dir_path, info=True, active='info'), Title(f"{dir_path.name} - Info"), Container(render_md(md_text)))
6281

6382
def ImageCard(dir_path):

0 commit comments

Comments
 (0)