Part 2: Flask and MongoDB Project. Simple Reddit Reader.

I finally got around to adding some additional functionality to my Simple Reddit Reader. I wanted to be able to grab reddits from specific sub-reddits. I also want to be able to filter the database by clicking on one of my sub-reddit categories and pass that into the query on the collection.

First, I add a dictionary of my favorite sub-reddits. My key value pairs contain the proper setence case name of the sub-reddit and the value holds the actual url-safe sub-reddit value.

subreddits = {  
    'Frontpage': 'FrontPage',
    'Explain Like I\'m Five': 'explainlikeimfive'

Next, I add the route for the sub-reddit filter to

@app.route('/reddit/<subreddit>', methods=['GET'])
def reddit_filter(subreddit):  
    filter_result = mongo.db.reddit.find({'subreddit': subreddit}).sort([('created', -1)])
    return render_template(

Now, when I click on a link in my sidebar, I am routed to the sub-reddit dict value and the collection is filtered to show only those stories in the sub-reddit in which I am filtering.

Finally, I add the HTML file for my sub-reddits.

{% extends "layout.html" %}

{% block title %}
  My Reddit Reader | Subreddit Filter
{% endblock %}

{% block body %}

    <h2><i class="fa fa-reddit-square"></i> {{ reddit_cat.title() }} Reddits
        <a style="margin-left:5px;" href="#menu-toggle" class="btn btn-default" id="menu-toggle"><i class="fa fa-toggle-left"></i> Toggle Menu</a>
        <span class="pull-right" style="font-size:14px;"><i class="fa fa-check-circle"></i> Currently Showing {{ filter_result.count() }} reddits</span>

    {% for item in filter_result %}
        <div class="well" style="padding:10px;margin-bottom:5px;margin-top:10px;">
            <h4>{{ item.title }}</h4>
            <h5>subreddit:/r/{{ item.subreddit }} <span style="margin-left:10px;">domain: {{ item.domain }}</span>
                <span class="pull-right">
                    <a href="{{ url_for('reddit_delete', reddit_id=item._id) }}" onclick="return confirm('Are you sure you want to delete this story?');">
                        <i class="fa fa-times-circle fa-3x"></i>
                <h6><small>{{ item.created.strftime('%m-%d-%Y @ %H:%M:%S') }}</small></h6>
                <a href="{{ }}" target="_blank">{{ }}</a>
    {% endfor %}

{% endblock %}

This is helpful when the document collection grows to thousands of documents.


In the above screen shot of the Reddit Reader app, you can see the sub in the sidebar.

Here, you can see the data set filtered by the sub-reddit category.


Later, I am going to add authentication and provide the user the ability to save favorites.

Until then... check the GitHub source for this project.

Craig Derington

Veteran full stack web dev focused on deploying high-performance, responsive, modern web applications using Python, NodeJS, Django, Flask, MongoDB and MySQL.

comments powered by Disqus