Flask新手求助:如何在get_match函数中复用select_symptoms的symptoms参数
Hey there! Let's work through how to get that symptoms data from your select_symptoms function into get_match in your Flask app. First, let's break down the issue: your two view functions are separate, so get_match can't directly access variables from select_symptoms—they’re in their own scopes. Also, that request.parameter_storage_class(symptoms) line isn’t the right way to handle this.
Here are a few practical solutions depending on your use case:
Solution 1: Pass the symptoms parameter directly to get_match (most straightforward)
If you want the frontend to call select_symptoms first, then send the same symptom ID to get_match, just update the get_match route to accept the parameter. You can also reuse the database query logic to fetch the symptom data:
@app.route('/organ/id/symptoms/<int:symptoms>', methods=["GET"]) def select_symptoms(symptoms): cur = mysql.connect().cursor() # Fixed the SQL syntax error (removed extra comma before FROM) # Also switched to parameterized query to avoid SQL injection! cur.execute('select symptoms, id from symptoms where id in (%s)', (symptoms,)) symptoms_list = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()] return jsonify({'The Symptoms': symptoms_list}) # Update the route to accept the symptoms parameter @app.route('/organ/id/symptoms/diagnosis/<int:symptoms>') def get_match(symptoms): # Fetch the symptom data just like in select_symptoms cur = mysql.connect().cursor() cur.execute('select symptoms, id from symptoms where id in (%s)', (symptoms,)) symptoms_data = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()] # Add your diagnosis logic here using symptoms_data return jsonify({'diagnosis_input': symptoms_data})
Important Note:
Always use parameterized queries (like %s in the example) instead of string concatenation for SQL—this prevents dangerous SQL injection attacks.
Solution 2: Use Flask Session to store data between requests
If you want users to call select_symptoms first, then access that same data in get_match without re-sending the parameter, use Flask’s session to temporarily store the symptom data. First, you need to set a secret key for your app:
app = Flask(__name__) app.secret_key = 'your_secure_secret_key_here' # Replace with a real secret key for production
Then update your view functions:
@app.route('/organ/id/symptoms/<int:symptoms>', methods=["GET"]) def select_symptoms(symptoms): cur = mysql.connect().cursor() cur.execute('select symptoms, id from symptoms where id in (%s)', (symptoms,)) symptoms_list = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()] # Store the symptom data in the user's session session['symptoms_data'] = symptoms_list return jsonify({'The Symptoms': symptoms_list}) @app.route('/organ/id/symptoms/diagnosis/') def get_match(): # Retrieve the stored data from the session symptoms_data = session.get('symptoms_data', []) if not symptoms_data: return jsonify({'error': 'No symptom data found—call /organ/id/symptoms/<id> first'}), 400 # Add your diagnosis logic here return jsonify({'diagnosis_input': symptoms_data})
Solution 3: Refactor to reuse query logic
If both functions need to fetch symptom data, pull that logic into a separate helper function to avoid code duplication:
def fetch_symptoms_by_id(symptom_id): cur = mysql.connect().cursor() cur.execute('select symptoms, id from symptoms where id in (%s)', (symptom_id,)) return [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()] @app.route('/organ/id/symptoms/<int:symptoms>', methods=["GET"]) def select_symptoms(symptoms): symptoms_list = fetch_symptoms_by_id(symptoms) return jsonify({'The Symptoms': symptoms_list}) @app.route('/organ/id/symptoms/diagnosis/<int:symptoms>') def get_match(symptoms): symptoms_data = fetch_symptoms_by_id(symptoms) # Add diagnosis logic here return jsonify({'diagnosis_input': symptoms_data})
内容的提问来源于stack exchange,提问作者jubey simanjuntak

