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

View File

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

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)