4
4
from utils import *
5
5
from importlib import import_module
6
6
from monsterui .all import *
7
+ from starlette .responses import HTMLResponse
7
8
8
9
def get_route (p ): return '/' .join (Path (p ).parts [1 :])
9
10
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:]
21
22
* Theme .blue .headers (highlightjs = True ),
22
23
Link (rel = 'icon' , type = 'image/x-ico' , href = "/files/gallery.ico" ))
23
24
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
+
25
35
26
36
def GalleryNavBar (dir_path , info = True , active = '' ):
27
37
nav_items = [
@@ -36,9 +46,12 @@ def GalleryNavBar(dir_path, info=True, active=''):
36
46
37
47
@app .get ('/split/{category}/{project}' )
38
48
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 )
42
55
return Container (
43
56
GalleryNavBar (dir_path , info = info , active = 'split' ),
44
57
@@ -49,15 +62,21 @@ def split_view(category: str, project: str):
49
62
50
63
@app .get ('/code/{category}/{project}' )
51
64
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 )
55
71
return Container (GalleryNavBar (dir_path , info = info , active = 'code' ), Title (f"{ dir_path .name } - Code" ), Container (Pre (Code (code_text , cls = 'language-python' ))))
56
72
57
73
@app .get ('/info/{category}/{project}' )
58
74
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 )
61
80
return Container (GalleryNavBar (dir_path , info = True , active = 'info' ), Title (f"{ dir_path .name } - Info" ), Container (render_md (md_text )))
62
81
63
82
def ImageCard (dir_path ):
0 commit comments