Setting up a web server to record health parameters

In this task, we will build a simple web server that can be used to maintain critical health parameters. Let's consider a scenario where a person is diagnosed with high blood pressure. The doctor asks the patient to record his/her vital health parameters such as pulse, oxygen level, or blood pressure. For this situation, we will build a simple web page that records the data and stores it in a CSV file.

Prepare for lift off

We will be using the Flask framework (http://Flask.pocoo.org/) to deploy the web server on Raspberry Pi. We made use of the Flask framework in Project 4, Christmas Light Sequencer. Just in case you skipped through Project 4, Christmas Light Sequencer, a Python package manager such as pip or easy_install is required to install the Flask framework:

sudo apt-get install python-pip

After the installation of the Python package manager is complete, the Flask framework may be installed as follows:

sudo pip install Flask

Engage thrusters

  1. We will modify this basic Flask framework example (http://runnable.com/UhLMQLffO1YSAADK/handle-a-post-request-in-Flask-for-python) to take input from a browser:
    • In the frontend of the web server, let's create textboxes in the form_submit.html file where we can enter the blood pressure, oxygen saturation levels, and pulse data. The landing page will look like the one shown in the following figure (when opened in a browser):
      Engage thrusters

      The form used to record vital health parameters

    • In the Python script that launches the web server, we read the submitted parameters and write them to a CSV file along with a timestamp:
      @app.route('/', methods=['POST'])
      def record():
        #record all the data from the form
        bloodPressure=request.form['BloodPressure']
        SpO2=request.form['SpO2']
        pulse=request.form['pulse']
        #before writing to a csv file, log time stamps
        date = datetime.today().strftime('%Y-%m-%d')
        time = datetime.now().strftime('%H:%M:%S')
        logfile = open('static\comments.csv', 'a')
        logfile.write(",".join([date,time,bloodPressure, SpO2, pulse,'
      ']))
          logfile.close()
      
        return render_template('form_action.html',date=date,time=time,bloodPressure=bloodPressure, SpO2=SpO2,pulse=pulse
    • Once we're done writing to the CSV file, we redirect the submitted form to a page that displays the recorded values along with the timestamp (as shown in the following figure):
    Engage thrusters

    The page displayed after the results are recorded

  2. It is possible to view the recorded data on the web page by reading the CSV file. We leave this for you to figure out (look at this book's website for the answer).

Objective complete – mini debriefing

We built a simple Flask-framework-based web server to record vital health parameters using Raspberry Pi. It is possible to e-mail the data at regular intervals using the smtp module in Python.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.253.62