added pgroups as table identifier
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
from typing import Annotated, Any
|
||||
|
||||
import arcticdb as adb
|
||||
import pandas as pd
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, Path
|
||||
from nicegui import app, ui
|
||||
|
||||
from aggridx import aggridx
|
||||
from registry import Registry
|
||||
|
||||
|
||||
PGroup = Annotated[str, Path(pattern=r"^p\d{5}$")]
|
||||
|
||||
|
||||
uri = "lmdb://adb"
|
||||
ac = adb.Arctic(uri)
|
||||
|
||||
@@ -26,7 +29,8 @@ router = APIRouter()
|
||||
grids = Registry()
|
||||
|
||||
|
||||
options = {
|
||||
OPTIONS = {
|
||||
"context": {"pgroup": None},
|
||||
"defaultColDef": {
|
||||
"filter": True,
|
||||
"editable": True,
|
||||
@@ -44,15 +48,16 @@ def update_adb(evt):
|
||||
# ignore event if it was no direct edit
|
||||
return
|
||||
|
||||
pgroup = evt.args["context"]["pgroup"]
|
||||
index = evt.args["data"]["index"]
|
||||
index = datetime.fromisoformat(index) # nicegui converts datetime to str
|
||||
df = lib.read("p12345", date_range=[index]).data
|
||||
df = lib.read(pgroup, date_range=[index]).data
|
||||
|
||||
col_id = evt.args["colId"]
|
||||
new_val = evt.args["newValue"]
|
||||
|
||||
df.at[index, col_id] = new_val
|
||||
lib.update("p12345", df)
|
||||
lib.update(pgroup, df)
|
||||
|
||||
|
||||
def update_grids(evt):
|
||||
@@ -65,20 +70,23 @@ def update_grids(evt):
|
||||
grid.set_cell_client(evt)
|
||||
|
||||
|
||||
@ui.page("/")
|
||||
def page():
|
||||
@ui.page("/{pgroup}")
|
||||
def page(pgroup: PGroup):
|
||||
# with ui.left_drawer() as ld:
|
||||
# ld.hide()
|
||||
# dark = ui.dark_mode()
|
||||
# ui.switch("dark mode").bind_value(dark)
|
||||
|
||||
try:
|
||||
df = lib.read("p12345").data
|
||||
df = lib.read(pgroup).data
|
||||
except adb.exceptions.NoSuchVersionException:
|
||||
df = pd.DataFrame()
|
||||
|
||||
df = df.reset_index()
|
||||
|
||||
options = OPTIONS.copy()
|
||||
options["context"]["pgroup"] = pgroup
|
||||
|
||||
grid = aggridx.from_pandas(df, options=options)
|
||||
grid.classes("h-[calc(100vh-2rem)]") # full height minus padding
|
||||
grid.on("cellValueChanged", update_adb)
|
||||
@@ -86,12 +94,12 @@ def page():
|
||||
grids.add(grid)
|
||||
|
||||
|
||||
@router.post("/append/")
|
||||
def append(row: dict[str, Any]):
|
||||
@router.post("/{pgroup}/append/")
|
||||
def append(pgroup: PGroup, row: dict[str, Any]):
|
||||
now = datetime.now()
|
||||
|
||||
df = pd.DataFrame(row, index=[now])
|
||||
lib.append("p12345", df)
|
||||
lib.append(pgroup, df)
|
||||
|
||||
now = str(now) # nicegui converts datetime to str
|
||||
row = {"index": now, **row} # setdefault would not force index to be the first column
|
||||
@@ -106,7 +114,7 @@ def append(row: dict[str, Any]):
|
||||
|
||||
app.include_router(router)
|
||||
|
||||
ui.run(title="stand", favicon="icon.png", fastapi_docs=True, dark=True)
|
||||
ui.run(title="stand", favicon="icon.png", fastapi_docs=True, dark=True, show=False)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user