温馨提示: 豌豆仅提供国内节点,不提供境外节点,不能用于任何非法用途,不能访问境外网站及跨境联网。

免费领取1万IP!

[雪峰磁针石博客]flask构建自动化测试平台6-交互式犯罪地图

发布时间:

6-交互式犯罪地图

本章将介绍以下主题:

  • mysql
  • 创建犯罪地图

本文最新版本
代码地址

安装mysql


# apt-get install mysql-server
# pip3 install pymysql
# python db_setup.py # 创建数据库

dbconfig.py


test = False
db_user = 'root'
db_password = '654321_'

db_setup.py


import pymysql
import dbconfig
connection = pymysql.connect(host='localhost',
                             user=dbconfig.db_user,
                             passwd=dbconfig.db_password)

try:
    with connection.cursor() as cursor:
        sql = "CREATE DATABASE IF NOT EXISTS crimemap"
        cursor.execute(sql)
        sql = """CREATE TABLE IF NOT EXISTS crimemap.crimes (
id int NOT NULL AUTO_INCREMENT,
latitude FLOAT(10,6),
longitude FLOAT(10,6),
date DATETIME,
category VARCHAR(50),
description VARCHAR(255),
updated_at TIMESTAMP,
PRIMARY KEY (id)
)"""
        cursor.execute(sql)
    connection.commit()
finally:
    connection.close()

crimemap.py


from dbhelper import DBHelper
from flask import Flask
from flask import render_template
from flask import request


app = Flask(__name__)
DB = DBHelper()


@app.route("/")
def home():
    try:
        data = DB.get_all_inputs()
    except Exception as e:
        print(e)
        data = None
    return render_template("home.html", data=data)


@app.route("/add", methods=["POST"])
def add():
    try:
        data = request.form.get("userinput")
        DB.add_input(data)
    except Exception as e:
        print(e)
    return home()


@app.route("/clear")
def clear():
    try:
        DB.clear_all()
    except Exception as e:
        print(e)
    return home()

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8000, debug=True)

dbhelper.py


import pymysql
import dbconfig


class DBHelper:

    def connect(self, database="crimemap"):
        return pymysql.connect(host='localhost',
                               user=dbconfig.db_user,
                               passwd=dbconfig.db_password,
                               db=database)

    def get_all_inputs(self):
        connection = self.connect()
        try:
            query = "SELECT description FROM crimes;"
            with connection.cursor() as cursor:
                cursor.execute(query)
            return cursor.fetchall()
        finally:
            connection.close()

    def add_input(self, data):
        connection = self.connect()
        try:
            query = "INSERT INTO crimes (description) VALUES (%s);"
            with connection.cursor() as cursor:
                cursor.execute(query, data)
                connection.commit()
        finally:
            connection.close()

    def clear_all(self):
        connection = self.connect()
        try:
            query = "DELETE FROM crimes;"
            with connection.cursor() as cursor:
                cursor.execute(query)
                connection.commit()
        finally:
            connection.close()


home.html


<html>
  <body>
    <head>
      <title>Crime Map</title>
    </head>

    <h1>Crime Map</h1>
    <form action="/add" method="POST">
      <input type="text" name="userinput">
      <input type="submit" value="Submit">
    </form>
    <a href="/clear">clear</a>
        
    {% for userinput in data %}
      <p>{{userinput}}</p>
    {% endfor %}
    
  </body>
</html>

可爱的python测试开发库 请在github上点赞,谢谢!
python中文库文档汇总
[雪峰磁针石博客]python3标准库-中文版
[雪峰磁针石博客]python3快速入门教程
接口自动化性能测试线上培训大纲
python测试开发自动化测试数据分析人工智能自学每周一练
更多内容请关注 雪峰磁针石:简书

  • 技术支持qq群: 144081101(后期会录制视频存在该群群文件) 591302926 567351477 钉钉免费群:21745728

  • 道家技术-手相手诊看相中医等钉钉群21734177 qq群:391441566 184175668 338228106 看手相、面相、舌相、抽签、体质识别。服务费50元每人次起。请联系钉钉或者微信pythontesting

图片.png

相关文章


自从Python数据可视化出了这个模块后,数据可视化就再简单不过了 PyCharm有些库(函数)没有代码提示 Python 爬虫 + 人脸检测会产生什么样的化学反应?简直不可思议! Python中的numba的基本应用!让你的Python快一万倍! 小蛇学python(15)pandas之数据合并 如何写出没有BUG的代码? 破解抖音反调试功能的思路与实践 慕课网Spark SQL日志分析 - 5.DateFrame&Dataset

上一篇:趋势:电竞游戏主播成为越南年轻人最热门的职业之一
下一篇:【译Py】数据科学面试终极指南(六)

咨询·合作