Chartjs-Limit data in Chart.js with Flask/SQLAlchemy in python

0👍

Your suggestion is close. You want to filter the reading query for the sensor you’re on & order the query by time.

If you do something like this in your route:

@app.route('/sensorlog')
def sensorlog():
    sensors = Sensor.query.order_by(Sensor.id.asc()).all()
    if sensors > 0:
        readings = {}
        for sensor in sensors:
            readings[sensor.id] = session.query(
                Sensor.readings
            ).filter_by(
                id=sensor.id
            ).order_by(
                time.desc()
            ).limit(30)
        return render_template('sensorlog.html', sensors=sensors, readings=readings)
    else:
        msg = 'No Sensors Found'
        return render_template('sensorlog.html', msg=msg)

You should now be sending an object of the results for each sensor as well as the sensor information.

Then in your jinja template you should only have to change the javascript where you’re calling buildChart() so that the labels, humidity & temperature arguments access the readings[sensor.id].

Hope that makes sense, it was a bit hard to get through without having access to the data or app myself.

Leave a comment