DmitrMakeev commited on
Commit
3ddd90b
·
verified ·
1 Parent(s): f07dabf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -895,7 +895,17 @@ if __name__ == "__main__":
895
 
896
 
897
 
898
-
 
 
 
 
 
 
 
 
 
 
899
  @app.route('/calculation', methods=['POST'])
900
  def handle_calculation():
901
  try:
@@ -973,10 +983,10 @@ def handle_calculation():
973
 
974
  # 10. Формируем ответ
975
  response = {
976
- 'actual_profile': {k: round(v, 2) for k, v in calculator.actual_profile.items()},
977
  'fertilizers': results,
978
- 'total_ec': round(calculator.calculate_ec(), 2),
979
- 'total_ppm': round(sum(calculator.actual_profile.values()), 2),
980
  'nitrogen_ratios': {
981
  'NO3_RATIO': NO3_RATIO,
982
  'NH4_RATIO': NH4_RATIO,
@@ -984,14 +994,19 @@ def handle_calculation():
984
  }
985
  }
986
 
987
- return jsonify(response)
 
 
 
 
 
 
 
 
 
988
 
989
  except Exception as e:
990
- return jsonify({'error': f'Ошибка при расчете: {str(e)}'}), 500
991
-
992
-
993
-
994
-
995
 
996
 
997
 
 
895
 
896
 
897
 
898
+
899
+ def round_floats(obj, ndigits=3):
900
+ """Рекурсивно округляет все float значения в структуре данных"""
901
+ if isinstance(obj, float):
902
+ return round(obj, ndigits)
903
+ elif isinstance(obj, dict):
904
+ return {k: round_floats(v, ndigits) for k, v in obj.items()}
905
+ elif isinstance(obj, (list, tuple)):
906
+ return [round_floats(x, ndigits) for x in obj]
907
+ return obj
908
+
909
  @app.route('/calculation', methods=['POST'])
910
  def handle_calculation():
911
  try:
 
983
 
984
  # 10. Формируем ответ
985
  response = {
986
+ 'actual_profile': calculator.actual_profile,
987
  'fertilizers': results,
988
+ 'total_ec': calculator.calculate_ec(),
989
+ 'total_ppm': sum(calculator.actual_profile.values()),
990
  'nitrogen_ratios': {
991
  'NO3_RATIO': NO3_RATIO,
992
  'NH4_RATIO': NH4_RATIO,
 
994
  }
995
  }
996
 
997
+ # Округляем ВСЕ числовые значения в ответе до 3 знаков
998
+ rounded_response = round_floats(response)
999
+
1000
+ # Для миллиграммов применяем целочисленное округление
1001
+ if 'fertilizers' in rounded_response:
1002
+ for fert in rounded_response['fertilizers'].values():
1003
+ if 'миллиграммы' in fert:
1004
+ fert['миллиграммы'] = int(round(fert['миллиграммы']))
1005
+
1006
+ return jsonify(rounded_response)
1007
 
1008
  except Exception as e:
1009
+ return jsonify({'error': str(e)}), 500
 
 
 
 
1010
 
1011
 
1012