35 lines
917 B
Python
35 lines
917 B
Python
import os
|
|
import json
|
|
import sqlite3
|
|
|
|
from flask import Flask, request, jsonify, render_template
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
|
basedir = os.path.abspath(os.path.dirname(__file__))
|
|
app = Flask(__name__)
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db') # 使用 SQLite 数据库
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
class Tracker(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
distance = db.Column(db.Integer, nullable=False)
|
|
data = db.Column(db.Text, nullable=False)
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def index():
|
|
db.create_all()
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route("/", methods=["POST"])
|
|
def upload_data():
|
|
data = request.json
|
|
print(data)
|
|
record = Tracker(distance=data["distance"], data=json.dumps(data["data"]))
|
|
db.session.add(record)
|
|
db.session.commit()
|
|
return jsonify({"message": "Data recorded"}), 200
|