Scoping SQLAlchemy's Session while using Tornado.gen

Note: This article is not about using SQLAlchemy and Tornado to do asynchronous calls to your database. It is about making sure each request uses the correct session even if it does async calls to other services.

Session management with SQLAlchemy and Tornado is a little bit tricky if you are doing asynchronous calls. You have to be careful to make sure your request is using the same session before and after an async call. The recommended way to do this is to associate a session with the request itself, but this is not feasible if you are using declarative_base. I got around this issue by making my own scoping function and patching tornado. 

The scoping function is extremely simple. The get method it defines is used by scoped_session to determine the correct session object to provide from its registry. The handlers and patches will use to the set method to set the correct session scope.

We create a base handler that sets the correct scope using the scope object created in database.py.

Finally, we do a little bit of monkey patching so the correct scope will be maintained before and after async calls using tornado.gen

With all this in place, we can safely have declarative base models and commit sessions whenever we want.

The $100 Blogging Challenge

I really want to write more blog posts, but I have never been able to prioritize writing over my other tasks. Blogging is beneficial for many reasons. It teaches you how to communicate your ideas better and it helps you build your personal brand. For software developers like myself, blogging allows us to talk about cool behind the scenes technology that we are building. These benefits, however, are not obvious to see. After writing a blog post, you cannot tangibly see your personal brand increase. It takes quite a bit of writing before you start to improve your writing. On the other hand, writing a few more lines of code or working on a side project has instant results.

My buddy, Dave Zohrob, and I wanted to see if we could get ourselves to blog more. So we started a blogging challenge a couple weeks ago. The rules are simple - you must write a blog post every week by 5 pm on Wednesday. If you fail to write a post, you must give $100 to the other person. Although it is a negative motivator, it adds a concrete incentive to write more. First of all there is the monetary part of it. One hundred dollars is enough to make one write. In addition, there is the social aspect. I know that at least one person is expecting me to write a post, so I have much more motivation to write it.

The experiment is going well so far. We both have written posts every week except for Thanksgiving. Admittedly, my posts so far have been extemporaneous and lacked real content, but I think that will change as weekly writing becomes a habit.

A Better Calorie Tracker

I have a love hate relationship with calorie trackers. Over the past few years, I have been using loseIt and myFitnessPal to help me get in better shape. The apps help me make better decisions on which foods to eat to reach my daily calorie, protein, fat, and carbohydrate goals. In fact, without a calorie tracker following my diet plan would be impossible. Even with all of these benefits, I hate using calorie trackers because they make entering food extremely painful and annoying.

Let's take a look at myFitnessPal. When you launch the app you are presented with this:

When I launch a calorie tracker, I am doing so because I either want to add some food or see what I can eat next that will fit within my goals. My daily goals change because I eat more calories and carbs on workout days.

Keeping that in mind, this home screen is almost worthless. Most of it is taken up by a newsfeed with stories that I do not care about. It also has controls to change the date even though that information is useless today. No information about protein, carbs, and fat are shown. These are important because without them it is hard to make a decision on what type of food to eat next.

To get to macros in myFitnessPal, I have to tap some non obvious button and then hit another button. After all that work, I am presented with this view that has too much irrelevant information.

To fix these issues, I would propose a home screen that looks like this (please excuse the shitty wireframe mockups):

I get all the important information that I need at a quick glance. There is no extra cruft and there is a huge button that makes it super easy to input your food.

Next, we go to the actual process of inputting food using myFitnessPal. After tapping add to diary on the home screen you go through the following flow:

Why do I need to input what meal this was a part of? Who cares? The food should just be added to my daily total. After clicking breakfast, I am presented with a search screen. It is not bad, but why do I need to choose different tabs to narrow my search? That all should just be taken care of by the app. Finally, the screen to actually add the food has too much irrelevant data.

Many times when I am on these screens, I am deciding on what I can eat. I might be planning my dinner or deciding what snack would be best. There is no easy way to do any of this with this flow.

The flow would be better this way:

Your current macros and calories would be readily available. This way you can easily see where you currently are in your daily allotment. The search screen would show the standard serving of each food and what its statistics allowing you to make better choices on what to eat. It would display your most recently logged food as well as food you tend to log at the current time on other days. Current totals for each item logged today would show in that item's row.

The add screen is also dramatically simplified. We only show the calories and macro nutrients for each item. To allow you to make better judgements on how much to eat, your new statistics are shown when you change the # of serving or the serving size.

I believe these changes would make a much better user experience. However, one of the biggest competitive advantages calorie tracking apps have right now is the size of their food databases. MyFitnessPal has a huge set of data and users are inputting more data everyday. A better calorie tracker will have to have better UX as well as a robust database of food.