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:123456@192.168.2.128: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中下载

基于flask的代码上传的更多相关文章

  1. python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)

    昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...

  2. git使用教程1-本地代码上传到github

    前言 不会使用github都不好意思说自己是码农,github作为一个开源的代码仓库管理平台,我们可以把自己的代码放到github上,分享给小伙伴,自己也能随时随地同步更新代码. 问题来了:为什么越来 ...

  3. Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件

    一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...

  4. (超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库

    (超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库 本地创建了一个 xcode 工程项目,现通过 命令行 将该项目上传到 github 或者 gitlab 远程仓库,具体 ...

  5. 将你的代码上传 Bintray 仓库

    在 Android Studio 中,我们通常可以利用 gradle 来导入别人写的第三方库,通常可以简单得使用一句话就能搞定整个导包过程, 比如: compile 'net.cpacm.moneyt ...

  6. git使用之如何将github库下载到本地与如何将代码上传github

    git使用之如何将github库下载到本地与如何将代码上传github ---------------------------------------------------------------- ...

  7. 一步一步实现android studio代码上传到github。

    本文只注重代码上传能成功就好,不解释什么是git什么事github,git有什么优势. 1,先创建一个android应用, 第二步:创建github账户 和 安装git.网上的文章多如牛毛.唯一要说的 ...

  8. nginx+vsftp图片下载java代码上传

    系统环境:阿里云centos7.3 安装nginx 查看nginx进程 ps aux|grep nginx 在/usr/local/nginx/sbin/目录下 nginx启动 ./nginx 快速停 ...

  9. Python基于Python实现批量上传文件或目录到不同的Linux服务器

    基于Python实现批量上传文件或目录到不同的Linux服务器   by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...

随机推荐

  1. C基础之移位操作

    因为左移操作不会导致符号位出现缺位,所以不考虑符号位,低位补0即可:右移操作会涉及到符号位出现缺位的问题,所以在有符号数的右移操作时要考虑符号位怎么补的问题. 左移操作(<<)对于无符号数 ...

  2. [Busybox]Busybox制作文件系统

    问题: 1.目前busybox和bootstrap两种方案制作文件系统,哪种开发周期更短,更加简单? 2.如果需要在文件系统中添加某个package,要怎么做,如vim/udhcpd等? 转自:htt ...

  3. P2P网络穿越 NAT穿越

    http://blog.csdn.net/mazidao2008/article/details/4933730 ——————————————————————————————————————————— ...

  4. 百度echarts数据报表统计

    http://echarts.baidu.com/ http://www.hcharts.cn/demo/index.php?p=13

  5. 在函数体的“出口处”,对 return 语句的正确性和效率进行检查

    在函数体的“出口处”,对 return 语句的正确性和效率进行检查. 如果函数有返回值,那么函数的“出口处”是 return 语句. 我们不要轻视 return 语 句.如果 return 语句写得不 ...

  6. PyQt5资料

    http://bbs.fishc.com/thread-59816-1-1.html https://pypi.python.org/pypi/PyQt5/ http://www.thehackeru ...

  7. leetCode 77.Combinations (组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. Frameset 两页面互调控件技术案例

    总共包含三个页面(Html),分别为Parent.Html.ChildA.Html.ChildB.Html Parent.Html页面代码 <frameset cols="50%,*& ...

  9. cookie绕过验证码并关联对话发送一个随笔草稿箱

    先手动发送一个草稿,然后用fiddler取到body参数 代码: #coding:utf-8import requests login_url="https://passport.cnblo ...

  10. WPF进阶之接口(1):IValueConverter,IMultiValueConverter

    看一个例子,FontFamily="Trebuchet MS, GlobalSansSerif.CompositeFont" .这样一条简单的语句,熟悉WPF的人在xaml中可能经 ...