This website's 5th gen release

This website went through another transformation. It began as a page on Blogger, then went to WordPress, to Jekyll (post in PT), to my custom engine in Clojure, and now in my custom engine written in Python. I'm rewriting it again because I'm using more Python than Clojure these days. Also, things started to break with the new releases of JVM.

Reduced page weight

Pages are slimmer now, partly by removing images I don't use anymore and by removing Google Analytics from the website. This website has too little traffic and no ads and does not cater to a particular audience. Which made me question why having the analytics in the first place. Moreover, I use a browser extension to block site trackers, so why do I have Google tracking on my website?

An excellent side effect of making the pages lighter is that they produce less carbon. According to the Website Carbon Calculator, the home page went from 0.06g of CO2 to 0.01g of CO2 per visit. It still feels like a lot, but it is better than 99% tested by the tool. I know that this is not an impressive achievement for a small website, but what caught my attention was that I could lower the emissions by a factor of 6.

Will the code for this blog engine be open-sourced?

That is a good question. Unfortunately, I'm not considering releasing the code. Not because it has any secret sauces in it, just because it is not in a releasable form. Since I'm a beginner in Python, the code is a bit messy, and it has some hardcode stuff I'm not planning to extract into configurations. Maybe in the future, if my Python improves, I can release it.

There’s one thing I can share, though. Writing code in Python is delightful. There is elegance and power in its simplicity. Listed down is part of the code I use to start a local HTTP server and rebuild the site if I change any file.

def preview():
    build_site()
    with PreviewServer.serve(4000) as httpd:
        try:
            log.info("starting file watcher")
            observer = Observer()
            observer.schedule(DirWatch(), str(RESOURCES_DIR), recursive=True)
            observer.schedule(DirWatch(), str(BLOG_ROOT_DIR.joinpath('layouts')), recursive=True)
            observer.start()

            log.info("serving at localhost:4000")
            httpd.serve_forever()
        except KeyboardInterrupt:
            log.info("interrupted")
            observer.stop()
            httpd.server_close()


class DirWatch(FileSystemEventHandler):
    """watch directory for changes, rebuild site if detects one"""
    INTERVAL = 2  # interval in seconds for a rebuild
    TRIGGERED = time.time()  # track time between builds

    def on_modified(self, event):
        # several events are triggered for each change,
        # we ignore them for INTERVAL seconds
        if int(time.time() - self.TRIGGERED) > self.INTERVAL:
            log.info('rebuilding...')
            try:
                build_site()
            # do not stop because of an error
            # just wait for the next file change
            except BaseException as e:
                traceback.print_exception(e)
            self.TRIGGERED = time.time()
        else:
            pass

One external library is in use here, watchdog, and the rest is out-of-the-box. There’s not much happening here; we serve our pages from our build directory and watch for changes in the sources’ directories. If a change is detected, then we rebuild the site.

Conclusion

I’m taking the opportunity of revamping this website and learn more Python, which is a delight to write. You should try it too.

XKCD Python

Published in Sep 11, 2022
Contact: