moved rest api instantiation into separate file

This commit is contained in:
2022-05-19 23:17:16 +02:00
parent 3d929ad54c
commit 8c58bf0198
3 changed files with 44 additions and 40 deletions

View File

@ -1,47 +1,15 @@
from threading import Thread from threading import Thread
import json
import cherrypy as cp import cherrypy as cp
from utils.df_utils import DateFrameHolder from tableapi import TableAPI
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)
# creating instances here allows to use TableAPI etc. as singletons # creating instances here allows to use TableAPI etc. as singletons
print(">>> start TableAPI") print(">>> start TableAPI")
api = TableAPI() restapi = TableAPI()
conf = { conf = {
"/": { "/": {
@ -52,7 +20,7 @@ conf = {
} }
} }
args = (api, "/", conf) args = (restapi, "/", conf)
thread = Thread(target=cp.quickstart, args=args, daemon=True) thread = Thread(target=cp.quickstart, args=args, daemon=True)
thread.start() thread.start()

View File

@ -3,17 +3,17 @@ from st_aggrid import AgGrid
import hacks import hacks
from restapi import api from restapi import restapi
from utils.st_utils import get_session_id, rerun from utils.st_utils import get_session_id, rerun
st.set_page_config(layout="wide") 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 changed = restapi.changed
df = api.data df = restapi.data
@ -38,7 +38,7 @@ response = AgGrid(
new_df = response.get("data")#, df) new_df = response.get("data")#, df)
if not new_df.equals(df) and not changed: if not new_df.equals(df) and not changed:
api.data = new_df restapi.data = new_df
# print("old:") # print("old:")
# print(df) # print(df)
# print("new:") # print("new:")

36
tableapi.py Normal file
View File

@ -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)