From: Bdale Garbee Date: Sat, 5 Apr 2025 06:18:12 +0000 (-0600) Subject: working skeletal web app using cherrypy and plotly X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=eea1cbba4722ca87e4806b7f6c7abb7a77008727;p=fw%2Fquantimotor working skeletal web app using cherrypy and plotly --- diff --git a/application.py b/application.py index 270ae11..6eb057d 100755 --- a/application.py +++ b/application.py @@ -1,23 +1,21 @@ #!/usr/bin/env python3 # Copyright (C) 2025 Bdale Garbee . GPLv3+ +import os +import cherrypy +import plotly.express as px import gpiod import iio import signal import sys import time -import os -import cherrypy -import plotly.express as px - -ctx = iio.LocalContext() -ctrl = ctx.find_device('ads8688') # configuration for each channel on ADS8688 OFFSET = "0" SCALE = "0.078127104" VOLTAGES = ['voltage0', 'voltage1', 'voltage2', 'voltage3', 'voltage4', 'voltage5', 'voltage6', 'voltage7'] +file_path = os.getcwd() from gpiod.line import Direction, Value def set_line_values(chip_path, line_values): @@ -32,58 +30,61 @@ def set_line_values(chip_path, line_values): ) request.set_values(line_values) -if __name__ == "__main__": - try: - # initialize hardware - set_line_values( - "/dev/gpiochip0", - {25: Value.ACTIVE, # take ADS8688 out of reset - 4: Value.ACTIVE, # indicate 'ready' to LPC - 16: Value.INACTIVE, # pyro off - 17: Value.INACTIVE, # alarm b off - 20: Value.INACTIVE, # turn continuity LED off - 21: Value.INACTIVE, # turn armed LED off - 27: Value.INACTIVE # alarm a off - } - ) - - # configure ADC channels - for id in VOLTAGES: - chan = ctrl.find_channel(id) - # must set scale before offset, so offset 0 is valid! - chan.attrs['scale'].value = SCALE - chan.attrs['offset'].value = OFFSET - - file_path = os.getcwd() - - class Root(object): - @cherrypy.expose - - # define what happens on default (index) page - def index(self): - cherrypy.log("index") - - # some sort of demo data - df = px.data.gapminder().query("country=='Canada'") - - # constrain height to 600 to avoid having to scroll for other info? - fig = px.line(df, x="year", y="lifeExp", \ - title='Life expectancy in Canada', height=600) - - # fig.to_html gives us the entire interactive graph as a python string! - plotly_data = fig.to_html(full_html=False) - - # for now, just concatenate a header and a footer - html_data = '

QuantiMotor

' + \ - plotly_data + \ - "

Thats all folks!

" - - # give the complete html to the client - return html_data - - cherrypy.quickstart(Root(), '/') - - - except OSError as ex: - print(ex, "\nD'oh!") +class Root(object): + @cherrypy.expose + + # define what happens on default (index) page + def index(self): + cherrypy.log("index") + + # some sort of demo data + df = px.data.gapminder().query("country=='Canada'") + + # constrain height to 600 to avoid having to scroll for other info? + fig = px.line(df, x="year", y="lifeExp", \ + title='Life expectancy in Canada', height=600) + + # fig.to_html gives us the entire interactive graph as a python string! + plotly_data = fig.to_html(full_html=False) + + # for now, just concatenate a header and a footer + html_data = '

QuantiMotor

' + \ + plotly_data + \ + "

Thats all folks!

" + + # give the complete html to the client + return html_data + +if __name__ == '__main__': + ctx = iio.LocalContext() + ctrl = ctx.find_device('ads8688') + + # initialize hardware + set_line_values( + "/dev/gpiochip0", + {25: Value.ACTIVE, # take ADS8688 out of reset + 4: Value.ACTIVE, # indicate 'ready' to LPC + 16: Value.INACTIVE, # pyro off + 17: Value.INACTIVE, # alarm b off + 20: Value.INACTIVE, # turn continuity LED off + 21: Value.INACTIVE, # turn armed LED off + 27: Value.INACTIVE # alarm a off + } + ) + + # configure ADC channels + for id in VOLTAGES: + chan = ctrl.find_channel(id) + # must set scale before offset, so offset 0 is valid! + chan.attrs['scale'].value = SCALE + chan.attrs['offset'].value = OFFSET + + config = { + 'global': { + 'server.socket_host': '0.0.0.0', + 'server.socket_port': 8080, + }, + } + + cherrypy.quickstart(Root(), '/', config)