43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
from flask import Flask, render_template, jsonify
|
||
import pandas as pd
|
||
import os
|
||
|
||
app = Flask(__name__)
|
||
|
||
|
||
# 获取当前文件夹中所有包含"ofdata"字符的CSV文件
|
||
def get_csv_files():
|
||
files = {}
|
||
for filename in os.listdir():
|
||
if "ofdata" in filename and filename.endswith(".csv"):
|
||
files[filename] = os.path.join(os.getcwd(), filename)
|
||
return files
|
||
|
||
|
||
# 根据文件路径加载数据,只读取前12列
|
||
def load_data(file_path):
|
||
df = pd.read_csv(file_path, usecols=range(12)) # 只读取前12列
|
||
return df.tail(120).to_dict(orient="records")
|
||
|
||
|
||
@app.route("/")
|
||
def index():
|
||
files = get_csv_files() # 获取所有符合条件的文件
|
||
# 默认显示第一个文件的数据
|
||
first_file = list(files.keys())[0] if files else None
|
||
data = load_data(files[first_file]) if first_file else []
|
||
return render_template("index.html", data=data, files=files, file_name=first_file)
|
||
|
||
|
||
@app.route("/data/<file_name>")
|
||
def switch_data(file_name):
|
||
files = get_csv_files() # 获取所有符合条件的文件
|
||
if file_name in files:
|
||
data = load_data(files[file_name])
|
||
return jsonify(data)
|
||
return jsonify({"error": "File not found"}), 404
|
||
|
||
|
||
if __name__ == "__main__":
|
||
app.run(host='0.0.0.0', port=5000, debug=True) # 监听所有网络接口
|