second commit
This commit is contained in:
14
README.md
Normal file
14
README.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
|
||||||
|
From the windows command line, execute the following two commands to install the libraries:
|
||||||
|
|
||||||
|
```
|
||||||
|
pip install pysondb
|
||||||
|
pip install fastapi
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
To run this app, from wnidows command line, execute:
|
||||||
|
```
|
||||||
|
fastapi dev --host 0.0.0.0 --port 8100 main.py
|
||||||
|
```
|
||||||
60
care_status.json
Normal file
60
care_status.json
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
{
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"name": "Gordie",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 642860791847515770
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ozzy",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 520275854060945539
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Cuddles",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 910984427821348550
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Kya",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 142082488335022425
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Scooter",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 276233041724878775
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Juju",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 812769644527389690
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Wanda",
|
||||||
|
"got_prepped": false,
|
||||||
|
"was_fed": false,
|
||||||
|
"has_cleaned": false,
|
||||||
|
"has_leftovers": false,
|
||||||
|
"id": 128015503582002645
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
1
care_transactions.json
Normal file
1
care_transactions.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"data": []}
|
||||||
62
main.py
Normal file
62
main.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#Just some generic imports
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from starlette.responses import FileResponse
|
||||||
|
from pysondb import db
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
# Get handles to database
|
||||||
|
care_db_file_name = "care_status.json"
|
||||||
|
care_transactions_file_name = "care_transactions.json"
|
||||||
|
|
||||||
|
status_db = db.getDb(care_db_file_name)
|
||||||
|
transactions_db = db.getDb(care_transactions_file_name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Return a html located within the pages folder
|
||||||
|
@app.get("/")
|
||||||
|
def root():
|
||||||
|
return FileResponse('pages/checkboxes_demo.html')
|
||||||
|
|
||||||
|
|
||||||
|
# Return data by just dumping the database content
|
||||||
|
@app.get('/data.json')
|
||||||
|
def get_data():
|
||||||
|
all_rows = status_db.getAll()
|
||||||
|
print("hello!")
|
||||||
|
for x in all_rows:
|
||||||
|
del x['id']
|
||||||
|
return {'result': all_rows}
|
||||||
|
|
||||||
|
|
||||||
|
# This is the "schema" of web request made back and forth between the frontend and backend
|
||||||
|
class UpdateRecordFeedSheet(BaseModel):
|
||||||
|
animal_name: str
|
||||||
|
column_name: str
|
||||||
|
value_set: bool
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# This updates a record
|
||||||
|
@app.post('/update')
|
||||||
|
def update_data(item: UpdateRecordFeedSheet):
|
||||||
|
|
||||||
|
# Extract the needed values
|
||||||
|
animal_name = item.animal_name
|
||||||
|
column_name = item.column_name
|
||||||
|
value_set = item.value_set
|
||||||
|
|
||||||
|
|
||||||
|
#Find the row to update, and update the new value
|
||||||
|
row_to_update = status_db.reSearch('name',animal_name)[0] #Pick the first row of results. This isn't a proper database
|
||||||
|
status_db.updateById(row_to_update['id'], {column_name:value_set})
|
||||||
|
|
||||||
|
# Add what was done to the transactions database
|
||||||
|
transactions_db.add( {'name': animal_name, 'column_updated': column_name, 'value_set': value_set, 'timestamp':datetime.now().isoformat()})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
95
pages/checkboxes_demo.html
Normal file
95
pages/checkboxes_demo.html
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
|
||||||
|
|
||||||
|
<link href="https://unpkg.com/tabulator-tables@6.3.0/dist/css/tabulator.min.css" rel="stylesheet">
|
||||||
|
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@6.3.0/dist/js/tabulator.min.js"></script>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<div id="example-table"></div>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
/* Function to execute when someone clicks on a cell */
|
||||||
|
function cell_click_event(event, cell_clicked) {
|
||||||
|
/* get all the values */
|
||||||
|
column_name = cell_clicked.getColumn()
|
||||||
|
.getField()
|
||||||
|
column_value = cell_clicked.getValue()
|
||||||
|
animal_name = cell_clicked.getRow()
|
||||||
|
.getCells()[0].getValue()
|
||||||
|
new_value = !column_value
|
||||||
|
|
||||||
|
/* format the data to send. Note how this resembles the UpdateRecordFeedSheet class in main.py */
|
||||||
|
let data_to_send = {
|
||||||
|
'animal_name': animal_name,
|
||||||
|
'column_name': column_name,
|
||||||
|
'value_set': new_value
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Send the data as a post request. If the request is done well, then update the cell value */
|
||||||
|
$.post({
|
||||||
|
url: 'update',
|
||||||
|
data: JSON.stringify(data_to_send),
|
||||||
|
dataType: 'json',
|
||||||
|
contentType: "application/json",
|
||||||
|
success: function(data) {
|
||||||
|
cell_clicked.setValue(new_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Get data from the data.json endpoint in main.py, and then create a table */
|
||||||
|
$.getJSON("data.json", function(data) {
|
||||||
|
var table_data = data["result"]
|
||||||
|
|
||||||
|
console.log(table_data)
|
||||||
|
|
||||||
|
|
||||||
|
/* Create the table */
|
||||||
|
var table = new Tabulator("#example-table", {
|
||||||
|
columns: [{
|
||||||
|
title: "Name",
|
||||||
|
field: "name"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Prepped?",
|
||||||
|
formatter: 'tickCross',
|
||||||
|
field: 'got_prepped',
|
||||||
|
sorter: "boolean",
|
||||||
|
cellClick: cell_click_event
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
title: "Fed?",
|
||||||
|
formatter: 'tickCross',
|
||||||
|
field: "was_fed",
|
||||||
|
sorter: "boolean",
|
||||||
|
cellClick: cell_click_event
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Cleaned?",
|
||||||
|
formatter: 'tickCross',
|
||||||
|
field: "has_cleaned",
|
||||||
|
sorter: "boolean",
|
||||||
|
cellClick: cell_click_event
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Leftovers?",
|
||||||
|
formatter: 'tickCross',
|
||||||
|
field: "has_leftovers",
|
||||||
|
sorter: "boolean",
|
||||||
|
cellClick: cell_click_event
|
||||||
|
},
|
||||||
|
|
||||||
|
],
|
||||||
|
data: table_data
|
||||||
|
});
|
||||||
|
})
|
||||||
|
</script>
|
||||||
39
prep_database.py
Normal file
39
prep_database.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# This generates database file
|
||||||
|
|
||||||
|
from pysondb import db
|
||||||
|
import os
|
||||||
|
|
||||||
|
care_db_file_name = "care_status.json"
|
||||||
|
care_transactions_file_name = "care_transactions.json"
|
||||||
|
|
||||||
|
|
||||||
|
# Clear out database files
|
||||||
|
def remove_file(file_name):
|
||||||
|
if os.path.exists(file_name):
|
||||||
|
os.remove(file_name)
|
||||||
|
|
||||||
|
|
||||||
|
remove_file(care_db_file_name)
|
||||||
|
remove_file(care_transactions_file_name)
|
||||||
|
|
||||||
|
|
||||||
|
# Recreate database files
|
||||||
|
status_db = db.getDb(care_db_file_name)
|
||||||
|
transactions_db = db.getDb(care_transactions_file_name)
|
||||||
|
|
||||||
|
|
||||||
|
#We need to create a database where the first column is the animal name, and the subsequent columns are whether the food was prepped, they were fed, house was cleaned, and whether they had leftovers
|
||||||
|
|
||||||
|
columns_to_add = ["got_prepped", "was_fed", "has_cleaned", "has_leftovers"]
|
||||||
|
animal_names = ["Gordie", "Ozzy", "Cuddles", "Kya", "Scooter", "Juju", "Wanda"]
|
||||||
|
|
||||||
|
|
||||||
|
# Go row by row, and add information
|
||||||
|
for animal in animal_names:
|
||||||
|
row_to_add = {'name': animal}
|
||||||
|
for column in columns_to_add:
|
||||||
|
row_to_add[column] = False #Set the "prepped/fed/cleaned/leftovers to false
|
||||||
|
|
||||||
|
status_db.add(row_to_add)
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user