基于flask的代码上传

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的代码上传的更多相关文章
- python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)
昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...
- git使用教程1-本地代码上传到github
前言 不会使用github都不好意思说自己是码农,github作为一个开源的代码仓库管理平台,我们可以把自己的代码放到github上,分享给小伙伴,自己也能随时随地同步更新代码. 问题来了:为什么越来 ...
- Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件
一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...
- (超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库
(超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库 本地创建了一个 xcode 工程项目,现通过 命令行 将该项目上传到 github 或者 gitlab 远程仓库,具体 ...
- 将你的代码上传 Bintray 仓库
在 Android Studio 中,我们通常可以利用 gradle 来导入别人写的第三方库,通常可以简单得使用一句话就能搞定整个导包过程, 比如: compile 'net.cpacm.moneyt ...
- git使用之如何将github库下载到本地与如何将代码上传github
git使用之如何将github库下载到本地与如何将代码上传github ---------------------------------------------------------------- ...
- 一步一步实现android studio代码上传到github。
本文只注重代码上传能成功就好,不解释什么是git什么事github,git有什么优势. 1,先创建一个android应用, 第二步:创建github账户 和 安装git.网上的文章多如牛毛.唯一要说的 ...
- nginx+vsftp图片下载java代码上传
系统环境:阿里云centos7.3 安装nginx 查看nginx进程 ps aux|grep nginx 在/usr/local/nginx/sbin/目录下 nginx启动 ./nginx 快速停 ...
- Python基于Python实现批量上传文件或目录到不同的Linux服务器
基于Python实现批量上传文件或目录到不同的Linux服务器 by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...
随机推荐
- RIP动态路由的配置
RIP其实相对比会比静态路由会简单的多,只需要使用rip命令添加邻居的网络号即可. 命令: Router(config)#ip route rip Router(config-router)#netw ...
- wait和waitpid
当有多个子进程的SIGCHLD信号到达父进程的时候,如果父进程用wait等待,那么父进程在处理第一个达到的SIGCHLD信号的时候,其他的SIGCHLD信号被堵塞,而且信号不被缓存,这样就会导致信号丢 ...
- Android——Android Bundle类(转)
今天发现自己连Bundle类都没有搞清楚,于是花时间研究了一下. 根据google官方的文档(http://developer.android.com/reference/android/os/Bun ...
- maven编译插件版本配置案例
<!-- Build Settings 构建设置 --> <build> <finalName>${project.artifactId}</finalNam ...
- svn:ignore 的用处
用svn管理代码,一直以来都受到一件不爽事情的困扰: 1)有些文件或文件夹不想在commit的时候看到,虽然他们是non-versioned,比如*.bak.*.class,*.scc(vss文件), ...
- keep learning
fueling people’s creativity 助长了人们的创造力,燃烧了人们的激情
- 在Javascript弹出窗口中输入换行符
private void showMessage(string strMsg) { Page.RegisterStartupScript("scriptStr", "&l ...
- java----EL表达式
Java Web中的EL(表达式语言)详解 表达式语言(Expression Language)简称EL,它是JSP2.0中引入的一个新内容.通过EL可以简化在JSP开发中对对象的引用,从而规范页面 ...
- 【BZOJ】1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1633 一开始也想到了状态f[i]表示i以后的字符串最少删的数 然后想到的转移是 f[i]=min{f ...
- 分享在github超酷超炫特效动画,不看你会懊悔的。
有图有真相直接上效果图,有须要的朋友们能够到连接上去下载. 下载地址:https://github.com/ChrisRenke/DrawerArrowDrawable 下载地址:https://gi ...