Lab 04 - Modular Python & Flask

Objectives

  • Python organization using packages
  • REST-ful APIs
  • Model-View-Controller architectures
  • Flask blueprints
  • CSS practice ;)

Contents

Introduction

Preparation

First, download the lab's archive from here and unzip it somewhere in your projects directory.

This time, you have a Makefile that automatically creates the virtualenv and installs dependencies from requirements.txt

make
# note: you still need to manually activate the virtualenv if you wish to
source .venv/bin/activate
# to start the web server:
make run

Tasks

01. [40p] IoT REST API

First, make sure to explore the lab code and see where's what and everything.

You need to implement a RESTful interface that will allow you to send sensor data from a (imaginary) IoT device. Also check out the TODOs:

  • a GET request to /api/iot should display the current data in JSON format;
  • a POST request to /api/iot should accept a JSON dictionary containing the temperature / humidity (check the model for the keys / format); it should accept partial data (e.g., if sending just { “rain”: 1 } it should keep the old values for all other sensors!);
  • a DELETE request to clean / reset all sensor values to zero!

Also implement the get/set functions inside the sensors model.

To test, either use a browser extension like RESTer (Firefox) or Talend (Chrome) or something else (beware of / check for reviews to avoid installing spyware! POSTMan has gone commercial BTW, ain't open anymore). As an alternative for purists, you can alwayys use ''curl'' from the command line!

02. [30p] Dashboard

Try to login and enter the IoT dashboard using your browser! Huh, isn't working? Well don't just sit there, go on and fix it!

The sensors dashboard is kind of bleak… can you style & colour it a little? here's some suggestions from our designer:

03. [30p] Persistent Database

Make the sensors data persistent across server restarts.

Use json as format and simply store / load the values from a file.

ii/labs/s2/03-mod.txt · Last modified: 2025/04/13 18:40 by florin.stancu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0