by Audrey M. Roy Greenfeld | Sat, Jan 25, 2025
Here I clean up the code, back-integrate the manual fixes I've since made, and reduce the steps it takes to export.
#| default_exp main
#| export from datetime import datetime from execnb.nbio import read_nb from nb2fasthtml.core import render_code_output from fasthtml.common import * from fasthtml.jupyter import * from importlib.metadata import distributions from IPython.display import display, HTML from monsterui import franken from monsterui.all import Theme import mistletoe import pygments from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter
IN_NOTEBOOK
#| export app,rt = fast_app(pico=False)
server = JupyUvi(app)
#| export def get_nb_paths(): if IN_NOTEBOOK: return L(sorted(Path().glob("*.ipynb"), reverse=True)) return L(sorted(Path("nbs/").glob("*.ipynb"), reverse=True))
nb_paths = get_nb_paths() nb_paths
#| export def get_title_and_desc(fpath): nb = read_nb(fpath) title = nb.cells[0].source.lstrip("# ") desc = nb.cells[1].source return title,desc
get_title_and_desc(nb_paths[0])
#| export def get_date_from_iso8601_prefix(fname): "Gets date from first 10 chars YYYY-MM-DD of `fname`, where `fname` is like `2025-01-12-Get-Date-From-This.whatever" try: return datetime.fromisoformat(str(fname)[0:10]) except ValueError: return None
date = get_date_from_iso8601_prefix(nb_paths[0].name) date
#| export def NBCard(title,desc,href,date): return A( franken.Card( franken.CardTitle(franken.H3(title)), franken.PSmall(f"{date:%a, %b %-d, %Y}", cls="uk-text-muted"), franken.P(desc), body_cls='space-y-2' ), href=href)
#| export def mk_nbcard_from_nb_path(nb_path): date = get_date_from_iso8601_prefix(nb_path.name) or datetime.now() return NBCard(*get_title_and_desc(nb_path), href=f'/nbs/{nb_path.name[:-6]}', date=date)
#| export @rt def index(): nb_paths = get_nb_paths() return ( Theme.blue.headers(), Title("audrey.feldroy.com"), franken.Container( Div( franken.H1('audrey.feldroy.com'), franken.PParagraph("The experimental Jupyter notebooks of Audrey M. Roy Greenfeld. This website and all its notebooks are open-source at ", franken.A("github.com/audreyfeldroy/arg-blog-fasthtml", href="https://github.com/audreyfeldroy/arg-blog-fasthtml"), cls="mb-6"), ), franken.Grid(*nb_paths.map(mk_nbcard_from_nb_path), cols_sm=1, cols_md=1, cols_lg=2, cols_xl=3) ) )
#| export def StyledCode(c, style='monokai'): fm = HtmlFormatter(style=style, cssclass=style) h = highlight(c, PythonLexer(), fm) sd = fm.get_style_defs(f".{style}") return Div(Style(sd), NotStr(h), id=style)
#| export def StyledMd(m): return Safe(mistletoe.markdown(m))
#| export def StyledCell(c): if c.cell_type == "markdown": return StyledMd(c.source) if c.cell_type == "code": if not c.outputs: return StyledCode(c.source) return StyledCode(c.source), render_code_output(c)
#| export @rt("/nbs/{name}") def notebook(name:str): fname = f"{name}.ipynb" if IN_NOTEBOOK else f"nbs/{name}.ipynb" fpath = Path(fname) nb = read_nb(fpath) title = nb.cells[0].source.lstrip("# ") desc = nb.cells[1].source if "MonsterUI" in title: return ( Theme.slate.headers(), Title(title), franken.Container( franken.H1(title), # Title franken.P(desc), # Desc *L(nb.cells[2:]).map(StyledCell), cls="space-y-5" ) ) return ( Style(':root {font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif; color-scheme: light dark;} body {background-color: light-dark(#ffffff, #1a1a1a); color: light-dark(#1a1a1a, #ffffff);} p {line-height: 1.5;}'), Title(title), Div( H1(title), # Title P(desc), # Desc *L(nb.cells[2:]).map(StyledCell), cls="space-y-5" ) )
@rt def versions(): dists = L([NS(name=dist.metadata['Name'], version=dist.version) for dist in distributions()]).sorted('name') dists = [Li(f'{d.name}: {d.version}') for d in dists] return (Title('Python Package Versions'), Style(':root {font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif; color-scheme: light dark;} body {background-color: light-dark(#ffffff, #1a1a1a); color: light-dark(#1a1a1a, #ffffff);} p {line-height: 1.5;}'), Div( H1('Python Package Versions'), Ul(*dists) ) )
@rt('/.well-known/{fname}') def wellknown(fname: str): fpath = f"../.well-known/{fname}" if IN_NOTEBOOK else f".well-known/{fname}" return Path(fpath).read_text()
#| export serve()
server.stop()
To export this notebook as arg-blog-fasthtml's main.py:
nb_export nbs/2025-01-25-This-Site-Is-Now-Powered-by-This-Notebook-Part-2.ipynb --lib_path .