python 基於flask的簡單web查詢程式

2021-08-26 18:13:31 字數 2687 閱讀 7765

3個.py檔案

create_html.py # 利用pandas 將result生成 **形式

mysql.py #連線資料庫獲取資料.

from flask import flask

from flask import request

from create_html import convert_to_html

from mysql import get_result

def home():

return 'home'

def signig_from():

result = get_result()#獲取資料

title = ['監護人','學校','年級','班級','學生姓名','託管時間','簽訂時間']

h=convert_to_html(result,title)

return h

#不用管下面

def signin():

if request.form['username']=='admin' and request.form['password']=='password':

return '

import pymysql

def get_result():

conn = pymysql.connect(host='***x',user='root',passwd='***',db='ygxy-sys')

cur = conn.cursor()

sql = '''select

a.guardian_name ,

e.s_school ,

e.s_session ,

e.s_class ,

d.name,

case param->'$[0]'

when '["0", "1", "0"]' then '中午'

when '["0", "1", "1"]' then '中午/晚上'

when '["0", "0", "1"]' then '晚上'

else '其他' end ,

b.create_time

from

guardian_data as a,

student_signed_protocol as b,

guardian_ship as c,

student_data as d,

student_school as e

where

c.main_guardian_id = a.guardian_phone

and b.id = c.id

and b.sid = d.sid

and d.sid = e.sid

'''print(sql)

result = [,,,,,,]

try:

cur.execute(sql)

results = cur.fetchall()

for row in results:

for i in range(0,7):

except exception as e:

print(e)

finally:

return result

conn.close()

注:pandas中dataframe 方法,接收資料的格式如下:

從資料庫裡取出的值格式不是這樣,所以需要做如下操作,把同一列值都放到乙個list中

result = [,,,,,,]

try:

cur.execute(sql)

results = cur.fetchall()

for row in results:

for i in range(0,7):

import pandas as pd

from mysql import get_result

def convert_to_html(result,title):

d = {}

index = 0

#for row in result:

for t in title:

d[t] = result[index]

index+=1

df = pd.dataframe(d)

#如資料過長,可能在**中無法顯示,加上pd.set_option語句可以避免這一情況

pd.set_option('max_colwidth',200)

df = df [title]

h =df.to_html(index=true)

with open('11.html','w',encoding='utf-8') as f:

for eachline in h:

f.write(eachline)

return h

if __name__=="__main__":

result = get_result()

title = ['監護人','學校','年級','班級','學生姓名','託管時間','簽訂時間']

convert_to_html(result,title)

基於Flask的最簡Web請求

這裡使用了flask作為web服務的框架,其簡單精悍,非常易學易用。如下 import logging.config import json from flask import flask from flask import make response,request logging.config....

python大道至簡之flask

1.flask安裝 pip install flask2.檢查flask成功與否 進入python python3 載入flask import flask 列印版本 print flask.version 1.編寫hello.py檔案 載入flask from flask import flask...

最簡Flask 路由例項

import time input time 2020 05 01 result time time.strptime input time,y m d print result time.tm yday time.strptime 將指定格式的時間轉換為9元素 九元素 年 日 時 分 秒 一周的第...