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. mysql学习笔记2--mysql的基本使用

    4. 运行和关闭MySQL服务器 首先检查MySQL服务器正在运行与否.在资源管理器查看有没有mysqld的进程,如果MySQL正在运行,那么会看到列出来的 mysqld 进程.如果服务器没有运行,那 ...

  2. HashMap与ConcurrentHashMap的区别(转)

    从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...

  3. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  4. C++ 运算符重载四(自定义数组类)

    //自定义数组类 #include<iostream> using namespace std; //分析:能获取数组长度,添加元素,删除元素,修改元素 //要求重载[],=,==,!=运 ...

  5. 如何修改织梦官方flash幻灯片的方法

    源代码: <!-- size: 280px * 192px --> <script language='javascript'> linkarr = new Array(); ...

  6. bootstrap基础学习一篇

    官网:http://www.bootcss.com/ 这里,主要讲解bootstrap3.关于他的介绍就不用复述了. 1.示例 <!doctype html> <html lang= ...

  7. CAEmitterCell 和 CAEmitterLayer具体解释

    一.在 UIKit 中,粒子系统由两部分组成: 1·      一个或多个  CAEmitterCells :发射器电池能够看作是单个粒子的原型(比如,一个单一的粉扑在一团烟雾).当散发出一个粒子,U ...

  8. 浅谈AutoResetEvent的用法

    using System;using System.Threading; namespace AutoResetEvent_Examples{    class MyMainClass    {    ...

  9. asp.net 动态添加多个用户控件

    动态添加多个相同用户控件,并使每个用户控件获取不同的内容. 用户控件代码: 代码WebControls using System; using System.Collections.Generic;  ...

  10. VC++ GetSafeHwnd用法

    GetSafeHwnd HWND GetSafeHwnd() const; 当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数. ...