website-mcknight-tech/site_mcknight_tech/__main__.py
Daniel McKnight 3ac9aaed04 Update homepage content
Add path router to allow URL navigation to specific pages
2025-02-25 21:03:16 -08:00

94 lines
No EOL
2.8 KiB
Python
Executable file

# Copyright (C) 2025 Daniel McKnight - All Rights Reserved
from os.path import isfile, join, dirname
from typing import Optional
from fastapi.openapi.models import RequestBody
from nicegui import ui
from fastapi.requests import Request
# TODO: Read from configuration
_tab_to_content = {
"home": "page_content/home.md",
"portfolio": "page_content/portfolio.md",
"resume": "page_content/resume.md",
"bio": "page_content/bio.md"
}
_error_page = "page_content/404.md"
_footer_block = "page_content/footer.html"
@ui.page("/{name}")
def _render_path(name: str):
"""
Parse a path element to render a page
:param name: path element of the request
"""
render_site(name)
def _get_content_from_file(file_name: str) -> str:
"""
Take a file name/path and return its contents
:param file_name: Relative or absolute path to a file
"""
file_name = join(dirname(__file__), file_name)
if not isfile(file_name):
file_name = _error_page
with open(file_name, 'r') as f:
return f.read()
def render_tab(tab_name: str):
"""
Render a simple webpage tab from a single file
:param tab_name: Tab name to render
"""
file_name = _tab_to_content.get(tab_name, _error_page)
with ui.scroll_area().classes('w-full h-screen no-wrap'):
ui.markdown(_get_content_from_file(file_name),
extras=['cuddled-lists', 'fenced-code-blocks', 'tables'])
def render_header() -> ui.tab_panels:
"""
Render sticky page header with navigation
"""
tab_names = _tab_to_content.keys()
with ui.tabs().classes('w-full') as tabs:
tab_list = {name: ui.tab(name) for name in tab_names}
with ui.tab_panels(tabs).classes('w-full mx-auto h-screen').style('max-width: 1200px') as header:
for name, t in tab_list.items():
with ui.tab_panel(t):
render_tab(name)
return header
def render_site(page_name: Optional[str] = None):
"""
Renders the site with the requested page active
:param page_name: Page to render. If `None` or invalid, `Home` will be rendered
"""
header = render_header()
if page_name and page_name in _tab_to_content.keys():
header.set_value(page_name)
else:
header.set_value(list(_tab_to_content.keys())[0])
ui.query('.nicegui-content').classes('h-screen no-wrap')
ui.html(_get_content_from_file(_footer_block), tag="footer").classes("rounded-lg shadow-sm m-4 w-full text-center")
ui.add_css("""
.fa {
font-size: 30px;
text-decoration: none;
margin: 10px 5px;
vertical-align: bottom;
}
""")
def main():
render_site()
ui.run(dark=None, reload=False, title="Daniel McKnight")
if __name__ == "__main__":
main()