Files
stand/restapi.py
2022-05-19 10:32:02 +02:00

59 lines
1.1 KiB
Python

from threading import Thread
import json
import cherrypy as cp
from utils.df_utils import DateFrameHolder
from utils.st_utils import rerun
@cp.expose
class TableAPI:
def __init__(self):
self.dfh = DateFrameHolder()
self.sid = None
self.changed = True
@property
def data(self):
self.changed = False
return self.dfh.df
@data.setter
def data(self, df):
self.dfh.df = df
@cp.tools.json_in()
def PATCH(self, **kwargs):
kwargs = kwargs or cp.request.json
self.dfh.append(kwargs)
self.changed = True
rerun(self.sid)
return str(self.dfh.df)
# creating instances here allows to use TableAPI etc. as singletons
print(">>> start TableAPI")
api = TableAPI()
conf = {
"/": {
"request.dispatch": cp.dispatch.MethodDispatcher(),
"tools.sessions.on": True,
"tools.response_headers.on": True,
"tools.response_headers.headers": [("Content-Type", "text/plain")],
}
}
args = (api, "/", conf)
thread = Thread(target=cp.quickstart, args=args, daemon=True)
thread.start()