1. 程式人生 > >pythonweb簡單框架中的application

pythonweb簡單框架中的application

# 根據不同的路徑返回不同的網頁
# 讓我們的入口函式讀起來像目錄
# 一個功能一個函式
import re

import pymysql
from pymysql import connect

# 定義一個空的字典
url_dict = dict()


# flask核心功能 就是路由功能
# 路由功能 完成
# 路由功能的功能就是用來控制當前的網頁是否展示
def set_url(url):
	def set_fun(func):
		def call_fun(*args, **kwargs):
			print("新增許可權")
			return func(*args, **kwargs)

		print(call_fun)  # 函式的引用
		print(url)  # 函式對應的地址

		# 新增到我們的字典中
		url_dict[url] = call_fun

		return call_fun

	return set_fun


# 如果if超過三個以上,我們可以考慮使用字典

def application(file_path):
	# 響應頭
	head_stauts = "HTTP/1.1 200 OK\r\n"

	# 定義一個url的字典
	# url_dict = {"/index.html": index, "/center.html": center, "/login.html": login}

	print("自動生成的字典:", url_dict)
	try:
		# 根據不同的地址去字典獲取相應的函式引用
		fun = url_dict[file_path]
		# 得到相應體
		body = fun()

	except Exception as e:
		print("異常:", e)
		head_stauts = "HTTP/1.1 404 not found\r\n"
		body = "not page is show"

	return head_stauts, body

################################################################上面全是框架的程式碼#################################


# 獲取前端程式碼,展示頁面
@set_url('/index.html')
def index():
	with open('./templates/index.html','r') as f:
		content = f.read()

	# return content

	# 連結
	conn = pymysql.connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
	# curser物件
	cs = conn.cursor()

	# 執行sql語句
	cs.execute("""select * from info;""")

	# 獲取資料
	data = cs.fetchall()

	# 關閉
	cs.close()
	conn.close()

	# 處理資料

	# 總字串
	table_str = ""

	# 資料標籤
	row_str = """
	<tr>
			<td>%s</td>
			<td>%s</td>
			<td>%s</td>
			<td>%s</td>
			<td>%s</td>
			<td>%s</td>
			<td>%s</td>
			<td>%s</td>
			<td>
				<input type="button" value="新增" id="toAdd" name="toAdd" systemidvaule="000007">
			</td>
			</tr>
			"""
	# 處理資料
	for temp in data:
		# print(temp)

		# 替換標籤內的資料,合成總字串
		table_str += row_str % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7])

	# 將前段留下的content替換成總字串
	content_new = re.sub(r'{%content%}',table_str,content)

	# print(content_new)

	return content_new


# 獲取前端程式碼,展示頁面
@set_url('/center.html')
def center():
	with open('./templates/center.html','r') as f:
		content = f.read()

	# return content

	# 連結
	conn = pymysql.connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
	# curser物件
	cs = conn.cursor()

	# 執行sql語句
	cs.execute("""select info.code,info.short,info.chg,info.turnover,info.price,info.highs,focus.note_info from info inner join focus on info.id = focus.info_id;""")

	# 獲取資料
	data = cs.fetchall()

	# 關閉
	cs.close()
	conn.close()

	# 處理資料

	# 總字串
	table_str = ""

	# 資料標籤
	row_str = """
<tr>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>
                <a type="button" class="btn btn-default btn-xs" href="/update/300268.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
            </td>
            <td>
                <input type="button" value="刪除" id="toDel" name="toDel" systemidvaule="300268">
            </td>
        </tr>
			"""
	# 處理資料
	for temp in data:
		# print(temp)

		# 替換標籤內的資料,合成總字串
		table_str += row_str % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6])

	# 將前段留下的content替換成總字串
	content_new = re.sub(r'{%content%}',table_str,content)

	print(content_new)

	return content_new


# 測試用
if __name__ == '__main__':
	center()