Persano commited on
Commit
b21c33d
·
verified ·
1 Parent(s): 3215ca5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -13,9 +13,13 @@ from datetime import datetime
13
 
14
  app = Flask(__name__)
15
 
16
- def formatar_brl(valor):
17
- """Formata número para moeda brasileira: R$ 1.234,56"""
18
- return f"R$ {valor:,.2f}".replace(",", "X").replace(".", ",").replace("X", ".")
 
 
 
 
19
 
20
  def gerar_analise(investimentos_finais, capital):
21
  melhor = max(investimentos_finais, key=investimentos_finais.get)
@@ -23,7 +27,7 @@ def gerar_analise(investimentos_finais, capital):
23
  retorno_pct = ((valor_melhor - capital) / capital) * 100
24
  analise = f"""
25
  Após análise dos cenários projetados para 5 anos, o investimento <strong>{melhor}</strong> apresenta o melhor desempenho,
26
- com um valor final estimado de <strong>{formatar_brl(valor_melhor)}</strong>, equivalente a um retorno de <strong>{retorno_pct:.1f}%</strong>.
27
  """
28
  return analise
29
 
@@ -40,7 +44,6 @@ def index():
40
 
41
  anos = list(range(1, 6))
42
 
43
- # Cálculos
44
  patrimonio_studio = [capital * ((1 + valorizacao / 100) ** ano) for ano in anos]
45
  renda_acumulada_studio = [capital * (((1 + studio_ret / 100) ** (12 * ano)) - 1) for ano in anos]
46
  studio_total = [p + r for p, r in zip(patrimonio_studio, renda_acumulada_studio)]
@@ -101,7 +104,7 @@ def index():
101
  df_formatado = df.copy()
102
  for col in df.columns:
103
  if col != "Ano":
104
- df_formatado[col] = df_formatado[col].apply(formatar_brl)
105
  tabela = df_formatado.to_html(index=False, classes="table table-striped table-sm", border=0)
106
 
107
  analise_final = gerar_analise(investimentos_finais, capital)
@@ -112,8 +115,8 @@ def index():
112
  retorno_pct = (retorno_abs / capital) * 100
113
  resumo.append({
114
  "Investimento": nome,
115
- "Valor Final": formatar_brl(valor_final),
116
- "Retorno Absoluto": formatar_brl(retorno_abs),
117
  "Retorno (%)": f"{retorno_pct:.1f}%"
118
  })
119
  resumo = sorted(resumo, key=lambda x: float(x["Retorno (%)"].replace("%", "").replace(",", ".")), reverse=True)
@@ -147,7 +150,6 @@ def index():
147
  )
148
  return render_template("index.html")
149
 
150
-
151
  @app.route("/download_pdf", methods=["POST"])
152
  def download_pdf():
153
  capital_inicial = float(request.form["capital"])
@@ -210,7 +212,7 @@ def download_pdf():
210
  df_formatado = df.copy()
211
  for col in df.columns:
212
  if col != "Ano":
213
- df_formatado[col] = df_formatado[col].apply(formatar_brl)
214
 
215
  tabela = df_formatado.to_html(index=False, classes="table table-sm table-bordered", border=1)
216
 
@@ -222,8 +224,8 @@ def download_pdf():
222
  retorno_pct = (retorno_abs / capital_inicial) * 100
223
  resumo.append({
224
  "Investimento": nome,
225
- "Valor Final": formatar_brl(valor_final),
226
- "Retorno Absoluto": formatar_brl(retorno_abs),
227
  "Retorno (%)": f"{retorno_pct:.1f}%"
228
  })
229
  resumo = sorted(resumo, key=lambda x: float(x["Retorno (%)"].replace("%", "").replace(",", ".")), reverse=True)
@@ -237,6 +239,8 @@ def download_pdf():
237
  Isso indica que ambas as estratégias podem ser consideradas, dependendo do perfil de risco e objetivos do investidor.
238
  """
239
 
 
 
240
  explicacao_detalhada = f"""
241
  <h3>Explicações e Detalhes</h3>
242
  <p>O investimento <strong>Studio</strong> considera dois componentes importantes: a valorização patrimonial do imóvel e a renda mensal obtida com o aluguel ou uso.</p>
@@ -267,6 +271,7 @@ def download_pdf():
267
  comentario_extra=comentario_extra,
268
  resumo=resumo,
269
  explicacao_detalhada=explicacao_detalhada,
 
270
  now=datetime.now()
271
  )
272
 
@@ -280,4 +285,3 @@ def download_pdf():
280
 
281
  if __name__ == '__main__':
282
  app.run(host='0.0.0.0', port=7860, debug=True)
283
-
 
13
 
14
  app = Flask(__name__)
15
 
16
+ # Filtro para formatar valores em moeda BRL
17
+ @app.template_filter('formatar_brl')
18
+ def formatar_brl_filter(valor):
19
+ try:
20
+ return f"R$ {valor:,.2f}".replace(",", "X").replace(".", ",").replace("X", ".")
21
+ except Exception:
22
+ return valor
23
 
24
  def gerar_analise(investimentos_finais, capital):
25
  melhor = max(investimentos_finais, key=investimentos_finais.get)
 
27
  retorno_pct = ((valor_melhor - capital) / capital) * 100
28
  analise = f"""
29
  Após análise dos cenários projetados para 5 anos, o investimento <strong>{melhor}</strong> apresenta o melhor desempenho,
30
+ com um valor final estimado de <strong>{formatar_brl_filter(valor_melhor)}</strong>, equivalente a um retorno de <strong>{retorno_pct:.1f}%</strong>.
31
  """
32
  return analise
33
 
 
44
 
45
  anos = list(range(1, 6))
46
 
 
47
  patrimonio_studio = [capital * ((1 + valorizacao / 100) ** ano) for ano in anos]
48
  renda_acumulada_studio = [capital * (((1 + studio_ret / 100) ** (12 * ano)) - 1) for ano in anos]
49
  studio_total = [p + r for p, r in zip(patrimonio_studio, renda_acumulada_studio)]
 
104
  df_formatado = df.copy()
105
  for col in df.columns:
106
  if col != "Ano":
107
+ df_formatado[col] = df_formatado[col].apply(formatar_brl_filter)
108
  tabela = df_formatado.to_html(index=False, classes="table table-striped table-sm", border=0)
109
 
110
  analise_final = gerar_analise(investimentos_finais, capital)
 
115
  retorno_pct = (retorno_abs / capital) * 100
116
  resumo.append({
117
  "Investimento": nome,
118
+ "Valor Final": formatar_brl_filter(valor_final),
119
+ "Retorno Absoluto": formatar_brl_filter(retorno_abs),
120
  "Retorno (%)": f"{retorno_pct:.1f}%"
121
  })
122
  resumo = sorted(resumo, key=lambda x: float(x["Retorno (%)"].replace("%", "").replace(",", ".")), reverse=True)
 
150
  )
151
  return render_template("index.html")
152
 
 
153
  @app.route("/download_pdf", methods=["POST"])
154
  def download_pdf():
155
  capital_inicial = float(request.form["capital"])
 
212
  df_formatado = df.copy()
213
  for col in df.columns:
214
  if col != "Ano":
215
+ df_formatado[col] = df_formatado[col].apply(formatar_brl_filter)
216
 
217
  tabela = df_formatado.to_html(index=False, classes="table table-sm table-bordered", border=1)
218
 
 
224
  retorno_pct = (retorno_abs / capital_inicial) * 100
225
  resumo.append({
226
  "Investimento": nome,
227
+ "Valor Final": formatar_brl_filter(valor_final),
228
+ "Retorno Absoluto": formatar_brl_filter(retorno_abs),
229
  "Retorno (%)": f"{retorno_pct:.1f}%"
230
  })
231
  resumo = sorted(resumo, key=lambda x: float(x["Retorno (%)"].replace("%", "").replace(",", ".")), reverse=True)
 
239
  Isso indica que ambas as estratégias podem ser consideradas, dependendo do perfil de risco e objetivos do investidor.
240
  """
241
 
242
+ valor_patrimonial_studio = patrimonio_studio[-1]
243
+
244
  explicacao_detalhada = f"""
245
  <h3>Explicações e Detalhes</h3>
246
  <p>O investimento <strong>Studio</strong> considera dois componentes importantes: a valorização patrimonial do imóvel e a renda mensal obtida com o aluguel ou uso.</p>
 
271
  comentario_extra=comentario_extra,
272
  resumo=resumo,
273
  explicacao_detalhada=explicacao_detalhada,
274
+ valor_patrimonial_studio=valor_patrimonial_studio,
275
  now=datetime.now()
276
  )
277
 
 
285
 
286
  if __name__ == '__main__':
287
  app.run(host='0.0.0.0', port=7860, debug=True)