-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
662 lines (604 loc) · 24.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import collections, functools, pathlib, json, csv
from datetime import datetime
from dateutil import parser
from importlib.metadata import distributions
from os import getenv
import pytz
import yaml
from fasthtml.common import *
from nb2fasthtml.core import (
render_nb, read_nb, get_frontmatter_raw,render_md,
strip_list
)
default_social_image = '/public/images/profile.jpg'
# This redirects dict is a relic of previous blog migrations.
# It is used to redirect old URLs embedded in books, presentations,
# and more to the new locations.
redirects = json.loads(pathlib.Path(f"redirects.json").read_text())
def MermaidJS(
sel='.language-mermaid', # CSS selector for mermaid elements
theme='base', # Mermaid theme to use
delay=500 # Delay in milliseconds before rendering
):
"Implements browser-based Mermaid diagram rendering."
return Script(src='/mermaid.js', type='module')
hdrs = (
MarkdownJS(),
HighlightJS(langs=['python', 'javascript', 'html', 'css',]),
Link(rel='stylesheet', href='/public/style.css', type='text/css'),
MermaidJS()
)
class ContentNotFound(Exception): pass
def render_code_output(cell,lang='python', render_md=render_md):
res = []
if len(cell['outputs'])==0: ''
for output in cell['outputs']:
print(output['output_type'])
if output['output_type'] == 'execute_result':
data = output['data']
if 'text/markdown' in data.keys():
res.append(NotStr(''.join(strip_list(data['text/markdown'][1:-1]))))
elif 'text/plain' in data.keys():
res.append(''.join(strip_list(data['text/plain'])))
if output['output_type'] == 'stream':
res.append(''.join(strip_list(output['text'])))
if res: return render_md(*res, container=Pre)
# The following functions are three content loading. They are cached in
# memory to boost the speed of the site. In production at a minumum the
# app is restarted every time the project is deployed.
@functools.cache
def list_posts(published: bool = True, posts_dirname="posts", content=False) -> list[dict]:
"""
Loads all the posts and their frontmatter.
Note: Could use pathlib better
"""
posts: list[dict] = []
# Fetch notebooks
for post in pathlib.Path('.').glob(f"{posts_dirname}/**/*.ipynb"):
if '.ipynb_checkpoints' in str(post): continue
nb = read_nb(post)
data: dict = get_frontmatter_raw(nb.cells[0])
data["slug"] = post.stem
data['cls'] = 'notebook'
if content:
data["content"] = render_nb(post,
cls='',
fm_fn=lambda x: '',
out_fn=render_code_output
)
posts.append(data)
# Fetch markdown
for post in pathlib.Path('.').glob(f"{posts_dirname}/**/*.md"):
raw: str = post.read_text().split("---")[1]
data: dict = yaml.safe_load(raw)
data["slug"] = post.stem
data['cls'] = 'marked'
if content:
data["content"] = '\n'.join(post.read_text().split("---")[2:])
posts.append(data)
posts.sort(key=lambda x: x["date"], reverse=True)
return [x for x in filter(lambda x: x["published"] is published, posts)]
@functools.lru_cache
def get_post(slug: str) -> tuple|None:
posts = list_posts(content=True)
post = next((x for x in posts if x["slug"] == slug), None)
if post is None: raise ContentNotFound
return (post['content'], post)
@functools.cache
def list_tags() -> dict[str, int]:
unsorted_tags = {}
for post in list_posts():
page_tags = post.get("tags", [])
for tag in page_tags:
if tag in unsorted_tags:
unsorted_tags[tag] += 1
else:
unsorted_tags[tag] = 1
tags: dict = collections.OrderedDict(
sorted(unsorted_tags.items(), key=lambda x: x[1], reverse=True)
)
return tags
# The next block of code is several date utilities
# We need these because I've been sloppy about defining dates
# TODO: Fix datetimes in all markdown files so this wouldn't be necessary
def convert_dtstr_to_dt(date_str: str) -> datetime:
"""
Convert a naive or non-naive date/datetime string to a datetime object.
Naive datetime strings are assumed to be in GMT (UTC) timezone.
"""
try:
dt = parser.parse(date_str)
if dt.tzinfo is None:
# If the datetime object is naive, set it to GMT (UTC)
dt = dt.replace(tzinfo=pytz.UTC)
return dt
except (ValueError, TypeError) as e:
return None
def format_datetime(dt: datetime) -> str:
"""Format the datetime object"""
if dt is None: return ''
formatted_date = dt.strftime("%B %d, %Y")
formatted_time = dt.strftime("%I:%M%p").lstrip('0').lower()
return f"{formatted_date} at {formatted_time}"
# Most pages use a number of reusable components to
# render out HTML. I follow the FT Component standard
# of TitleCasing these, even though they are functions.
# That makes them easier to identify as FT Components.
def Layout(title, socials, *tags):
"""Generic layout for pages"""
return title, socials, (
Header(
A(Img(
cls='borderCircle', alt='Daniel Roy Greenfeld', src='/public/images/profile.jpg', width='108', height='108')
, href='/'),
A(H2('Daniel Roy Greenfeld'), href="/"),
P(
A('About', href='/about'),' | ',
A('Articles', href='/posts'), ' | ',
A('Books', href='/books'), ' | ',
A('Jobs', href='/jobs'), ' | ',
A('Tags', href='/tags'), ' | ',
A('Search', href='/search')
), style="text-align: center;"
),
Main(*tags, cls='container'),
Footer(Hr(), P(
A('LinkedIn', href='https://www.linkedin.com/in/danielfeldroy/'), ' | ',
A('Bluesky', href='https://bsky.app/profile/daniel.feldroy.com'), ' | ',
A('Twitter', href='https://twitter.com/pydanny'), ' | ',
'Feeds: ', A('All', href='/feeds/atom.xml'), NotStr(', ') ,A('Python', href='/feeds/python.atom.xml'), NotStr(', ') , A('TIL', href='/feeds/til.atom.xml')
),
P(f'All rights reserved {datetime.now().year}, Daniel Roy Greenfeld'),
cls='container'
),
Div(
Div(
H2('Search'),
Input(name='q', type='text', id='search-input', hx_trigger="keyup", placeholder='Enter your search query...', hx_get='/search-results', hx_target='.search-results-modal'),
Div(cls='search-results-modal'),
cls='modal-content'
),
id='search-modal',
style='display:none;',
cls='modal overflow-auto'
),
Div(hx_trigger="keyup[key=='/'] from:body"),
Script("""
//document.documentElement.setAttribute('data-theme', 'light');
document.body.addEventListener('keydown', e => {
if (e.key === '/') {
e.preventDefault();
document.getElementById('search-modal').style.display = 'block';
document.getElementById('search-input').focus();
}
if (e.key === 'Escape') {
document.getElementById('search-modal').style.display = 'none';
}
});
document.getElementById('search-input').addEventListener('input', e => {
htmx.trigger('.search-results', 'htmx:trigger', {value: e.target.value});
});
""")
)
def BlogPostPreview(title: str, slug: str, timestamp: str, description: str):
"""
This renders a blog posts short display used for the index, article list, and tags.
"""
return Span(
H2(A(title, href=f"/posts/{slug}")),
P(description, Br(), Small(Time(format_datetime(convert_dtstr_to_dt(timestamp)))))
)
def TILPreview(title: str, slug: str, timestamp: str, description: str):
return Span(
H3(A(title[4:], href=f"/posts/{slug}")),
P(Small(Time(format_datetime(convert_dtstr_to_dt(timestamp)))))
)
def TagLink(slug: str):
return Span(A(slug, href=f"/tags/{slug}"), " ")
def TagLinkWithCount(slug: str, count: int):
return Span(A(Span(slug), Small(f" ({count})"), href=f"/tags/{slug}"), " ")
def MarkdownPage(slug: str):
"""Renders a non-sequential markdown file"""
try:
text = pathlib.Path(f"pages/{slug}.md").read_text()
except FileNotFoundError:
return Page404()
content = ''.join(text.split("---")[2:])
metadata = yaml.safe_load(text.split("---")[1])
date = metadata.get('date', '')
return (Title(metadata.get('title', slug)),
Socials(site_name="https://daniel.feldroy.com",
title=metadata.get('title', slug),
description=metadata.get('description', 'slug'),
url=f"https://daniel.feldroy.com/{slug}",
image=metadata.get("image", default_social_image),
),
Section(
H1(metadata.get('title', '')),
P(metadata.get('author', ''), Br(), Small(Time(format_datetime(convert_dtstr_to_dt(date))))),
Div(content,cls="marked")
)
)
### Going forwards everything is mostly a view
def Page404():
"""404 view"""
return FtResponse(Layout(Title("404 Not Found"),
Socials(site_name="https://daniel.feldroy.com",
title="Daniel Roy Greenfeld",
description="Daniel Roy Greenfeld's personal blog",
url="https://daniel.feldroy.com",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
H1("404 Not Found"), P("The page you are looking for does not exist.")),
status_code=404
)
def not_found():
return Page404()
exception_handlers = {
404: not_found
}
app, rt = fast_app(hdrs=hdrs, debug=False, exception_handlers=exception_handlers)
@rt
def index():
all_posts = list_posts()
posts = [BlogPostPreview(title=x["title"],slug=x["slug"],timestamp=x["date"],description=x.get("description", "")) for x in all_posts if 'TIL' not in x.get('tags')]
popular = [BlogPostPreview(title=x["title"],slug=x["slug"],timestamp=x["date"],description=x.get("description", "")) for x in all_posts if x.get("popular", False)]
tils = [TILPreview(title=x["title"],slug=x["slug"],timestamp=x["date"],description='') for x in all_posts if 'TIL' in x.get('tags')]
return Layout(
Title("Daniel Roy Greenfeld"),
Socials(site_name="https://daniel.feldroy.com",
title="Daniel Roy Greenfeld",
description="Daniel Roy Greenfeld's personal blog",
url="https://daniel.feldroy.com",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Div(cls='grid')(
Section(
H1('Recent Writings'),
*posts[:4],
P(A('Read all articles', href=posts))
),
Section(
H1('TIL', Small(' (Today I learned)')),
*tils[:7],
P(A('Read more TIL articles', href='/tags/TIL'))
),
Section(
H1('Popular Writings'),
*popular
),
)
)
@rt
def posts():
duration = round((datetime.now() - datetime(2005, 9, 3)).days / 365.25, 2)
description = f'Everything written by Daniel Roy Greenfeld for the past {duration} years.'
posts = [BlogPostPreview(title=x["title"],slug=x["slug"],timestamp=x["date"],description=x.get("description", "")) for x in list_posts()]
return Layout(
Title("All posts by Daniel Roy Greenfeld"),
Socials(site_name="https://daniel.feldroy.com",
title="All posts by Daniel Roy Greenfeld",
description=description,
url="https://daniel.feldroy.com/posts/",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Section(
H1(f'All Articles ({len(posts)})'),
P(description),
*posts,
A("← Back to home", href="/"),
))
@rt("/posts/{slug}")
def get(slug: str):
try:
content, metadata = get_post(slug)
except ContentNotFound:
return Page404()
tags = [TagLink(slug=x) for x in metadata.get("tags", [])]
specials = ()
if 'TIL' in metadata['tags']:
specials = (A(
Img(src="/public/logos/til-1.png", alt="Today I Learned", width="200", height="200", cls="center"),
href="/tags/TIL")
)
return Layout(
Title(metadata['title']),
Socials(site_name="https://daniel.feldroy.com",
title=metadata["title"],
description=metadata.get("description", ""),
url=f"https://daniel.feldroy.com/posts/{slug}",
image="https://daniel.feldroy.com" + metadata.get("image", default_social_image),
),
Section(
H1(metadata["title"]),
P(I(metadata.get('description', ''))),
P(Small(Time(format_datetime(convert_dtstr_to_dt(metadata['date']))))),
Div(content,cls=metadata['cls']),
Div(style="width: 200px; margin: auto; display: block;")(*specials),
P(Span("Tags: "), *tags),
A("← Back to all articles", href="/"),
),
)
@rt("/tags")
def get():
tags = [TagLinkWithCount(slug=x[0], count=x[1]) for x in list_tags().items()]
return Layout(Title("Tags"),
Socials(site_name="https://daniel.feldroy.com",
title="Tags",
description="All tags used in the site.",
url="https://daniel.feldroy.com/tags/",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Section(
H1('Tags'),
P('All tags used in the blog'),
*tags,
Br(), Br(),
A("← Back home", href="/"),
)
)
@rt("/tags/{slug}")
def get(slug: str):
posts = [BlogPostPreview(title=x["title"],slug=x["slug"],timestamp=x["date"],description=x.get("description", "")) for x in list_posts() if slug in x.get("tags", [])]
return Layout(Title(f"Tag: {slug}"),
Socials(site_name="https://daniel.feldroy.com",
title=f"Tag: {slug}",
description=f'Posts tagged with "{slug}" ({len(posts)})',
url=f"https://daniel.feldroy.com/tags/{slug}",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Section(
H1(f'Posts tagged with "{slug}" ({len(posts)})'),
*posts,
A("← Back home", href="/"),
)
)
def _search(q: str=''):
def _s(obj: dict, name: str, q: str):
content = obj.get(name, "")
if isinstance(content, list):
content = " ".join(content)
return q.lower().strip() in content.lower().strip()
messages = []
posts = []
description = f"No results found for '{q}'"
if q.strip():
posts = [BlogPostPreview(title=x["title"],slug=x["slug"],timestamp=x["date"],description=x.get("description", "")) for x in list_posts() if
any(_s(x, name, q) for name in ["title", "description", "content", "tags"])]
if posts:
messages = [H2(f"Search results on '{q}'"), P(f"Found {len(posts)} entries")]
description = f"Search results on '{q}'"
elif q.strip():
messages = [P(f"No results found for '{q}'")]
return Div(
Meta(property='description', content=description),
Meta(property='og:description', content=description),
Meta(name='twitter:description', content=description),
*messages,
*posts
)
@rt("/search")
def get(q: str|None = None):
result = []
if q is not None:
result.append(_search(q))
return Layout(Title("Search"),
Socials(site_name="https://daniel.feldroy.com",
title="Search the site",
description='',
url="https://daniel.feldroy.com/search",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Form(cls='center', role='group')(
Input(name="q", id='q', value=q, type="search", autofocus=True),
Button("Search", hx_get="/search-results", hx_target='.search-results', hx_include='#q', onclick="updateQinURL()")
),
Section(
Div(cls='search-results')(*result),
P(Small('Hint: Use the "/" shortcut to search from any page.')),
A("← Back home", href="/"),
),
Script("""function updateQinURL() {
let url = new URL(window.location);
const value = document.getElementById('q').value
url.searchParams.set('q', value);
window.history.pushState({}, '', url);
};
"""),
)
@rt('/search-results')
def get(q: str):
return _search(q)
@rt
def fitness():
with open('public/fitness-2024.csv') as f:
rows = [o for o in csv.DictReader(f)]
dates = collections.defaultdict(list)
for row in rows: dates[row['Date'][:7]].append(row)
config = {'responsive': True}
config = json.dumps(config)
charts = []
for month, rows in dates.items():
fitness = [{
'type': 'bar',
'x': [o['Date'] for o in rows],
'y': [o['Weight'] for o in rows],
'text': [o['Weight'] for o in rows],
'textposition': 'auto',
'hoverinfo': 'none',
'marker': {'color': 'blue',},
'name': 'Weight kg'
},
{
'type': 'bar',
'x': [o['Date'] for o in rows],
'y': [o['BJJ'] for o in rows],
'text': [o['BJJ'] for o in rows],
'textposition': 'auto',
'hoverinfo': 'none',
'marker': {'color': 'green',},
'name': 'BJJ'
},
{
'type': 'bar',
'x': [o['Date'] for o in rows],
'y': [o['Other'] for o in rows],
'text': [o['Other'] for o in rows],
'textposition': 'auto',
'hoverinfo': 'none',
'marker': {'color': 'red',},
'name': 'Strength'
}
]
layout = {
'title': {
'text': datetime.strptime(month, '%Y-%m').strftime("%B %Y")
},
'font': {'size': 18},
'barcornerradius': 15,
}
layout = json.dumps(layout)
chart_name = f'weightChart-{month}'
charts.insert(0, Div(id=chart_name))
charts.insert(1, Script(f"Plotly.newPlot('{chart_name}', {fitness}, {layout}, {config});"))
current_weight = rows[-1]['Weight']
return Layout(
Title('Fitness Tracking'),
Socials(site_name="https://daniel.feldroy.com",
title=f"Fitness Tracking",
description='My fitness journey over time.',
url=f"https://daniel.feldroy.com/fitness",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Script(src="https://cdn.plot.ly/plotly-2.32.0.min.js"),
Section(
P(
'Wt Goal: ', Strong('77 kg / 169 lbs'), Br(),
f'Current: {current_weight} kg / {round(float(current_weight) * 2.2, 2)} lb'
),
H2(f'Fitness Tracking'),
Ol(
Li('Weight kg is how much I weight in kilograms.'),
Li('BJJ is how many minutes of Brazilian Jiu-Jitsu in a day.'),
Li('Strength is how many minutes of strength training in a day, most often weights or HIIT, somes alternative exercise like Yoga or Pilates.'),
),
*charts,
A("← Back home", href="/"),
)
)
@rt('/writing-stats')
def writing_stats():
years = collections.defaultdict(int)
for post in list_posts(): years[post['date'][:4]] += 1
data = [
{
'x': list(map(int, years.keys())),
'y': list(map(int, years.values())),
'type': 'scatter'
}
]
# data = json.dumps(data)
config = {'responsive': True}
config = json.dumps(config)
layout = {
'title': {
'text': 'Writing stats'
},
'font': {'size': 18},
'barcornerradius': 15,
}
layout = json.dumps(layout)
return Layout(
Title('Writing Stats'),
Socials(site_name="https://daniel.feldroy.com",
title=f"Writing Stats",
description='Numbers about my writing patterns',
url=f"https://daniel.feldroy.com/fitness",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Script(src="https://cdn.plot.ly/plotly-2.32.0.min.js"),
Div(id='post-counts'),
Script(f"Plotly.newPlot('post-counts', {data}, {layout}, {config});"),
# P(f'{years}'),
# P(f'{data}')
)
@rt
def versions():
dists = L([NS(name=dist.metadata['Name'], version=dist.version) for dist in distributions()])
dists = sorted(dists, key=lambda x: x.name.lower())
dists = [Li(f'{d.name}: {d.version}') for d in dists]
return Layout(
Title('Package Versions'),
Socials(site_name="https://daniel.feldroy.com",
title=f"Package Versions",
description='Getting Python versions',
url=f"https://daniel.feldroy.com/fitness",
image="https://daniel.feldroy.com/public/images/profile.jpg",
),
Ul(*dists)
)
@rt("/feeds/{fname:path}.{ext}")
def get(fname:str, ext:str):
return FileResponse(f'feeds/{fname}.{ext}')
@rt('/.well-known/atproto-did')
def wellknown_atproto_did():
# for bluesky
return getenv('BLUESKY_ATPROTO', 'Nothing here!')
reg_re_param("static", "ico|gif|jpg|jpeg|webm|css|js|woff|png|svg|mp4|webp|ttf|otf|eot|woff2|txt")
@rt("/{slug}.html")
def get(slug: str):
url = redirects.get(slug, None) or redirects.get(slug + ".html", None)
if url is not None:
return Redirect(loc=url)
return Page404()
@rt("/{slug}.ipynb")
def get(slug: str):
try:
nb = render_nb(f'nbs/{slug}.ipynb', wrapper=Div, cls='', fm_fn=None)
except:
return Page404()
return Layout(
Title("Demo JupyterA"),
Socials(site_name="https://daniel.feldroy.com",
title="Demo Jupyter",
description='Demo Jupyter',
url=f"https://daniel.feldroy.com/{slug}.ipynb",
image=default_social_image,
),
nb
)
@rt
def stories():
stories = Path('pages/stories/').glob('*.md')
links = []
for story in stories:
story = str(story)[5:-3]
links.append(
Li(A(href=story)(story))
)
return Layout(
Title('Stories'),
Socials(site_name="https://daniel.feldroy.com",
title="Stories",
description='Stories',
url=f"https://daniel.feldroy.com/stories",
image=default_social_image,
),
*Ul(*links)
)
@rt("/{slug}")
def get(slug: str):
redirects_url = redirects.get(slug, None)
if redirects_url is not None:
return Redirect(loc=redirects_url)
try:
return Layout(*MarkdownPage(slug))
except TypeError:
return Page404()
@rt("/{slug_1}/{slug_2}")
def get(slug_1: str, slug_2: str):
try:
return Layout(*MarkdownPage(slug_1 + "/" + slug_2))
except TypeError:
return Page404()
serve(reload_includes="*.md,*.ipynb,*.css")