Compare-PBI-Data/app.py
chenwu da0502c369 new file: Compare-PBI-Data.spec
modified:   Evaluate_Result/admin_example_1.xlsx
	modified:   Evaluate_Result/admin_example_2.xlsx
	new file:   __pycache__/compareData.cpython-313.pyc
	new file:   __pycache__/config.cpython-313.pyc
	new file:   __pycache__/getDataFromAS.cpython-313.pyc
	new file:   __pycache__/getQueries.cpython-313.pyc
	modified:   app.py
	new file:   build/Compare-PBI-Data/Analysis-00.toc
	new file:   build/Compare-PBI-Data/Compare-PBI-Data.pkg
	new file:   build/Compare-PBI-Data/EXE-00.toc
	new file:   build/Compare-PBI-Data/PKG-00.toc
	new file:   build/Compare-PBI-Data/PYZ-00.pyz
	new file:   build/Compare-PBI-Data/PYZ-00.toc
	new file:   build/Compare-PBI-Data/base_library.zip
	new file:   build/Compare-PBI-Data/generated-2c83337f9fff21d32f0901febbdb8eec9b843eb92a216cf8171391083f1c5046.ico
	new file:   build/Compare-PBI-Data/localpycs/pyimod01_archive.pyc
	new file:   build/Compare-PBI-Data/localpycs/pyimod02_importers.pyc
	new file:   build/Compare-PBI-Data/localpycs/pyimod03_ctypes.pyc
	new file:   build/Compare-PBI-Data/localpycs/pyimod04_pywin32.pyc
	new file:   build/Compare-PBI-Data/localpycs/struct.pyc
	new file:   build/Compare-PBI-Data/warn-Compare-PBI-Data.txt
	new file:   build/Compare-PBI-Data/xref-Compare-PBI-Data.html
	new file:   build_exe.py
	modified:   config.py
	new file:   dist/Compare-PBI-Data.exe
	new file:   dist/Evaluate_Result/admin_example_1.xlsx
	new file:   dist/Evaluate_Result/admin_example_2.xlsx
	new file:   dist/Export_Json/example.json
	modified:   getDataFromAS.py
	modified:   getQueries.py
	new file:   icon.jpg
	modified:   requirements.txt
	deleted:    run.py
	modified:   templates/index.html
2025-03-08 16:21:14 +08:00

213 lines
7.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, request, render_template, redirect, url_for, Response
import os
import threading
import config
from getDataFromAS import ASDataFetcher
from getQueries import getQueries_From_json
from compareData import Comparator
import time
import sys
import webbrowser
app = Flask(__name__)
result = ""
result_buffer = []
@app.route('/')
def index():
global result
return render_template('index.html', config=config, result=result)
@app.route('/update_config', methods=['POST'])
def update_config():
for key, value in request.form.items():
if hasattr(config, key) and key not in ['CURRENT_DIR_PATH', 'os', 'NET_FOLDER']:
setattr(config, key, value)
return redirect(url_for('index'))
@app.route('/stream')
def stream():
def generate():
global result_buffer
last_index = 0
while True:
if len(result_buffer) > last_index:
# 发送新的消息,一次只发送一条,确保前端有足够时间处理
import json
json_data = json.dumps({"message": result_buffer[last_index]})
yield f"data: {json_data}\n\n"
last_index += 1
time.sleep(0.2) # 每发送一条消息后短暂休眠,给前端处理时间
else:
time.sleep(0.1) # 如果没有新消息短暂休眠避免过度占用CPU
return Response(generate(), mimetype='text/event-stream')
@app.route('/exit', methods=['POST'])
def exit_app():
# 关闭应用程序的函数
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return 'Server shutting down...'
@app.route('/run', methods=['POST'])
def run():
global result, result_buffer
result = ""
result_buffer = []
# 拼接导出页面查询json文件夹路径
json_folder_path = os.path.join(config.CURRENT_DIR_PATH, "Export_Json")
# 拼接查询结果excel文件夹路径
result_folder_path = os.path.join(config.CURRENT_DIR_PATH, "Evaluate_Result")
# 检查文件夹是否存在,如果不存在则创建它
if not os.path.exists(json_folder_path):
os.makedirs(json_folder_path)
if not os.path.exists(result_folder_path):
os.makedirs(result_folder_path)
try:
message = "---*********************************************************---\n"
message += " 开始读取json\n"
message += "---*********************************************************---\n"
result += message
result_buffer.append(message)
# 拼接导出页面查询json文件路径
export_page_json_path_1 = os.path.join(json_folder_path, config.EXPORT_PAGE_JSON_NAME_1)
export_page_json_path_2 = os.path.join(json_folder_path, config.EXPORT_PAGE_JSON_NAME_2)
# 读取json文件中的查询
queries_1 = getQueries_From_json(export_page_json_path_1)
queries_2 = getQueries_From_json(export_page_json_path_2)
message = "\n---*********************************************************---\n"
message += " 成功读取json\n"
message += "---*********************************************************---\n"
result += message
result_buffer.append(message)
except Exception as e:
message = f"错误提示: {e}\n"
result += message
result_buffer.append(message)
return redirect(url_for('index'))
# 创建 ASDataFetcher 实例
fetcher_1 = ASDataFetcher(
workspace=config.WORKSPACE_1,
username=config.USERNAME_1,
password=config.PASSWORD_1,
queries=queries_1,
json_name=config.EXPORT_PAGE_JSON_NAME_1,
result_folder_path=result_folder_path,
isAdmin=config.IS_ADMIN_1,
catalog=config.CATALOG_1,
effective_username=config.EFFECTIVE_USERNAME_1,
customdata=config.CUSTOMER_DATA_1,
role=config.ROLE_1,
model_number=1,
)
# 创建 ASDataFetcher 实例
fetcher_2 = ASDataFetcher(
workspace=config.WORKSPACE_2,
username=config.USERNAME_2,
password=config.PASSWORD_2,
queries=queries_2,
json_name=config.EXPORT_PAGE_JSON_NAME_2,
result_folder_path=result_folder_path,
isAdmin=config.IS_ADMIN_2,
catalog=config.CATALOG_2,
effective_username=config.EFFECTIVE_USERNAME_2,
customdata=config.CUSTOMER_DATA_2,
role=config.ROLE_2,
model_number=2,
)
# 定义线程任务
def run_fetcher(fetcher, print_lock):
global result, result_buffer
try:
fetcher.writeToExcel(print_lock)
with print_lock:
message = f"\n-------------------------\n{fetcher.json_name} 查询完成\n"
message += "\n".join(fetcher.log_messages) + "\n"
result += message
result_buffer.append(message)
except Exception as e:
with print_lock:
message = f"错误提示: {e}\n"
message += "\n".join(fetcher.log_messages) + "\n"
result += message
result_buffer.append(message)
return
# 创建锁
print_lock = threading.Lock()
# 创建线程
thread_1 = threading.Thread(target=run_fetcher, args=(fetcher_1, print_lock))
thread_2 = threading.Thread(target=run_fetcher, args=(fetcher_2, print_lock))
try:
message = "---*********************************************************---\n"
message += " 开始查询\n"
message += "---*********************************************************---\n"
result += message
result_buffer.append(message)
# 启动线程
thread_1.start()
thread_2.start()
# 等待线程完成
thread_1.join()
thread_2.join()
message = "\n---*********************************************************---\n"
message += " 查询完成\n"
message += "---*********************************************************---\n"
result += message
result_buffer.append(message)
except Exception as e:
message = f"错误提示: {e}\n"
result += message
result_buffer.append(message)
return redirect(url_for('index'))
try:
message = "\n---------------------------------------------------------------\n"
message += " 开始比较\n"
message += "---------------------------------------------------------------\n"
result += message
result_buffer.append(message)
comparator = Comparator(fetcher_1.result_full_excel_name, fetcher_2.result_full_excel_name)
comparator.compare_ExcelFiles()
message = "\n".join(comparator.log_messages) + "\n"
result += message
result_buffer.append(message)
message = "\n---------------------------------------------------------------\n"
message += " 比较完成\n"
message += "---------------------------------------------------------------\n"
result += message
result_buffer.append(message)
except Exception as e:
message = f"错误提示: {e}\n"
result += message
result_buffer.append(message)
return redirect(url_for('index'))
def open_browser():
"""在启动 Flask 服务器后自动打开浏览器。"""
webbrowser.open_new("http://127.0.0.1:5555/")
if __name__ == '__main__':
# 启动自动打开浏览器的线程
threading.Timer(1, open_browser).start()
app.run(debug=True, threaded=True,port=5555)