Flask and MongoDB Project. A Simple Reddit Reader

I like to read reddit every day.

  • Science
  • Data Is Beautiful
  • Mildly Interesting
  • Aww
  • Today I F*ck!d Up (TIFU)
  • Life Pro Tips (LPT)
  • Old School Cool
  • Explain Like I'm Five

are among some of my favorites sub-reddits. And the front page.

A couple of days ago I was working on a project that uses AWS Lambda automation to call a URL, pass some vars, retrieve some data, decode the raw data into the response, format the document into a simple multi-level dictionary and insert it into a MongoDB backend.

I really like Mongo DB. It's schema-less, you know... Documents can be anything. Everything. And they have the same structure as Python dictionaries.

I was on reddit this morning and started to think about wiring up a simple reddit reader using Flask; because I really hate the reddit website.

Is it still 1995 at over there at reddit.com?

I know it's not about the visual experience, but let's get serious. It's all about the UI. The official reddit app on my Moto X is great. But I'm at a computer for upwards of 8-10 hours a day. I want to simplify the reader experience part.

Which is the reason I believe CSS frameworks such as Bootstrap, Foundation and Material Design have "stormed the web" the past few years. These easy-to-use frameworks have turned developers into designers. They come packed with features and ready to use example layouts. Or just head over to StartBootstrap and get a starter pack.

But that's a topic for another day...

Let's stay on point.

I'm going to assume you already have the mongod service running on your computer and you can connect to the service using your terminal or Robomongo. Port 27017.

Note, if you have not already installed MongoDB, go here and follow these instructions.

Create your project directory and fire up a new virtualenv.

craigderington@python-dev:~/sites$ mkdir reddit_reader  
craigderington@python-dev:~/sites$ cd reddit_reader  
craigderington@python-dev:~/sites/reddit_reader$ virtualenv .env --python=python2.7  
craigderington@python-dev:~/sites/reddit_reader$ source .env/bin/activate  
(.env)craigderington@python-dev:~/sites/reddit_reader$ which python
/home/craigderington/sites/reddit_reader/.env/bin/python
(.env)craigderington@python-dev:~/sites/reddit_reader$ pip install requests, flask, flask-mongo
[ dependencies installed]
The App

While some people like to have clear separation of functionality in Flask apps, I prefer more of a "single page app" approach.

It's pure simplicity.

This is a pretty basic Flask app right now but we have quite a bit of functionality with just a few lines. Of course, there is no authentication yet, but I can get a list of Mongo documents from my reddit collection, display them, refresh the local reddit database by calling a refresh function as well as the ability to delete any articles I want to remove from my local database.

Later, I am going to add a user model, OAuth authentication, and give the user the ability to save favorites and archive articles.

I also want to add a few usability feature to swap sub-reddits (currently defaults to /r/technology), a per page variable, and maybe a sort option. So, for a basic scaffold, this is a pretty good start.

HTML

I'm a long-time Bootstrap guy, so I'm using the simple-sidebar-template from StartBootstrap.

I am using the Flask standard web document structure.

  • layout.html
  • index.html
  • login.html
Screenshot of the simple reddit reader.


Ubuntu 14.04
DELL Vostro
Opera 39.0.22

Simple Reddit Reader by Craig Derington using Flask and MongoDB

Wrapping up.

This looks pretty good for a first draft. I will add some additional functionality and update on another post.

Github source.

Craig Derington

Espousing the virtues of Secular Humanism, Libertarianism, Free and Open Source Software, Linux, Ubuntu, Terminal Multiplexing, Docker, Python, Flask, Django, Go, MySQL, PostgreSQL, MongoDB and Git.

comments powered by Disqus