基于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 ...
随机推荐
- RabbitMQ之发布订阅【译】
在上一节中我们创建了一个工作队列,最好的情况是工作队列能够把任务恰到好处的分配给每一个worker.这一节中我们将做一些完全不同的事情--将消息传递给每一个消费者,这种模式被称为发布/订阅. 为了说明 ...
- 程序中判断android系统版本
public static int getAndroidSDKVersion() { int version; try { version = Integer.valueOf(android.os.B ...
- ubuntu14.04中安装jdk
1. 下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 将下载的 .g ...
- php -- 用文本来存储内容,file_put_contents,serialize,unserialize
根据存储的内容来划分 字符串: file_put_contents :将一个字符串写入文件 语法:int file_put_contents ( string $filename , mixed $d ...
- Python 资料性网站。
伯乐在线:http://blog.jobbole.com/category/python/ http://blog.chinaunix.net/uid/22334392/cid-24327-list- ...
- WPF datagrid 弹出右键菜单时先选中该项
private void datagrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { ...
- Unity中用触摸控制物体旋转和放大
using UnityEngine;using System.Collections;using System.IO; public class ScaleAndRotate : MonoBehavi ...
- UDP传输原理及数据分片——学习笔记
TCP传输可靠性是:TCP协议里自己做了设计来保证可靠性. IP报文本身是不可靠的 UDP也是 TCP做了很多复杂的协议设计,来保证可靠性. TCP 面向连接,三次握手,四次挥手 拥塞机制 重传机制 ...
- Docker 如何把镜像上传到docker hub
1 首先你得准备一个hub 的帐号, 去 https://hub.docker.com 注册吧! 2 在hub那里新建一个仓库, 这个就类似于github那边的..create ---> cre ...
- onTouch事件分发
事件机制 我们知道view中有onTouch,onClick, 1.并且onTouch优先于onClick执行, 2.onTouch有返回值,为true时onClick并不再执行了 因为一切VIew都 ...