Pharmacy Refill
Pharmacy refill queue API with HMAC-signed receipts keyed by a runtime secret and B-tree indexed lookups
One app.py with 2 tables, 3 computed columns, 6 HTTP routes. Needs pixeltable[serve]>=0.7.14. Updated 2026-10-06.
app.py
"""Pharmacy Refill API built with Pixeltable.export REFILL_SIGNING_KEY=<any-long-random-string>pxt schema update app.py pharmacypxt service run app.py pharmacy"""import pixeltable as pxtimport pixeltable.functions as pxtffrom pixeltable.serving import FastAPIRouterfrom udfs import receipt_signature, refill_state, status_label# ---- tables ----TableModel = pxt.model_base()class Prescriptions(TableModel, name='prescriptions', has_default_idxs=False):rx_number = pxt.Column(type=pxt.String, primary_key=True)patient_ref: pxt.String # an opaque reference, not a namedrug: pxt.Stringrefills_left: pxt.Intprescriber: pxt.Stringstate = refill_state(refills_left)__indexes__ = [pxt.BtreeIndex(patient_ref), pxt.BtreeIndex(drug)]class Refills(TableModel, name='refills', has_default_idxs=False):id = pxt.Column(value=pxtf.uuid.uuid7(), primary_key=True)rx_number: pxt.Stringpatient_ref: pxt.Stringrequested_at: pxt.Stringchannel: pxt.String # counter / app / drive-thrustatus: pxt.String # requested / ready / picked-uplabel = status_label(status, channel)receipt_sig = receipt_signature(rx_number, requested_at, status) # re-signed when status changes__indexes__ = [pxt.BtreeIndex(rx_number), pxt.BtreeIndex(status)]# ---- queries ----@pxt.querydef queue(status: str):"""Refills in one status, oldest request first (status index)."""return Refills.where(Refills.status == status).select(Refills.id, Refills.rx_number, Refills.requested_at, Refills.label).order_by(Refills.requested_at)@pxt.querydef patient_prescriptions(patient_ref: str):"""A patient's prescriptions (patient_ref index)."""return Prescriptions.where(Prescriptions.patient_ref == patient_ref).select(Prescriptions.rx_number, Prescriptions.drug, Prescriptions.refills_left, Prescriptions.state).order_by(Prescriptions.rx_number)# ---- routes ----refill_api = FastAPIRouter(name='refill_api')refill_api.add_insert_route(Prescriptions, path='/prescriptions',inputs=[Prescriptions.rx_number, Prescriptions.patient_ref, Prescriptions.drug, Prescriptions.refills_left,Prescriptions.prescriber],outputs=[Prescriptions.rx_number, Prescriptions.state],)refill_api.add_insert_route(Refills, path='/refills',inputs=[Refills.rx_number, Refills.patient_ref, Refills.requested_at, Refills.channel, Refills.status],outputs=[Refills.id, Refills.label, Refills.receipt_sig],)refill_api.add_update_route(Refills, path='/refills/status', inputs=[Refills.status],outputs=[Refills.id, Refills.label, Refills.receipt_sig])refill_api.add_compute_route(Refills, path='/sign',inputs=[Refills.rx_number, Refills.requested_at, Refills.status],outputs=[Refills.receipt_sig])refill_api.add_query_route(path='/refills/queue', query=queue, method='get')refill_api.add_query_route(path='/patients/prescriptions', query=patient_prescriptions, method='get')
What app.py declares
Each row links to its line.
- runs on insert
- stored
- index
- route
serves refill_api
- POST/prescriptionsinsert, prescriptionstakes rx_number, patient_ref, drug, refills_left, prescriber; returns rx_number, stateL62
- POST/refillsinsert, refillstakes rx_number, patient_ref, requested_at, channel, status; returns id, label, receipt_sigL68
- POST/refills/statusupdate, refillstakes status; returns id, label, receipt_sigL73
- POST/signcompute, refillstakes rx_number, requested_at, status; returns receipt_sigL75
- GET/refills/queuequery, queuetakes statusL78
- GET/patients/prescriptionsquery, patient_prescriptionstakes patient_refL79
Run it
Needs uv. The routes then answer on port 8000.
terminal
git clone https://github.com/pierrebrunelle/pixeltable-pharmacy-refillscd pixeltable-pharmacy-refillsuv syncuv run pxt schema update app.py pharmacyuv run python seed.py pharmacyuv run pxt service run app.py pharmacy --port 8000
Related apps
- Forge Heat CyclesForge heat-cycle registry API: B-tree indexes, chained risk UDFs, and parallel shop-floor writersShares: B-tree indexes, UDFs
- Climbing Route ToposClimbing-route topo registry API: photo uploads with thumbnails, grayscale copies, grade bands and index-backed crag lookupsShares: B-tree indexes, UDFs
- Warehouse Pick ListsWarehouse pick-list REST API: units, pick zones, waves and tote labels as incremental computed columns over Json order linesShares: B-tree indexes, UDFs