from flask import Flask, render_template,request
import pymysql
from flask_cors import *
import json

app=Flask(__name__)
CORS(app, supports_credentials=True)

@app.route("/")
def index():

    return render_template("index.html")

@app.route("/english")
def english():
    wanted = request.args.get("wanted", type=str)
    if not wanted:
        wanted = "pineapple"

    db = pymysql.connect(host="localhost", user="root", password='123456', database="english46", charset='utf8')
    cursor = db.cursor()
    try:
        cursor.execute("select * from map_enword where english like '%" + wanted + "%'")
        rs = cursor.fetchall()
        rs = (list)(rs)
        rscount = len(rs)
    except:
        print('error')
        db.rollback()
    db.close()
    return render_template("english.html", rscount=rscount, rs=rs)

@app.route("/hist")
def hist():
    db = pymysql.connect(host="localhost", user="root", password='123456', database="english46", charset='utf8')
    cursor = db.cursor()
    rs=[]
    rscount=0
    try:
        for i in range(ord('a'),ord('z')+1):
            print(i)
            i=chr(i)
            cursor.execute("select count(*) from map_enword where english like '" + i + "%'")
            count= cursor.fetchone()
            count=list(count)
            rs.append(count[0])
        rscount = len(rs)
    except:
        print('error')
        db.rollback()
    db.close()
    return render_template("hist.html", rscount=rscount, rs=rs)

@app.route("/datajson")
def datajson():
    db = pymysql.connect(host="localhost", user="root", password='123456', database="english46", charset='utf8')
    cursor = db.cursor()
    rs=[]
    rscount=0
    try:
        for i in range(ord('a'),ord('z')+1):
            print(i)
            i=chr(i)
            cursor.execute("select count(*) from map_enword where english like '" + i + "%'")
            count= cursor.fetchone()
            count=list(count)
            rs.append({'char':i,'size':count[0]})
        rscount = len(rs)
    except:
        print('error')
        db.rollback()
    db.close()

    return json.dumps(rs)

if "__main__"==__name__:
    app.run(port=5002)






