from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_restx import Api, Resource, reqparse
import uuid
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:fullgroup81!@192.168.0.108:5432/healthy_track'
db = SQLAlchemy(app)
api = Api(app, version='1.0', title='TodoMVC API',
description='A simple TodoMVC API',
)
# 定义模型
class HospitalChecklistCommonData(db.Model):
id = db.Column(db.String(36), primary_key=True)
time = db.Column(db.String(255))
report_name = db.Column(db.String(255))
department = db.Column(db.String(255))
patient_name = db.Column(db.String(255))
specimen_type = db.Column(db.String(255))
gender = db.Column(db.String(255))
age = db.Column(db.String(255))
check_item = db.Column(db.String(255))
clinical_diagnosis = db.Column(db.String(255))
specimen_condition = db.Column(db.String(255))
clinical_symptoms = db.Column(db.String(255))
recommendation = db.Column(db.String(255))
test_result = db.Column(db.String(255))
test_purpose = db.Column(db.String(255))
hospital = db.Column(db.String(255))
image_url = db.Column(db.String(255))
with app.app_context():
db.create_all()
# 初始化数据库
# db.create_all()
@api.route('/api/common_data', methods=['GET'])
class QueryHospitalChecklistCommonData(Resource):
def get(self):
# 获取查询参数
# 添加更多查询条件
# 执行查询并返回结果
results = query.all()
return jsonify([item.to_dict() for item in results])
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=5555)
Assistant
You can use Flask-RESTX's reqparse module to define the required and optional arguments for your API endpoint. Here's an example:
python
User
Unable to render schema
Traceback (most recent call last):
File "/home/marstar/anaconda3/envs/my_assistant/lib/python3.11/site-packages/flask_restx/api.py", line 571, in schema
self._schema = Swagger(self).as_dict()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/marstar/anaconda3/envs/my_assistant/lib/python3.11/site-packages/flask_restx/swagger.py", line 275, in as_dict
serialized = self.serialize_resource(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/marstar/anaconda3/envs/my_assistant/lib/python3.11/site-packages/flask_restx/swagger.py", line 474, in serialize_resource
doc = self.extract_resource_doc(resource, url, route_doc=route_doc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/marstar/anaconda3/envs/my_assistant/lib/python3.11/site-packages/flask_restx/swagger.py", line 386, in extract_resource_doc
key = (name, param.get("in", "query"))
^^^^^^^^^
AttributeError: 'String' object has no attribute 'get' 报错了
Assistant
The error you're encountering is due to the fact that api.__name__ is a string and not an instance of Api. To fix this issue, you can change the line self = Api(app, version='1.0', title='TodoMVC API', description='A simple TodoMVC API', ) to self = Api(self, version='1.0', title='TodoMVC API', description='A simple TodoMVC API', ).
Here's the updated code:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
# 添加更多查询条件
if 'time' in args:
query = query.filter(HospitalChecklistCommonData.time == args['time'])
if 'report_name' in args:
query = query.filter(HospitalChecklistCommonData.report_name == args['report_name'])
if 'department' in args:
query = query.filter(HospitalChecklistCommonData.department == args['department'])
if 'patient_name' in args:
query = query.filter(HospitalChecklistCommonData.patient_name == args['patient_name'])
if 'specimen_type' in args:
query = query.filter(HospitalChecklistCommonData.specimen_type == args['specimen_type'])
if 'gender' in args:
query = query.filter(HospitalChecklistCommonData.gender == args['gender'])
if 'age' in args:
query = query.filter(HospitalChecklistCommonData.age == args['age'])
if 'check_item' in args:
query = query.filter(HospitalChecklistCommonData.check_item == args['check_item'])
if 'clinical_diagnosis' in args:
query = query.filter(HospitalChecklistCommonData.clinical_diagnosis == args['clinical_diagnosis'])
if 'specimen_condition' in args:
query = query.filter(HospitalChecklistCommonData.specimen_condition == args['specimen_condition'])
if 'clinical_symptoms' in args:
query = query.filter(HospitalChecklistCommonData.clinical_symptoms == args['clinical_symptoms'])
if 'recommendation' in args:
query = query.filter(HospitalChecklistCommonData.recommendation == args['recommendation'])
if 'test_result' in args:
query = query.filter(HospitalChecklistCommonData.test_result == args['test_result'])
if 'test_purpose' in args:
query = query.filter(HospitalChecklistCommonData.test_purpose == args['test_purpose'])
if 'hospital' in args:
query = query.filter(HospitalChecklistCommonData.hospital == args['hospital'])
if 'image_url' in args:
query = query.filter(HospitalChecklistCommonData.image_url == args['image_url'])
# 执行查询并返回结果
results = query.all()
return jsonify([item.to_dict() for item in results])
code
code
from flask_restx import Resource, Api, fields, reqparse
# Define your fields
resource_fields = {
'id': fields.String(required=True, description='The id of the resource'),
'time': fields.String(required=False, description='The time the resource was created'),
'report_name': fields.String(required=False, description='The name of the report'),
# Add more fields here...
}
# Create a parser
parser = reqparse.RequestParser()
parser.add_argument('time', type=str, help='Time of the resource', required=False)
parser.add_argument('report_name', type=str, help='Name of the report', required=False)
# Add more arguments here...
@api.route('/api/common_data', methods=['GET'])
class QueryHospitalChecklistCommonData(Resource):
def get(self):
# Parse the query parameters
args = parser.parse_args()
# Get the query results
results = query_all(args)
# Return the results as JSON
return jsonify(results)
In the example above, we defined a reqparse.RequestParser instance called parser and added arguments for the time and report_name fields. You can add more arguments for other optional fields in your API.
When the get method of QueryHospitalChecklistCommonData is called, the parser.parse_args() method will parse the query parameters and return them as a dictionary. You can then use these values to filter your database query.
Note that you should define your database query function query_all to accept the parsed arguments and return the filtered results.
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_restx import Api, Resource, reqparse, fields
app = Flask(__name__)
api = Api(app, version='1.0', title='TodoMVC API',
description='A simple TodoMVC API',
)
# Define your fields
resource_fields = {
'id': fields.String(required=True, description='The id of the resource'),
'time': fields.String(required=False, description='The time the resource was created'),
'report_name': fields.String(required=False, description='The name of the report'),
# Add more fields here...
}
# Create a parser
parser = reqparse.RequestParser()
parser.add_argument('time', type=str, help='Time of the resource', required=False)
parser.add_argument('report_name', type=str, help='Name of the report', required=False)
# Add more arguments here...
@api.route('/api/common_data', methods=['GET'])
class QueryHospitalChecklistCommonData(Resource):
def get(self):
# Parse the query parameters
args = parser.parse_args()
# Get the query results
results = query_all(args)
# Return the results as JSON
return jsonify(results)
This should resolve the issue and allow you to render the schema without any problems.