zoukankan      html  css  js  c++  java
  • Flask框架学习笔记(API接口管理平台 V1.0)

    今天博主终于完成了API接口管理平台,最后差的就是数据库的维护,

    博主这里介绍下平台的设计原理,首先基于python,利用flask的web框架+bootstrap前端框架完成,先阶段完成了前台展示页

    二期要加入登录退出,后台管理

    下面是文档结构图

    涉及的python第三方模块:flask、flask-bootstrap、sqlalchemy

    整体页面的布局:页头的导航,右侧的API分类,页面信息

    页面信息内容包括:接口说明,请求参数,返回参数,请求示例,返回示例

    下面是定义数据库对象的model.py 文件

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    
    from sqlalchemy import Column, create_engine
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base
    import json
    import sys
    # 导入数据库所有表字段类型
    from sqlalchemy.dialects.mysql import 
        BIGINT, BINARY, BIT, BLOB, BOOLEAN, CHAR, DATE, 
        DATETIME, DECIMAL, DECIMAL, DOUBLE, ENUM, FLOAT, INTEGER, 
        LONGBLOB, LONGTEXT, MEDIUMBLOB, MEDIUMINT, MEDIUMTEXT, NCHAR, 
        NUMERIC, NVARCHAR, REAL, SET, SMALLINT, TEXT, TIME, TIMESTAMP, 
        TINYBLOB, TINYINT, TINYTEXT, VARBINARY, VARCHAR, YEAR
    
    # 创建对象的基类:
    Base = declarative_base()
    
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    
    # 定义api表对象
    class Api(Base):
        # 表的名字:
        __tablename__ = 'api'
    
        # 表的结构:
        id = Column(INTEGER(10), primary_key=True)
        name = Column(VARCHAR(50))
        url = Column(TEXT)
        method = Column(VARCHAR(10))
        service = Column(VARCHAR(50))
        access_token = Column(VARCHAR(255))
        reqParam = Column(TEXT)
        response = Column(TEXT)
        requestExam = Column(TEXT)
        responseExam = Column(TEXT)
    
    
    # 定义model表对象
    class Model(Base):
        # 表的名字:
        __tablename__ = 'model'
    
        # 表的结构:
        id = Column(VARCHAR(25), primary_key=True)
        name = Column(VARCHAR(50))
        ch_name = Column(VARCHAR(50))
        from_id = Column(VARCHAR(25))

    下面是试图views.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    
    from app import app
    from flask import render_template, flash, redirect, session, url_for, request, g
    from models import Api, Model
    from config import connect_db
    import json
    import sys
    
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    
    @app.route("/")
    @app.route("/index")
    def index():
        return render_template("index.html")
    
    
    @app.route("/<id>", methods=["GET", "POST"])
    def model(id):
        all_name = []
        db = connect_db()
        conn = db()
        table_model = conn.query(Model).filter(Model.name == id).one()
        model_name = table_model.name
        model_ch_name = table_model.ch_name
        model_all = conn.query(Model).filter(Model.from_id == table_model.id).all()
        for i in range(len(model_all)):
            name = {"ch_name": model_all[i].ch_name, "name": model_all[i].name}
            all_name.append(name)
        conn.close()
        return render_template("model.html",
                               model_name=model_name,
                               model_ch_name=model_ch_name,
                               all_name=all_name)
    
    
    @app.route("/desk/<id>")
    def form(id):
        all_names = []
        db = connect_db()
        conn = db()
        table_model = conn.query(Model).filter(Model.name == id).one()
        model_all = conn.query(Model).filter(Model.from_id == table_model.from_id).all()
        for i in range(len(model_all)):
            name = {"ch_name": model_all[i].ch_name, "name": model_all[i].name}
            all_names.append(name)
        model_id = conn.query(Model).filter(Model.id == table_model.from_id).one()
        model_ch_name = model_id.ch_name
        model_name = model_id.name
        table_api = conn.query(Api).filter(Api.id == table_model.id).one()
        name = table_api.name
        url = table_api.url
        method = table_api.method
        service = json.loads(table_api.service)
        access_token = json.loads(table_api.access_token)
        reqparam = json.loads(table_api.reqParam)
        response = json.loads(table_api.response)
        request_exam = table_api.requestExam
        response_exam = table_api.responseExam
        conn.close()
        return render_template("form.html",
                               url=url,
                               method=method,
                               name=name,
                               all_names=all_names,
                               model_name=model_name,
                               model_ch_name=model_ch_name,
                               service=service,
                               access_token=access_token,
                               reqparam=reqparam,
                               response=response,
                               request_exam=request_exam,
                               response_exam=response_exam)

    博主根据路由对首页,模块,接口 定义了视图

    最后给大家看下效果

  • 相关阅读:
    Html5新标签解释及用法
    时间线效果
    NHibernate数据访问层核心代码
    C#开源资源项目
    安卓开发环境搭建之最新版(So Easy!)
    WIN8相关资源汇总
    HTML5 Shiv – 让该死的IE系列支持HTML5吧
    WCF 提高传输数据性能方法之二 (数据分割)
    支付宝集成问题(.net篇)
    WCF 提高传输数据性能方法之一 (使用压缩)
  • 原文地址:https://www.cnblogs.com/cllovewxq/p/5696014.html
Copyright © 2011-2022 走看看