from flask import Flask,Blueprint,request,render_template
from flask import current_app as app
from uploadCode import db
from models import CodeRecord
import zipfile
import shutil
import os
import uuid uploadBlue = Blueprint("uploadBlue", __name__) @uploadBlue.route("/upload", methods=["GET", "POST"])
def upload():
if request.method == "GET":
return render_template("upload.html", error="")
# 先获取前端传过来的文件
file = request.files.get("zip_file")
# 判断是否是zip包
zip_file_type = file.filename.rsplit(".", 1)
if zip_file_type[-1] != "zip":
return render_template("upload.html", error="上传的必须是zip包")
# 解压保存
upload_path = os.path.join(app.config.root_path, "files", str(uuid.uuid4()))
print(upload_path)
# zipfile.ZipFile(file.stream, upload_path)
shutil._unpack_zipfile(file, upload_path)
# 遍历保存的文件夹得到所有.py文件
file_list = []
for (dirpath, dirname, filenames) in os.walk(upload_path):
for filename in filenames:
file_type = filename.rsplit(".", 1)
if file_type[-1] != "py":
continue
file_path = os.path.join(dirpath, filename)
file_list.append(file_path)
# 打开每个文件读取行数
sum_num = 0
for path in file_list:
with open(path, mode="rb") as f:
for line in f:
if line.strip().startswith(b"#"):
continue
sum_num += 1
# 得到总行数去保存数据库
return str(sum_num) @uploadBlue.route("/")
def index():
# 展示用户提交代码柱状图
queryset = db.session.query(CodeRecord).all()
date_list = []
num_list = []
for obj in queryset:
date_list.append(str(obj.upload_date))
num_list.append(obj.code_nums)
return render_template("index.html", date_list=date_list, num_list=num_list)
upload
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from uploadCode.views.upload import uploadBlue def create_app():
app = Flask(__name__)
app.config.from_object("settings.DevConfig")
app.register_blueprint(uploadBlue)
db.init_app(app)
return app
__init__
from uploadCode import create_app
app =create_app()
if __name__ == '__main__':
app.run()
manager
from uploadCode import db # from uploadCode import create_app class User(db.Model):
__tablename__ = "user"
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(32))
# code_record = db.relationship("codeRecord", backref="user") class CodeRecord(db.Model):
__tablename__ ="codeRecord" id = db.Column(db.Integer,primary_key=True)
upload_date = db.Column(db.Date)
code_nums = db.Column(db.Integer) user_id= db.Column(db.Integer,db.ForeignKey("user.id")) # app = create_app()
# app_ctx = app.app_context()
# with app_ctx:
# db.create_all()
models
class DevConfig(object):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:[email protected]:3306/day104?charset=utf8"
SQLALCHEMY_POOL_SIZE = 5
SQLALCHEMY_TRACK_MODIFICATIONS = True
settings
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
请上传你的代码:<input type="file" name="zip_file">
<button type="submit">提交</button>
{{error}}
</form>
</body>
</html>
upload
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script src="/static/echarts.common.min.js"></script>
</head>
<body>
<div id="container" style="height: 600px"></div>
{{date_list}}
{{num_list}}
<div id="info" date_list="{{date_list}}" num_list="{{num_list}}"></div>
<script>
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
let infoEle = document.getElementById("info");
let date_list = infoEle.getAttribute("date_list");
let num_list = infoEle.getAttribute("num_list");
option = null;
app.title = '坐标轴刻度与标签对齐'; option = {
color: ['#3398DB'],
tooltip : {
trigger: 'axis',
axisPointer : { // 坐标轴指示器,坐标轴触发有效
type : 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis : [
{
type : 'category',
data : eval(date_list), //注意要使用eval,否则无法正常显示
axisTick: {
alignWithLabel: true
}
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'直接访问',
type:'bar',
barWidth: '60%',
data:eval(num_list) //注意要使用eval,否则无法正常显示
}
]
};
;
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
</script>
</body>
</html>
index
其中static中的引用的需要去http://www.echartsjs.com/feature.html中下载