71 lines
2.9 KiB
Python
71 lines
2.9 KiB
Python
from flask import Flask, request, jsonify
|
|
from flasgger import Swagger
|
|
import json
|
|
from algorithms import *
|
|
|
|
app = Flask(__name__)
|
|
|
|
# 配置 Swagger
|
|
app.config['SWAGGER'] = {
|
|
'title': '插值计算API',
|
|
'description': '提供基于双线性插值的计算服务',
|
|
'version': '1.0.0',
|
|
'uiversion': 3
|
|
}
|
|
swagger = Swagger(app)
|
|
|
|
@app.route('/PythonCalculate', methods=['POST'])
|
|
def calculate():
|
|
"""
|
|
插值计算接口
|
|
---
|
|
tags:
|
|
- 计算
|
|
parameters:
|
|
- in: body
|
|
name: body
|
|
required: true
|
|
schema:
|
|
type: object
|
|
properties:
|
|
text:
|
|
type: string
|
|
description: 包含计算参数的JSON字符串
|
|
example: '{"FuncName":"calculate_main","ClassName":"","Par":[{"name":"X_NUM","dataType":"0","arrayType":"0","isOut":"2","data":5},{"name":"Y_NUM","dataType":"0","arrayType":"0","isOut":"2","data":5},{"name":"X","dataType":"1","arrayType":"0","isOut":"2","data":2.5},{"name":"Y","dataType":"1","arrayType":"0","isOut":"2","data":3.5},{"name":"X0","dataType":"1","arrayType":"1","isOut":"2","data":[1,2,3,4,5]},{"name":"Y0","dataType":"1","arrayType":"1","isOut":"2","data":[1,2,3,4,5]},{"name":"Z0","dataType":"1","arrayType":"1","isOut":"2","data":[1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,5,6,7,8,9]},{"name":"Z","dataType":"1","arrayType":"0","isOut":"1","data":0},{"name":"ZX","dataType":"1","arrayType":"0","isOut":"1","data":0},{"name":"ZY","dataType":"1","arrayType":"0","isOut":"1","data":0},{"name":"Z_OUT","dataType":"1","arrayType":"1","isOut":"1","data":0}]}'
|
|
responses:
|
|
200:
|
|
description: 计算结果
|
|
schema:
|
|
type: object
|
|
properties:
|
|
text:
|
|
type: string
|
|
description: 包含计算结果的JSON字符串
|
|
"""
|
|
# 解析输入JSON
|
|
request_data = request.json
|
|
input_data = json.loads(request_data["text"])
|
|
|
|
# 提取输入参数
|
|
num_projects = input_data["Par"][0]["Data"]
|
|
total_resource = input_data["Par"][1]["Data"]
|
|
num_iterations = input_data["Par"][2]["Data"]
|
|
resource_allocation, total_revenue = dpsa(num_projects, total_resource, num_iterations,0.1)
|
|
# 调用算法函数
|
|
# 构建输出JSON
|
|
output_data = {
|
|
"funcName": "calculate_main",
|
|
"className": "",
|
|
"par": [
|
|
{"name": "num_projects", "dataType": 0, "arrayType": 0, "isOut": 2, "data": num_projects},
|
|
{"name": "total_resource", "dataType": 1, "arrayType": 0, "isOut": 2, "data": total_resource},
|
|
{"name": "num_iterations", "dataType": 0, "arrayType": 0, "isOut": 2, "data": num_iterations},
|
|
{"name": "resource_allocation", "dataType": 1, "arrayType": 1, "isOut": 1, "data": resource_allocation},
|
|
{"name": "total_revenue", "dataType": 1, "arrayType": 0, "isOut": 1, "data": total_revenue}
|
|
]
|
|
}
|
|
|
|
return jsonify(output_data)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |