+# Workaround for
+# https://github.com/matplotlib/matplotlib/issues/13723. Despite the
+# issue is closed, the problem is still there. See
+# https://github.com/matplotlib/matplotlib/issues/13723#issuecomment-761302131.
+plot_lock = threading.Lock()
+
+# Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
+# updated to our needs
+def humanize_ts(time, max_interval="years"):
+ """Convert date/time in ISO format to relative, human readable string.
+
+ Example return values: 'an hour ago', 'Yesterday', '3 months ago',
+ 'just now', etc.
+
+ When optional max_interval is set to "days", the return value will
+ report at most the number of days ago, not week, months or years.
+ """
+ if jinja2.is_undefined(time):
+ return time
+ if max_interval not in ["years", "days"]:
+ raise ValueError
+
+ diff = datetime.now(timezone.utc) - time
+ second_diff = diff.seconds
+ day_diff = diff.days
+
+ if day_diff < 0:
+ return 'in the future'
+
+ if day_diff == 0:
+ if second_diff < 10:
+ return "just now"
+ if second_diff < 60:
+ return str(int(second_diff)) + " seconds ago"
+ if second_diff < 120:
+ return "a minute ago"
+ if second_diff < 3600:
+ return str(int(second_diff / 60)) + " minutes ago"
+ if second_diff < 7200:
+ return "an hour ago"
+ if second_diff < 86400:
+ return str(int(second_diff / 3600)) + " hours ago"
+ if day_diff == 1:
+ return "Yesterday"
+ if day_diff < 7 or max_interval == "days":
+ return str(day_diff) + " days ago"
+ if day_diff < 31:
+ return str(int(day_diff / 7)) + " weeks ago"
+ if day_diff < 365:
+ return str(int(day_diff / 30)) + " months ago"
+ return str(int(day_diff / 365)) + " years ago"
+
+
+app.jinja_env.filters['humanize'] = humanize_ts
+
+
+def days_filter(time):
+ """Return the number of days elapsed since time."""
+ if jinja2.is_undefined(time):
+ return time
+ diff = datetime.now(timezone.utc) - time
+ return diff.days
+
+
+app.jinja_env.filters['days'] = days_filter
+