Skip to content

Instantly share code, notes, and snippets.

@capjamesg
Created January 30, 2022 19:40
Show Gist options
  • Save capjamesg/169abb4798b1847be615312f05ac2194 to your computer and use it in GitHub Desktop.
Save capjamesg/169abb4798b1847be615312f05ac2194 to your computer and use it in GitHub Desktop.
# generate sparklines
collections_to_build_sparklines_for = ["posts", "likes", "notes", "bookmarks"]
sparklines = ""
for c in collections_to_build_sparklines_for:
dates = {}
for i in range(0, 90):
dates[(datetime.datetime.now() - datetime.timedelta(days=i)).strftime("%Y-%m-%d")] = 0
for post in site_config[c]:
if c == "posts":
date = post["date"].strftime("%Y-%m-%d")
else:
date = datetime.datetime.strptime(post["full_date"], "%Y-%m-%d %H:%M:%S-00:00").strftime("%Y-%m-%d")
if dates.get(date) is not None:
dates[date] += 1
values = dates.values()
# convert values to list
data_points = list(values)
data_points.reverse()
number_of_posts = len(site_config[c])
sparklines += f"""
<p><a href="/{c}/">{number_of_posts} {c.title()}</a> <embed class="light_mode" src="/assets/sparkline.svg?{','.join([str(val) for val in data_points])}"
class="sparkline" width=100 height=15 /><embed class="dark_mode" src="/assets/sparkline_dark_mode.svg?{','.join([str(val) for val in data_points])}"
class="sparkline" width=100 height=15 /></p>"""
@kinduff
Copy link

kinduff commented Jan 30, 2022

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment