{"id":427,"date":"2017-07-06T15:11:05","date_gmt":"2017-07-06T22:11:05","guid":{"rendered":"http:\/\/www.kenwalger.com\/blog\/?p=427"},"modified":"2017-07-06T15:21:07","modified_gmt":"2017-07-06T22:21:07","slug":"flask-blood-glucose-tracker","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/python\/flask\/flask-blood-glucose-tracker\/","title":{"rendered":"Flask Blood Glucose Tracker"},"content":{"rendered":"<p>My oldest daughter was diagnosed with <a href=\"http:\/\/www.diabetes.org\/diabetes-basics\/type-1\/\">Type 1 Diabetes<\/a> at the age of two. Technology has come a long way in terms of tracking blood sugar levels, but I thought I would start a Python web application\u00a0to do so. I&#8217;ll be using the <a href=\"http:\/\/flask.pocoo.org\/\">Flask<\/a> web framework for the project and, since I&#8217;m not a marketing or product naming genius, will call the project Flask Blood Glucose Tracker. I&#8217;m certainly open to other, more catchy, names.<\/p>\n<p>This is somewhat of a different post for me, in that I&#8217;ll be walking through the generation of an actual application. The application will mostly be a minimal viable product (MVP) though. It should,\u00a0however,\u00a0be a good tutorial on how to integrate some concepts and technologies together. As such, in this first post, I&#8217;ll cover some of the project specifications and features that I&#8217;d like to include and work on implementing them in future posts.<\/p>\n<h3>Application for Diabetes<\/h3>\n<p>Diabetics have to check their blood glucose levels frequently, typically using a <a href=\"https:\/\/www.amazon.com\/gp\/product\/B004AI8O4M\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B004AI8O4M&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=cf3914413b7299234f6d4133081b8366\" rel=\"noopener\">blood sugar monitor<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B004AI8O4M\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/>. These checks involve getting a small portion of blood from a finger (or arm) prick and having the monitor test it. It then returns a measurement of the amount of glucose in the blood as a number of milligrams per deciliter (mG\/dL). These readings then will be the numbers we want to record in our application.<\/p>\n<p>It is also important to know if the reading is taken before or after eating and the time of day the reading was taken. Therefore, the application will need to accommodate for that as well. The readings themselves, along with the time of day and relation to meal time are all factors into the amount of insulin needs to be injected.<\/p>\n<p>Throughout this, and subsequent posts, I will do my best to explain diabetes-specific\u00a0terms in as user-friendly of a way as possible. I am basing much of the numbers that I use, ranges of &#8220;good and bad&#8221;, etc. on managing my daughter&#8217;s diabetes over the last 14 years and the countless doctor appointments I&#8217;ve attended. As a disclaimer, I am not a medical professional. Please check with your physician about specifics with diabetes. There is a <a href=\"https:\/\/www.amazon.com\/gp\/product\/1119090725\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1119090725&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=b8a0d39dc342e1826870aaa1d9cf2981\" rel=\"noopener\">Diabetes For Dummies<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=1119090725\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> book which provides a decent overview as well.<\/p>\n<h3>Flask Blood Glucose Tracker Application Features<\/h3>\n<p>There are already a lot of excellent products on the market for keeping track of one&#8217;s blood sugar levels. With that in mind, this application is going to be fairly simple to start. I would like to build it with growth in mind, however, so building a REST API into the project seems like a good idea. My basic feature list to start with is:<\/p>\n<ul>\n<li>Register for the BGT site, with email confirmation.<\/li>\n<li>Login\/Logout and based on role have different access.\n<ul>\n<li>Patient to access and edit my own data.<\/li>\n<li>Physician to get a list of all current patient records.<\/li>\n<li>Administrator for site maintenance.<\/li>\n<\/ul>\n<\/li>\n<li>Input blood sugar levels with date and time of reading and indication of before or after a meal or snack.<\/li>\n<li>Display the data in a table with averages.<\/li>\n<li>Display the data in a chart or visual format.<\/li>\n<li>Typical <a href=\"http:\/\/www.kenwalger.com\/blog\/nosql\/mongodb-crud-with-python\/\">CRUD operations<\/a> for the data<\/li>\n<li>REST API to expose patient blood sugars in a secure fashion<\/li>\n<li>Data is stored in the cloud for accessibility and ease of database maintenance.<\/li>\n<\/ul>\n<p>For the reporting features highlighting high and low blood sugar levels in the report would be helpful. Since what is &#8220;good&#8221; and &#8220;bad&#8221; can change for each individual, I&#8217;ll include a field for each individual to set that.<\/p>\n<h3>Application Stack<\/h3>\n<p>I have already mentioned that this application will be built with Flask. For the data store, I will use <a href=\"https:\/\/www.mongodb.com\">MongoDB<\/a>. To keep in line with the feature request of storing data in the cloud, MongoDB offers a Database as a Service (DBaaS) called <a href=\"https:\/\/www.mongodb.com\/cloud\/atlas\">Atlas<\/a>.<\/p>\n<p>I haven&#8217;t quite figured out yet where I&#8217;ll ultimately host this application, perhaps Heroku? Or maybe on my own server.<\/p>\n<h3>Application Libraries<\/h3>\n<p>When it comes to libraries, there are a lot of choices. Here&#8217;s what I&#8217;ll be using, which will also be included in a <code>requirements.txt<\/code> file.<\/p>\n<ul>\n<li>Flask version 0.12.2<\/li>\n<li>Flask-Login, 0.4.0<\/li>\n<li>Flask-PyMongo, 0.5.1<\/li>\n<li>Bokeh, 0.12.6<\/li>\n<li>Jinja2, 2.9.6<\/li>\n<li>pandas, 0.20.2<\/li>\n<\/ul>\n<p>There are other libraries that will but used as well, but those are the main ones of interest.<\/p>\n<p>I&#8217;m also more of a fan of <a href=\"http:\/\/foundation.zurb.com\/\">Zurb Foundation<\/a> than <a href=\"http:\/\/getbootstrap.com\/2.3.2\/\">Twitter Bootstrap<\/a>, so I&#8217;ll be using that for styling.<\/p>\n<h3>Pages and Routes<\/h3>\n<h6>Web Pages<\/h6>\n<p>To start with we need a way for a user to log in,\u00a0enter their personal data, enter a new blood sugar record, edit their record, and view their information in both a tabular format and then in a chart format.<\/p>\n<p>We&#8217;ll need the following pages, at least to start.<\/p>\n<ul>\n<li>Index<\/li>\n<li>Registration<\/li>\n<li>Login\/Logout Page<\/li>\n<li>Profile page, login required<\/li>\n<li>Records page (create, read, update, delete), login required<\/li>\n<li>Chart page, login required<\/li>\n<\/ul>\n<p>For a Physician we would want to be able to:<\/p>\n<ul>\n<li>Display all of their patients, login required<\/li>\n<\/ul>\n<p>For an Administrator, we want to be able to<\/p>\n<ul>\n<li>Have the ability to manage users (patients &amp; physicians) but not be able to see patient medical data.<\/li>\n<\/ul>\n<h6>REST API Routes<\/h6>\n<p>From the API I want to expose the ability to securely read and write (<code>GET<\/code> and <code>POST<\/code>) data to a patient&#8217;s record. This will make it easier to, for example, write a mobile application to connect to our data. Or, with the advances in blood sugar monitors, perhaps automatically update our application with readings from a device.<\/p>\n<h3>Document Model<\/h3>\n<p>Since I will be using MongoDB to store data for this application, I&#8217;ll be leveraging the <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/document\/\">document<\/a> model. This offers a lot of flexibility in how data is stored, among other benefits. I would encourage you to read my blog post on the <a href=\"http:\/\/www.kenwalger.com\/blog\/nosql\/document-model\/\">document model<\/a> if you are not familiar with it.<\/p>\n<p>To start with, the basic data we want to capture and model will be as follows:<\/p>\n<figure id=\"attachment_433\" aria-describedby=\"caption-attachment-433\" style=\"width: 600px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/sampleDocument-e1499375778129.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"433\" data-permalink=\"https:\/\/www.kenwalger.com\/blog\/python\/flask\/flask-blood-glucose-tracker\/attachment\/sampledocument\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/sampleDocument-e1499375778129.png?fit=600%2C667&amp;ssl=1\" data-orig-size=\"600,667\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"sampleDocument\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Sample Patient Record document&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/sampleDocument-e1499375778129.png?fit=270%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/sampleDocument-e1499375778129.png?fit=840%2C933&amp;ssl=1\" class=\"size-full wp-image-433\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/sampleDocument-e1499375778129.png?resize=600%2C667\" alt=\"BGT Sample Document\" width=\"600\" height=\"667\" \/><\/a><figcaption id=\"caption-attachment-433\" class=\"wp-caption-text\">Sample Patient Record document<\/figcaption><\/figure>\n<p>In looking at this sample document, the <code>groups<\/code> field will keep track of values such as\u00a0<em>patient<\/em>,\u00a0<em>physician<\/em>, and\u00a0<em>admin<\/em>. I have chosen to implement the <code>postal_code<\/code> and <code>MRN<\/code> values as strings instead of integers to accommodate alpha-numeric values.<\/p>\n<p>Through the course of developing this application, we will see the flexibility of the document model in action. We&#8217;ll see how we can utilize some of the features from MongoDB&#8217;s <a href=\"https:\/\/docs.mongodb.com\/manual\/aggregation\/\">aggregation pipeline<\/a> to handle our data processing as well.<\/p>\n<h3>Wrap Up<\/h3>\n<p>I have outlined a nice project here which will utilize several different bits of programming. In the next few posts then, I&#8217;ll cover how to implement all of these features into an MVP application. I would definitely enjoy receiving feedback, so please leave comments below.<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p><em>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\">@kenwalger<\/a> to get the latest updates on my postings.<\/em><\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F427&#038;t=Flask%20Blood%20Glucose%20Tracker&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F427&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F07%2FfeatureImage-e1499354484819.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=Flask%20Blood%20Glucose%20Tracker\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F427&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F427&#038;title=Flask%20Blood%20Glucose%20Tracker\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F427&#038;title=Flask%20Blood%20Glucose%20Tracker\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Flask%20Blood%20Glucose%20Tracker&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F427\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>My oldest daughter was diagnosed with Type 1 Diabetes at the age of two. Technology has come a long way in terms of tracking blood sugar levels, but I thought I would start a Python web application\u00a0to do so. I&#8217;ll be using the Flask web framework for the project and, since I&#8217;m not a marketing &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/python\/flask\/flask-blood-glucose-tracker\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Flask Blood Glucose Tracker&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":431,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[77,4,7],"tags":[81,82,79,78,80],"yst_prominent_words":[123,754,549,544,541,540,99,552,298,286,555,545,542,553,546,556,543,548,562,547],"class_list":["post-427","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask","category-mongodb","category-python","tag-api","tag-diabetes","tag-pymongo","tag-python","tag-rest","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/featureImage-e1499354484819.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-6T","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=427"}],"version-history":[{"count":5,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/427\/revisions\/434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/431"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=427"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}