Recently I’ve been working on a Flask app that analyze the likes you sent on Twitter and get a list of users in the order of the number of likes you sent to them. You can try it here.

I wrote this project purely for the purpose of learning Flask, and in this post I am gonna talk about my first experience in it and some problems I ran into.

BTW, the idea of the app is from @xierch, credit goes to him :)

Simple

Before this project, I already know the basic front-end development skills and I am familiar with Python. It is my very first experience in back-end development, and I don’t really want to touch the more industrial standard back-end tools since that requires much more time. I am already familiar with Python so I decided it’s the way to go. I did try learning Django before but I gave up since there’s much to learn and I don’t have the time.

In contrast, Flask is very simple to learn and get started. As someone without any back-end knowledge, I spent less than an hour to read this tutorial by TutorialsPoint to get the basic ideas of Flask and then I just start developing.

Powerful

The feature I like most in Flask is that you can add Python logics right inside the HTML file. Here’s an example:

1
2
3
4
5
6
@app.route('/like')
def like():
# ...
# Other stuff
# ...
return render_template('like.html', ary = sort, username = session['username'], limit = limit)

The above function in Python handle requests sent to ‘/like’ and render a page to show a list of users you sent most likes to.

sort is an list of tuples in form of (username, likes_sent) and has been sorted according to the value of likes_sent, session['username'] is the Twitter user name of the current authorized user, and limit is the number of likes analyzed.

And here’s the like.html render by the function as a template:

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
{% raw %}
{% extends 'common.html' %}
{% block body %}
<div class="block">
<p style="font-size: 1.3em">Likes Sent by @{{ username }}</p>
<br>
<p>out of {{ limit }} most recent likes</p>
<br>
{% if dict %}
<div id="like-container">
{% for (user, count) in dict[0:15] %}
<div class="like-line">
<a href="{{ 'https://twitter.com/' + user.replace('@', '') }}">{{user}}</a>
<p>{{count}}</p>
</div>
{% endfor %}
</div>
{% endif %}
<br>
<div class="btn-line">
<a href="{{ url_for('share') }}">Share to Twitter</a>
<a href="{{ url_for('full') }}">Get Full Result</a>
</div>
</div>
{% endblock %}
{% endraw %}

As you can see, the above HTML code extends the <body> tag of another common.html, which define the header, footer and style of the whole site. common.html has an empty <body> so any pages can dynamically add HTML into its body. This dramatically reduce the code duplication and save development time.

Things inside {{ }} in the template HTML code will be executed as Python commands, which enables you to handle the parameters passed to the template to dynamically generate the web page.

Problems I Ran Into

Some problems that get me stuck as well as my solutions are recorded here in case someone else got the got the same problems, as well as for my future reference.

1. PIL import error

I got this error when my image generation script was trying to create an image of the most liked users. These problems are not necessarily Flask related, but I will still include them in this post so I can easily find them when needed.

ImportError: The _imagingft C module is not installed

I simply googled the error message and found this Stack Overflow answer that helps.

Basically the PIL(Pillow) library uses the _imagingft C module in its ImageFont.py, which is used to set the font used in the ImageDraw.Draw() function to draw text on image, and I got this error because my system didn’t have this module installed. Installed libfreetype first and reinstall Pillow solve the problem for me.

1
2
3
sudo aptitude install libfreetype6-dev
sudo pip uninstall Pillow
sudo pip install Pillow

2. Permission Issue

After deploy the app on an Apache server, everything works fine except that when the app tries to save the created image, an IOError raises:

IOError: [Errno 13] Permission denied

The solution is straight forward: just give the www-date group the permission to write in the directory.

1
2
sudo chown -R www-data:www-data /path/to/directory
sudo chmod -R 775 /path/to/directory