Simpler times

Flask and Freeze

Well, now that I have two posts is time to refactor. A static site is simple, but it will give me a lot of work as the number of posts grows. I think I will want to have only some post listed on the home page, have some kind of categories or organization by date… the idea is to have at least 52 posts at the end of the year so those utilities will be useful.

In simpler times, just after the use of static sites as blogs, people used simple applications to manage their blogs. Nowadays those applications had evolved into monsters like Wordpress. I wanted something more simple and built by me: after all, the idea is that this blog to be a place for my own learning, and installing something built by another people won’t bring me very much.

I chose Flask as the framework to build such an application: I’m learning Python so that will be the language for the app. There are more complicated frameworks like Django, but Flask is less opinionated and it will allow me to be more flexible with my code. I’ll code more, it’s sure, but maybe I can apply Domain Driven Design concepts, testing, etc.

After choosing Flask I had a (great and original) idea: what if, after writing a post, my app generated the entire site in Html and that was what would be uploaded to the server? I only wanted a static site, perhaps with the ability to write comments. This approach would be enough for my blog and It would be easier to deploy.

But after googling about it I realized two things: it was a good approach but it was not at all original. There are lots of frameworks and libraries to help with that. A static site has strong advantages: speed, security and ease of deployment, so it's a path which has been explored by developers in the pasts three or four years.

I think I’ll use a mixed approach. There is a library, flask-freeze, which takes a Flask application and generates a static site. That will be enough until I include comments; then I plan to use a mini-server which manages comments and regenerates the post page as needed.

By now I have a basic Flask app running, serving the posts and generating a static site from it. The posts are fixed: there is no database at the moment.The code for creating a Flask application is very simple:


from flask import Flask, render_template

def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)

@app.route('/')
def root():
    return render_template('/index.html')

return app

Routes work with annotations. I didn't like annotations very much: they obscure the code because you must look in another place to discover what the annotation is doing. But later I learnt some functional programming and discovered that annotations are higher-order functions. and, if used with care (only a couple of annotations with well-defined meaning across and app) can be useful.

On the other hand, I do not like the use of annotations to define the routes: I believe that the routes are part of the application configuration and should be together in some file so they are easy to visualize and modify. Flask allows to define the routes through app.add_url_rule(), but for now, I will use the decorators until the application has a more complexity.

The only route for the app will render a Jinja2 template. I have a template for the site scaffold and another one, inheriting from it, for the index. I've also included a custom font for the site and a code formatter.

Next steps: a post’s database and the thing I fear more: introducing TDD. But that will be next week.