flask框架的简易资源统计系统

flask框架的简易资源统计系统

文件目录

在这里插入图片描述

static:

在这里插入图片描述

templates

在这里插入图片描述

app.py

from config import *
from forest import *
from user import *
from conflagration import *  # 这4行引入了该项目其他几个文件


@app.route('/')
def helloflask():
    return render_template('forestdenglu.html')  # 启动时跳入到该模板


# forestdenglu.html

if __name__ == '__main__':  # 整个项目的入口
    app.run(host='127.0.1.4', port=5500, debug=True)  # 可以设置ip和端口号等等


config.py

from flask import *
from flask_sqlalchemy import *  # 引入了flask库和flask_sqlalchemy库
from sqlalchemy import func  # 这个库没用到

# flask基本配置信息:
app = Flask(__name__)
app.config.from_object('setting')    # 这个setting和setting.py是对应的
db = SQLAlchemy(app=app)

setting.py

SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:123456@127.0.0.1:3306/trees?charset=utf8'
# 该文件是连数据库的,root是用户名 123456是密码 @127.0.0.1:3306是数据库的ip和端口号,本地服务器的一般
# 都这么写 trees是数据库的名字 charset=utf8是编码方式

model.py

from config import *  # 引入config文件


# 本文件用来存放类的,写了两种类

# 第一种 用来操作数据库的数据,一个类对应一个表
class User(db.Model):  # 类名要和数据库的表名一致,首字母要大写
    id = db.Column(db.Integer, primary_key=True)  # 这下面每一行要和表中的列对应上
    password = db.Column(db.String(50))  # 例如:id是列名,db.Integer说明这是一个整形,primary_key=True说明这列是主键
# db.String(50)说明这是一个长度为50的字符串,数据库里对应的类型是VARCHAR,但是通过实际使用发现
# 类型的对应并没有那么严格。很多时候python里的类型写错了也一样能用,但是能对应上还是对应上吧

class Conflagration(db.Model):
    year = db.Column(db.Integer, primary_key=True)
    commonly = db.Column(db.Float(50))
    more = db.Column(db.Float(50))
    major = db.Column(db.Float(50))
    significant = db.Column(db.Float(50))


class Forest(db.Model):
    year = db.Column(db.Integer, primary_key=True)
    forestrylandarea = db.Column(db.Float(50))
    forestcoverage = db.Column(db.Float(50))
    plantationarea = db.Column(db.Float(50))
    forest = db.Column(db.Float(50))
    livingtree = db.Column(db.Float(50))
    standingtree = db.Column(db.Float(50))


# 第二种
# 用来存放一些数据,在后面的文件会用到,
# 大概就是给这个类传入 year, forestrylandarea, forestcoverage,等等对应的数据(有顺序的),
# 然后这个类实例化的对象就含有了这些数据
class Snfo:
    def __init__(self, year, forestrylandarea, forestcoverage, plantationarea, forest, livingtree, standingtree):
        self.year = year
        self.forestrylandarea = forestrylandarea
        self.forestcoverage = forestcoverage
        self.plantationarea = plantationarea
        self.forest = forest
        self.livingtree = livingtree
        self.standingtree = standingtree


class Cunzhi:
    def __init__(self, year, count):
        self.year = year
        self.count = count


class EchartsA:
    def __init__(self, company, count):
        self.company = company
        self.count = count


class Cnfo:
    def __init__(self, year, commonly, more, major, significant):
        self.year = year
        self.commonly = commonly
        self.more = more
        self.major = major
        self.significant = significant

forest.py

from config import *  # 引入config文件
from model import *  # 引入model文件


@app.route('/denglu')
def denglu():
    return render_template('forestdenglu.html')  # 用来跳转界面的


@app.route('/fanhuione')
def fanhuione():
    return render_template('index.html')  # 用来跳转界面的


@app.route('/tubiao')
def tubiao():
    return render_template('forestubiao.html')  # 用来跳转界面的


@app.route('/chuntubiao')  # 用来跳转界面的,根据get到的数值选择跳到哪个界面
def chuntubiao():
    opt = int(request.args.get('opt'))
    if opt == 0:
        return render_template('tubiao.html')
    elif opt == 1:
        return render_template('tubiao1.html')
    elif opt == 2:
        return render_template('tubiao2.html')
    elif opt == 3:
        return render_template('tubiao3.html')
    elif opt == 4:
        return render_template('tubiao4.html')
    else:
        return render_template('tubiao5.html')


@app.route('/forest_select', methods=['POST', 'GET'])  # 设置路由的请求接收方式
def forest_select():
    year = request.form.get('year')  # 获取form表单的数据
    list = []
    if year != None and year != '':
        list.append(Forest.year == year)

    # order_by(User.id.desc()).offset(0).limit(3)  
    # order_by():用来排序,通过里面的参数进行排序
    forests = Forest.query.filter(*list).order_by(Forest.year.asc()).all()  # 返回一个列表,里面是筛选到的数据库的数据
    snfos = []
    for f in forests:  # 通过Snfo类以及.__dict__把forests里面的数据转化为字典并存入snfos列表
        snfos.append(Snfo(f.year, f.forestrylandarea, f.forestcoverage, f.plantationarea,
                          f.forest, f.livingtree, f.standingtree).__dict__)
    return json.dumps(snfos)  # 返回一个json串

user.py

from config import *
from model import *


# 用来登录的
@app.route('/user_select', methods=['POST'])
def user_select():
    if request.method == 'POST':
        id = request.form.get('id')  # 获取form表单的数据
        password = request.form.get('password')  # 获取form表单的数据
        if not all([id, password]):  # 用来判空的
            return render_template('neirongbuquan.html')
        # 使用之前写的User类进行数据库查询
        # filter(User.id == id, User.password == password):筛选条件
        # .first():只显示第一个
        user = User.query.filter(User.id == id, User.password == password).first()
        if user:
            return render_template('index.html')
        else:
            return "密码错误!登录失败!"


@app.route('/zhuche')
def zhuche():
    return render_template("zhuche.html")


@app.route('/qudenglv')
def qudenglv():
    return render_template("forestdenglu.html")


# 用来注册的
@app.route('/user_insert', methods=['POST'])
def user_insert():
    uname = request.form.get('uname')  # 根据文本框的名字进行赋值
    upass = request.form.get('upass')
    user = User(id=uname, password=upass)  # 实例化一个User对象
    db.session.add(user)  # 添加一个User对象user
    db.session.commit()  # 事务提交 commit提交关键字
    return render_template("qudenglu.html")  # 界面跳转

forestubiao.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Log on</title>
    <link rel="stylesheet" href="{{url_for('static',filename='../static/css/main2.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename='../static/css/main3.css')}}">
</head>
<body>


<div id="log">
    全国森林资源统计系统
</div>



<div class="dakuan">
   <form action="user_select" method="post">
       <div class="one">账号:<input name="id"><br></div>
       <div class="two">密码:<input name="password" type="password"><br></div>

<div class="four">
    <input class="five" type="submit" value="登录" id="deng">
    <input class="five" type="reset" value="重置">
</div>

<div id="maolo">还没有账号?<a id="poli" href="{{url_for('zhuche')}}" >立即注册</a></div>
</form>
</div>


<div id="sda">
    ——数据来源:国家统计局——
</div>



</body>
</html>

一些注意事项

flask框架 模板html引入静态文件不能直接在scr里填路径,需要这样:

<link rel="stylesheet" href="{{url_for('static',filename='../static/css/main.css')}}">
<script src="{{ url_for('static',filename='js/jquery-1.9.1.min.js') }}"></script>

html里添加Python函数:

{{url_for('函数名字')}}

完整代码

https://github.com/20001220/shenli

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DRPAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值