Unverified Commit fdb6d661 authored by Xiaomeng Zhao's avatar Xiaomeng Zhao Committed by GitHub
Browse files

Merge pull request #668 from LollipopsAndWine/dev

feat: 集成前端界面,配置一键启动
parents 93713047 5834b147
...@@ -4,7 +4,7 @@ from common.web_hook import before_request ...@@ -4,7 +4,7 @@ from common.web_hook import before_request
from common.logger import setup_log from common.logger import setup_log
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print("root_dir", root_dir)
def _register_db(flask_app): def _register_db(flask_app):
from common import import_models from common import import_models
...@@ -30,6 +30,8 @@ def create_app(config): ...@@ -30,6 +30,8 @@ def create_app(config):
ma.init_app(app=app) ma.init_app(app=app)
from .analysis import analysis_blue from .analysis import analysis_blue
app.register_blueprint(analysis_blue) app.register_blueprint(analysis_blue)
from .react_app import react_app_blue
app.register_blueprint(react_app_blue)
app.before_request(before_request) app.before_request(before_request)
......
...@@ -4,6 +4,7 @@ from .upload_view import UploadPdfView ...@@ -4,6 +4,7 @@ from .upload_view import UploadPdfView
from .analysis_view import AnalysisTaskView, AnalysisTaskProgressView from .analysis_view import AnalysisTaskView, AnalysisTaskProgressView
from .img_md_view import ImgView, MdView from .img_md_view import ImgView, MdView
from .task_view import TaskView, HistoricalTasksView, DeleteTaskView from .task_view import TaskView, HistoricalTasksView, DeleteTaskView
from .markdown_view import MarkdownView
analysis_blue = Blueprint('analysis', __name__) analysis_blue = Blueprint('analysis', __name__)
...@@ -15,4 +16,5 @@ api_v2.add_resource(ImgView, '/analysis/pdf_img') ...@@ -15,4 +16,5 @@ api_v2.add_resource(ImgView, '/analysis/pdf_img')
api_v2.add_resource(MdView, '/analysis/pdf_md') api_v2.add_resource(MdView, '/analysis/pdf_md')
api_v2.add_resource(TaskView, '/extract/taskQueue') api_v2.add_resource(TaskView, '/extract/taskQueue')
api_v2.add_resource(HistoricalTasksView, '/extract/list') api_v2.add_resource(HistoricalTasksView, '/extract/list')
api_v2.add_resource(DeleteTaskView, '/extract/task/<int:id>') api_v2.add_resource(DeleteTaskView, '/extract/task/<int:id>')
\ No newline at end of file api_v2.add_resource(MarkdownView, '/extract/markdown')
\ No newline at end of file
import json import json
import threading import threading
from multiprocessing import Process
from pathlib import Path from pathlib import Path
from flask import request, current_app, url_for from flask import request, current_app, url_for
from flask_restful import Resource from flask_restful import Resource
...@@ -37,6 +38,7 @@ class AnalysisTaskProgressView(Resource): ...@@ -37,6 +38,7 @@ class AnalysisTaskProgressView(Resource):
"status": analysis_pdf.status, "status": analysis_pdf.status,
"url": file_url, "url": file_url,
"fileName": file_name, "fileName": file_name,
"file_key": analysis_task.file_key,
"content": [], "content": [],
"markdownUrl": [], "markdownUrl": [],
"fullMdLink": "", "fullMdLink": "",
...@@ -53,6 +55,7 @@ class AnalysisTaskProgressView(Resource): ...@@ -53,6 +55,7 @@ class AnalysisTaskProgressView(Resource):
"status": analysis_pdf.status, "status": analysis_pdf.status,
"url": file_url, "url": file_url,
"fileName": file_name, "fileName": file_name,
"file_key": analysis_task.file_key,
"content": bbox_info, "content": bbox_info,
"markdownUrl": md_link_list, "markdownUrl": md_link_list,
"fullMdLink": full_md_link, "fullMdLink": full_md_link,
...@@ -65,6 +68,7 @@ class AnalysisTaskProgressView(Resource): ...@@ -65,6 +68,7 @@ class AnalysisTaskProgressView(Resource):
"status": analysis_pdf.status, "status": analysis_pdf.status,
"url": file_url, "url": file_url,
"fileName": file_name, "fileName": file_name,
"file_key": analysis_task.file_key,
"content": [], "content": [],
"markdownUrl": [], "markdownUrl": [],
"fullMdLink": "", "fullMdLink": "",
...@@ -78,6 +82,7 @@ class AnalysisTaskProgressView(Resource): ...@@ -78,6 +82,7 @@ class AnalysisTaskProgressView(Resource):
"status": analysis_pdf.status, "status": analysis_pdf.status,
"url": file_url, "url": file_url,
"fileName": file_name, "fileName": file_name,
"file_key": analysis_task.file_key,
"content": [], "content": [],
"markdownUrl": [], "markdownUrl": [],
"fullMdLink": "", "fullMdLink": "",
...@@ -212,10 +217,10 @@ class AnalysisTaskView(Resource): ...@@ -212,10 +217,10 @@ class AnalysisTaskView(Resource):
pdf_analysis_folder = current_app.config['PDF_ANALYSIS_FOLDER'] pdf_analysis_folder = current_app.config['PDF_ANALYSIS_FOLDER']
pdf_dir = f"{current_app.static_folder}/{pdf_analysis_folder}/{file_stem}" pdf_dir = f"{current_app.static_folder}/{pdf_analysis_folder}/{file_stem}"
image_dir = f"{pdf_dir}/images" image_dir = f"{pdf_dir}/images"
t = threading.Thread(target=analysis_pdf_task, process = Process(target=analysis_pdf_task,
args=(pdf_dir, image_dir, file_path, analysis_task.is_ocr, args=(pdf_dir, image_dir, file_path, analysis_task.is_ocr,
analysis_task.analysis_pdf_id)) analysis_task.analysis_pdf_id))
t.start() process.start()
# 生成文件的URL路径 # 生成文件的URL路径
file_url = url_for('analysis.uploadpdfview', filename=analysis_task.file_name, as_attachment=False) file_url = url_for('analysis.uploadpdfview', filename=analysis_task.file_name, as_attachment=False)
......
import json
from pathlib import Path
from flask import request, current_app
from flask_restful import Resource
from common.custom_response import generate_response
class MarkdownView(Resource):
def put(self):
"""
编辑markdown
"""
params = json.loads(request.data)
file_key = params.get('file_key')
data = params.get('data', {})
if not data:
return generate_response(code=400, msg="empty data", msgZH="数据为空,无法更新markdown")
pdf_analysis_folder = current_app.config['PDF_ANALYSIS_FOLDER']
pdf_dir = f"{current_app.static_folder}/{pdf_analysis_folder}"
markdown_file_dir = ""
for path_obj in Path(pdf_dir).iterdir():
if path_obj.name.startswith(file_key):
markdown_file_dir = path_obj
break
if markdown_file_dir and Path(markdown_file_dir).exists():
for k, v in data.items():
md_path = f"{markdown_file_dir}/{k}.md"
if Path(md_path).exists():
with open(md_path, 'w', encoding="utf-8") as f:
f.write(v)
full_content = ""
for path_obj in Path(markdown_file_dir).iterdir():
if path_obj.is_file() and path_obj.suffix == ".md" and path_obj.stem != "full":
with open(path_obj, 'r', encoding="utf-8") as f:
full_content += f.read() + "\n"
with open(f"{markdown_file_dir}/full.md", 'w', encoding="utf-8") as f:
f.write(full_content)
else:
return generate_response(code=400, msg="Invalid file_key", msgZH="文件哈希错误")
return generate_response()
...@@ -97,7 +97,7 @@ def analysis_pdf_task(pdf_dir, image_dir, pdf_path, is_ocr, analysis_pdf_id): ...@@ -97,7 +97,7 @@ def analysis_pdf_task(pdf_dir, image_dir, pdf_path, is_ocr, analysis_pdf_id):
full_md_content += item["md_content"] + "\n" full_md_content += item["md_content"] + "\n"
full_md_name = "full.md" full_md_name = "full.md"
with open(f"{pdf_dir}/{full_md_name}", "w") as file: with open(f"{pdf_dir}/{full_md_name}", "w", encoding="utf-8") as file:
file.write(full_md_content) file.write(full_md_content)
with app.app_context(): with app.app_context():
full_md_link = url_for('analysis.mdview', filename=full_md_name, as_attachment=False) full_md_link = url_for('analysis.mdview', filename=full_md_name, as_attachment=False)
...@@ -108,7 +108,7 @@ def analysis_pdf_task(pdf_dir, image_dir, pdf_path, is_ocr, analysis_pdf_id): ...@@ -108,7 +108,7 @@ def analysis_pdf_task(pdf_dir, image_dir, pdf_path, is_ocr, analysis_pdf_id):
for n, md in enumerate(json.loads(md_content)): for n, md in enumerate(json.loads(md_content)):
md_content = md["md_content"] md_content = md["md_content"]
md_name = f"{md.get('page_no', n)}.md" md_name = f"{md.get('page_no', n)}.md"
with open(f"{pdf_dir}/{md_name}", "w") as file: with open(f"{pdf_dir}/{md_name}", "w", encoding="utf-8") as file:
file.write(md_content) file.write(md_content)
md_url = url_for('analysis.mdview', filename=md_name, as_attachment=False) md_url = url_for('analysis.mdview', filename=md_name, as_attachment=False)
md_link_list.append(f"{md_url}&pdf={pdf_name}") md_link_list.append(f"{md_url}&pdf={pdf_name}")
......
...@@ -59,3 +59,4 @@ db = SQLAlchemy() ...@@ -59,3 +59,4 @@ db = SQLAlchemy()
migrate = Migrate() migrate = Migrate()
jwt = JWTManager() jwt = JWTManager()
ma = Marshmallow() ma = Marshmallow()
folder = app.config.get("REACT_APP_DIST")
from pathlib import Path
from flask import Blueprint
from ..extentions import app, Api
from .react_app_view import ReactAppView
from loguru import logger
folder = Path(app.config.get("REACT_APP_DIST", "../../web/dist/")).resolve()
logger.info(f"react_app folder: {folder}")
react_app_blue = Blueprint('react_app', __name__, static_folder=folder, static_url_path='', template_folder=folder)
react_app_api = Api(react_app_blue, prefix='')
react_app_api.add_resource(ReactAppView, '/')
\ No newline at end of file
from flask import render_template, Response
from flask_restful import Resource
class ReactAppView(Resource):
def get(self):
# 创建自定义的响应对象
rendered_template = render_template('index.html')
response = Response(rendered_template, mimetype='text/html')
return response
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment