Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
NanoPatch-UI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Wortman, Elliot (UG - Comp Sci & Elec Eng)
NanoPatch-UI
Commits
587a5235
Commit
587a5235
authored
1 year ago
by
Wortman, Elliot (UG - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
Sqlite database read & write functions
parent
d89e71e6
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
app.py
+3
-2
3 additions, 2 deletions
app.py
db_sqlite.py
+78
-0
78 additions, 0 deletions
db_sqlite.py
deploy.sh
+1
-1
1 addition, 1 deletion
deploy.sh
with
83 additions
and
3 deletions
.gitignore
+
1
−
0
View file @
587a5235
...
...
@@ -30,3 +30,4 @@ coverage
__pycache__/
env/
archive.zip
measurements.db
This diff is collapsed.
Click to expand it.
app.py
+
3
−
2
View file @
587a5235
from
flask
import
Flask
,
jsonify
,
render_template
,
send_from_directory
from
flask_cors
import
CORS
from
db_sqlite
import
read_latest_entry_as_json
app
=
Flask
(
__name__
,
static_folder
=
"
./dist/assets
"
,
template_folder
=
"
./dist
"
)
CORS
(
app
)
...
...
@@ -12,8 +14,7 @@ def main():
@app.route
(
"
/values
"
)
def
values
():
values
=
[{
"
N
"
:
10
},
{
"
P
"
:
11
},
{
"
K
"
:
12
},
{
"
T
"
:
5
},
{
"
H
"
:
6
},
{
"
M
"
:
7
}]
return
jsonify
(
values
)
return
read_latest_entry_as_json
()
@app.route
(
"
/past-values
"
)
def
pastValues
():
...
...
This diff is collapsed.
Click to expand it.
db_sqlite.py
0 → 100644
+
78
−
0
View file @
587a5235
import
sqlite3
import
json
DB_FILE
=
"
measurements.db
"
def
insert_measurement
(
input_values
):
"""
Insert values into the database and fetch the latest entry
"""
try
:
# Create a database connection
conn
=
sqlite3
.
connect
(
DB_FILE
)
cursor
=
conn
.
cursor
()
# Create table if it doesn't exist
cursor
.
execute
(
'''
CREATE TABLE IF NOT EXISTS measurements (
nitrogen INTEGER,
phosphorus INTEGER,
potassium INTEGER,
temperature INTEGER,
humidity INTEGER,
moisture INTEGER
);
'''
)
# Insert new values
cursor
.
execute
(
'''
INSERT INTO measurements
(nitrogen, phosphorus, potassium, temperature, humidity, moisture)
VALUES (?, ?, ?, ?, ?, ?)
'''
,
input_values
)
conn
.
commit
()
# Fetch the latest entry
cursor
.
execute
(
"
SELECT * FROM measurements ORDER BY rowid DESC LIMIT 1;
"
)
row
=
cursor
.
fetchone
()
# Close the connection
conn
.
close
()
# Process and return the result
if
row
:
column_names
=
[
'
nitrogen
'
,
'
phosphorus
'
,
'
potassium
'
,
'
temperature
'
,
'
humidity
'
,
'
moisture
'
]
data
=
dict
(
zip
(
column_names
,
row
))
return
json
.
dumps
(
data
,
indent
=
4
)
else
:
return
json
.
dumps
({})
except
sqlite3
.
Error
as
e
:
print
(
e
)
return
None
def
read_latest_entry_as_json
():
"""
Fetch the latest entry from the database and return it as a list of dicts
such as [{
"
N
"
: 10}, {
"
P
"
: 11}, {
"
K
"
: 12}, {
"
T
"
: 5}, {
"
H
"
: 6}, {
"
M
"
: 7}]
"""
try
:
# Create a database connection
conn
=
sqlite3
.
connect
(
DB_FILE
)
cursor
=
conn
.
cursor
()
# Fetch the latest entry
cursor
.
execute
(
"
SELECT * FROM measurements ORDER BY rowid DESC LIMIT 1;
"
)
row
=
cursor
.
fetchone
()
# Close the connection
conn
.
close
()
# Process and return the result
if
row
:
column_names
=
[
'
N
'
,
'
P
'
,
'
K
'
,
'
T
'
,
'
H
'
,
'
M
'
]
data
=
[{
column_names
[
i
]:
row
[
i
]}
for
i
in
range
(
len
(
row
))]
return
json
.
dumps
(
data
)
else
:
return
json
.
dumps
([])
except
sqlite3
.
Error
as
e
:
print
(
e
)
return
None
This diff is collapsed.
Click to expand it.
deploy.sh
+
1
−
1
View file @
587a5235
...
...
@@ -43,6 +43,6 @@ if [ ! -f "app.py" ]; then
fi
# Create a zip file
zip
-r
demo.zip requirements.txt app.py dist
zip
-r
demo.zip requirements.txt app.py
db_sqlite.py
dist
echo
"Archive 'demo.zip' created successfully."
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment