44from utils import *
55from importlib import import_module
66from monsterui .all import *
7+ from starlette .responses import HTMLResponse
78
89def get_route (p ): return '/' .join (Path (p ).parts [1 :])
910def 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
2636def 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}' )
3848def 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}' )
5164def 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}' )
5874def 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
6382def ImageCard (dir_path ):
0 commit comments