Код: Выделить всё
https://trefle.io/explore?search=coconut
My code:
Код: Выделить всё
BASE_URL = 'https://trefle.io/api/v1/plants/search'
@app.route('/plant', methods=['GET', 'POST'])
@login_required
def plant():
if request.method == 'POST':
plant_name = request.form.get('name')
if not plant_name:
return jsonify({'error': 'Plant name is required'}), 400
try:
# Construct the Trefle API query
response = requests.get(
BASE_URL,
params={'token': TREFLE_API_KEY,'q': plant_name}
)
response.raise_for_status()
plant_data = response.json()
if plant_data.get('data'):
# Extract growing information
plant_info = [
{
"Name": plant.get('common_name', "N/A"),
"Scientific Name": plant.get('scientific_name', "N/A"),
"Light": plant.get('main_species', {}).get('growth', {}).get('light', "unknown"),
"Atmospheric Humidity": plant.get('main_species', {}).get('growth', {}).get('atmospheric_humidity', "unknown"),
"pH": f"Best between {plant.get('main_species', {}).get('growth', {}).get('ph_min', 'unknown')} and {plant.get('main_species', {}).get('growth', {}).get('ph_max', 'unknown')}",
"Precipitations": f"Best between {plant.get('main_species', {}).get('growth', {}).get('precipitation_min', 'unknown')} and {plant.get('main_species', {}).get('growth', {}).get('precipitation_max', 'unknown')}",
"Temperature": f"Best between {plant.get('main_species', {}).get('growth', {}).get('temperature_min', 'unknown')}°C and {plant.get('main_species', {}).get('growth', {}).get('temperature_max', 'unknown')}°C",
}
for plant in plant_data['data']
]
return render_template('plant.html', layout=layout_template, user_name=check_name(), plants=plant_info)
return jsonify({'message': 'No plants found for your query.'}), 404
except requests.exceptions.RequestException as e:
return jsonify({'error': 'Failed to fetch data from the Trefle API', 'details': str(e)}), 500
return render_template('plant.html', layout=layout_template, user_name=check_name(), plants=None)
Код: Выделить всё
"details": "500 Server Error: Internal Server Error for url: https://trefle.io/api/v1/plants/search?q=coconut&token=MY_TOKEN(the token is correct)",
"error": "Failed to fetch data from the Trefle API"
}
Приносим извинения, но что-то пошло не так.
Если вы являетесь владельцем приложения, проверьте журналы для получения дополнительной информации.>
Подробнее здесь: https://stackoverflow.com/questions/792 ... trefle-api