From 8c58bf019883937637197aaad89b06671388bb33 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Thu, 19 May 2022 23:17:16 +0200 Subject: [PATCH] moved rest api instantiation into separate file --- restapi.py | 38 +++----------------------------------- stand.py | 10 +++++----- tableapi.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 tableapi.py diff --git a/restapi.py b/restapi.py index 279056d..cfa3e17 100644 --- a/restapi.py +++ b/restapi.py @@ -1,47 +1,15 @@ 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) - - def GET(self): - return str(self.dfh.df) - +from tableapi import TableAPI # creating instances here allows to use TableAPI etc. as singletons print(">>> start TableAPI") -api = TableAPI() +restapi = TableAPI() conf = { "/": { @@ -52,7 +20,7 @@ conf = { } } -args = (api, "/", conf) +args = (restapi, "/", conf) thread = Thread(target=cp.quickstart, args=args, daemon=True) thread.start() diff --git a/stand.py b/stand.py index 86fa566..71a30d1 100644 --- a/stand.py +++ b/stand.py @@ -3,17 +3,17 @@ from st_aggrid import AgGrid import hacks -from restapi import api +from restapi import restapi from utils.st_utils import get_session_id, rerun st.set_page_config(layout="wide") -api.sid = get_session_id() # rest api needs current session ID to trigger the next rerun +restapi.sid = get_session_id() # rest api needs current session ID to trigger the next rerun -changed = api.changed -df = api.data +changed = restapi.changed +df = restapi.data @@ -38,7 +38,7 @@ response = AgGrid( new_df = response.get("data")#, df) if not new_df.equals(df) and not changed: - api.data = new_df + restapi.data = new_df # print("old:") # print(df) # print("new:") diff --git a/tableapi.py b/tableapi.py new file mode 100644 index 0000000..3422ecc --- /dev/null +++ b/tableapi.py @@ -0,0 +1,36 @@ +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) + + def GET(self): + return str(self.dfh.df) + + +