|
|
from flask import Flask, request, jsonify |
|
|
import matplotlib.pyplot as plt |
|
|
import io |
|
|
import base64 |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return "Backend para o simulador está rodando." |
|
|
|
|
|
@app.route('/simular', methods=['POST']) |
|
|
def simular(): |
|
|
try: |
|
|
|
|
|
capital = float(request.form.get('capital', 0)) |
|
|
studio_ret = float(request.form.get('studio_ret', 0)) / 100 |
|
|
valorizacao = float(request.form.get('valorizacao', 0)) / 100 |
|
|
franquia_ret = float(request.form.get('franquia_ret', 0)) |
|
|
acoes_ret = float(request.form.get('acoes_ret', 0)) / 100 |
|
|
renda_fixa = float(request.form.get('renda_fixa', 0)) / 100 |
|
|
inflacao = float(request.form.get('inflacao', 0)) / 100 |
|
|
|
|
|
anos = 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
studio_acumulado = capital * ((1 + studio_ret) ** (12 * anos)) |
|
|
|
|
|
|
|
|
valor_imovel = capital * ((1 + valorizacao) ** anos) |
|
|
lucro_franquia_total = franquia_ret * anos |
|
|
imovel_acumulado = valor_imovel + lucro_franquia_total |
|
|
|
|
|
|
|
|
acoes_acumulado = capital * ((1 + acoes_ret) ** anos) |
|
|
|
|
|
|
|
|
renda_fixa_acumulado = capital * ((1 + renda_fixa) ** anos) |
|
|
|
|
|
|
|
|
inflacao_acumulada = (1 + inflacao) ** anos |
|
|
|
|
|
|
|
|
studio_real = studio_acumulado / inflacao_acumulada |
|
|
imovel_real = imovel_acumulado / inflacao_acumulada |
|
|
acoes_real = acoes_acumulado / inflacao_acumulada |
|
|
renda_fixa_real = renda_fixa_acumulado / inflacao_acumulada |
|
|
|
|
|
|
|
|
texto = f""" |
|
|
Simulação para {anos} anos com capital inicial de R$ {capital:,.2f}: |
|
|
|
|
|
- Studio (retorno mensal de {studio_ret*100:.2f}%): R$ {studio_real:,.2f} (valor corrigido pela inflação) |
|
|
- Imóvel (valorização anual de {valorizacao*100:.2f}% + lucro franquia anual R$ {franquia_ret:,.2f}): R$ {imovel_real:,.2f} |
|
|
- Ações (retorno anual de {acoes_ret*100:.2f}%): R$ {acoes_real:,.2f} |
|
|
- Renda Fixa (retorno anual de {renda_fixa*100:.2f}%): R$ {renda_fixa_real:,.2f} |
|
|
|
|
|
Nota: Todos os valores estão ajustados pela inflação média anual de {inflacao*100:.2f}%. |
|
|
""" |
|
|
|
|
|
|
|
|
labels = ['Studio', 'Imóvel', 'Ações', 'Renda Fixa'] |
|
|
valores = [studio_real, imovel_real, acoes_real, renda_fixa_real] |
|
|
|
|
|
fig, ax = plt.subplots(figsize=(8,5)) |
|
|
bars = ax.bar(labels, valores, color=['#00c9a7', '#007cf0', '#ffa500', '#dd1144']) |
|
|
ax.set_title(f'Valor acumulado corrigido pela inflação em {anos} anos', fontsize=14, pad=15) |
|
|
ax.set_ylabel('Valor (R$)') |
|
|
ax.grid(axis='y', linestyle='--', alpha=0.6) |
|
|
|
|
|
|
|
|
for bar in bars: |
|
|
yval = bar.get_height() |
|
|
ax.text(bar.get_x() + bar.get_width()/2, yval + max(valores)*0.02, f'R$ {yval:,.0f}', ha='center', fontsize=11) |
|
|
|
|
|
|
|
|
buf = io.BytesIO() |
|
|
plt.tight_layout() |
|
|
plt.savefig(buf, format='png') |
|
|
plt.close(fig) |
|
|
buf.seek(0) |
|
|
img_base64 = base64.b64encode(buf.read()).decode('utf-8') |
|
|
|
|
|
return jsonify({'texto': texto.strip(), 'graficoBase64': img_base64}) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 400 |
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(debug=True) |
|
|
|
|
|
|
|
|
|