Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
|
|
|
|
|
|
|
| 3 |
# Corrige problema fontconfig (necessário para WeasyPrint em alguns ambientes)
|
| 4 |
os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
|
| 5 |
os.makedirs('/tmp/fontconfig', exist_ok=True)
|
|
@@ -11,7 +13,6 @@ import io
|
|
| 11 |
import base64
|
| 12 |
from weasyprint import HTML
|
| 13 |
|
| 14 |
-
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
def gerar_simulacao(capital, studio_ret, valorizacao, franquia_ret, acoes_ret, renda_fixa, anos=10, inflacao=4.5):
|
|
@@ -28,7 +29,6 @@ def gerar_simulacao(capital, studio_ret, valorizacao, franquia_ret, acoes_ret, r
|
|
| 28 |
renda_valor = cap_renda
|
| 29 |
franquia_valor = cap_franquia
|
| 30 |
else:
|
| 31 |
-
# Renda de aluguel anual (studio_ret é mensal)
|
| 32 |
renda_aluguel = cap_studio * (studio_ret / 100) * 12
|
| 33 |
cap_studio += renda_aluguel
|
| 34 |
cap_studio *= (1 + valorizacao / 100)
|
|
@@ -38,7 +38,6 @@ def gerar_simulacao(capital, studio_ret, valorizacao, franquia_ret, acoes_ret, r
|
|
| 38 |
cap_acoes *= (1 + acoes_ret / 100)
|
| 39 |
cap_renda *= (1 + renda_fixa / 100)
|
| 40 |
|
| 41 |
-
# Ajusta pela inflação anual
|
| 42 |
cap_studio /= (1 + inflacao / 100)
|
| 43 |
cap_acoes /= (1 + inflacao / 100)
|
| 44 |
cap_renda /= (1 + inflacao / 100)
|
|
@@ -85,6 +84,8 @@ def plotar_grafico(df):
|
|
| 85 |
def index():
|
| 86 |
grafico = None
|
| 87 |
df = None
|
|
|
|
|
|
|
| 88 |
if request.method == 'POST':
|
| 89 |
try:
|
| 90 |
capital = float(request.form['capital'])
|
|
@@ -100,10 +101,11 @@ def index():
|
|
| 100 |
|
| 101 |
return render_template('index.html', grafico=grafico, tabela=df.to_html(classes="table table-striped table-hover", index=False),
|
| 102 |
capital=capital, studio_ret=studio_ret, valorizacao=valorizacao, franquia_ret=franquia_ret,
|
| 103 |
-
acoes_ret=acoes_ret, renda_fixa=renda_fixa, inflacao=inflacao
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
return f"Erro: {e}"
|
| 106 |
-
return render_template('index.html')
|
| 107 |
|
| 108 |
@app.route('/download_excel', methods=['POST'])
|
| 109 |
def download_excel():
|
|
@@ -143,12 +145,11 @@ def download_pdf():
|
|
| 143 |
df = gerar_simulacao(capital, studio_ret, valorizacao, franquia_ret, acoes_ret, renda_fixa, inflacao=inflacao)
|
| 144 |
grafico = plotar_grafico(df)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
valores_finais = df.iloc[-1, 1:] # pega a última linha, exceto a coluna 'Ano'
|
| 148 |
investimento_mais_valorizado = valores_finais.idxmax()
|
| 149 |
valor_mais_alto = valores_finais.max()
|
| 150 |
|
| 151 |
-
html_content = render_template('
|
| 152 |
tabela=df.to_html(classes="table table-striped table-hover", index=False),
|
| 153 |
grafico=grafico,
|
| 154 |
capital=capital,
|
|
@@ -158,9 +159,10 @@ def download_pdf():
|
|
| 158 |
acoes_ret=acoes_ret,
|
| 159 |
renda_fixa=renda_fixa,
|
| 160 |
inflacao=inflacao,
|
| 161 |
-
df=df,
|
| 162 |
investimento_mais_valorizado=investimento_mais_valorizado,
|
| 163 |
-
valor_mais_alto=valor_mais_alto
|
|
|
|
|
|
|
| 164 |
pdf = HTML(string=html_content).write_pdf()
|
| 165 |
|
| 166 |
response = make_response(pdf)
|
|
@@ -172,3 +174,4 @@ def download_pdf():
|
|
| 172 |
|
| 173 |
if __name__ == '__main__':
|
| 174 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
# Corrige problema fontconfig (necessário para WeasyPrint em alguns ambientes)
|
| 6 |
os.environ['FONTCONFIG_PATH'] = '/tmp/fontconfig'
|
| 7 |
os.makedirs('/tmp/fontconfig', exist_ok=True)
|
|
|
|
| 13 |
import base64
|
| 14 |
from weasyprint import HTML
|
| 15 |
|
|
|
|
| 16 |
app = Flask(__name__)
|
| 17 |
|
| 18 |
def gerar_simulacao(capital, studio_ret, valorizacao, franquia_ret, acoes_ret, renda_fixa, anos=10, inflacao=4.5):
|
|
|
|
| 29 |
renda_valor = cap_renda
|
| 30 |
franquia_valor = cap_franquia
|
| 31 |
else:
|
|
|
|
| 32 |
renda_aluguel = cap_studio * (studio_ret / 100) * 12
|
| 33 |
cap_studio += renda_aluguel
|
| 34 |
cap_studio *= (1 + valorizacao / 100)
|
|
|
|
| 38 |
cap_acoes *= (1 + acoes_ret / 100)
|
| 39 |
cap_renda *= (1 + renda_fixa / 100)
|
| 40 |
|
|
|
|
| 41 |
cap_studio /= (1 + inflacao / 100)
|
| 42 |
cap_acoes /= (1 + inflacao / 100)
|
| 43 |
cap_renda /= (1 + inflacao / 100)
|
|
|
|
| 84 |
def index():
|
| 85 |
grafico = None
|
| 86 |
df = None
|
| 87 |
+
gerar_pdf = False # Flag para template
|
| 88 |
+
|
| 89 |
if request.method == 'POST':
|
| 90 |
try:
|
| 91 |
capital = float(request.form['capital'])
|
|
|
|
| 101 |
|
| 102 |
return render_template('index.html', grafico=grafico, tabela=df.to_html(classes="table table-striped table-hover", index=False),
|
| 103 |
capital=capital, studio_ret=studio_ret, valorizacao=valorizacao, franquia_ret=franquia_ret,
|
| 104 |
+
acoes_ret=acoes_ret, renda_fixa=renda_fixa, inflacao=inflacao,
|
| 105 |
+
gerar_pdf=gerar_pdf)
|
| 106 |
except Exception as e:
|
| 107 |
return f"Erro: {e}"
|
| 108 |
+
return render_template('index.html', gerar_pdf=gerar_pdf)
|
| 109 |
|
| 110 |
@app.route('/download_excel', methods=['POST'])
|
| 111 |
def download_excel():
|
|
|
|
| 145 |
df = gerar_simulacao(capital, studio_ret, valorizacao, franquia_ret, acoes_ret, renda_fixa, inflacao=inflacao)
|
| 146 |
grafico = plotar_grafico(df)
|
| 147 |
|
| 148 |
+
valores_finais = df.iloc[-1, 1:]
|
|
|
|
| 149 |
investimento_mais_valorizado = valores_finais.idxmax()
|
| 150 |
valor_mais_alto = valores_finais.max()
|
| 151 |
|
| 152 |
+
html_content = render_template('index.html',
|
| 153 |
tabela=df.to_html(classes="table table-striped table-hover", index=False),
|
| 154 |
grafico=grafico,
|
| 155 |
capital=capital,
|
|
|
|
| 159 |
acoes_ret=acoes_ret,
|
| 160 |
renda_fixa=renda_fixa,
|
| 161 |
inflacao=inflacao,
|
|
|
|
| 162 |
investimento_mais_valorizado=investimento_mais_valorizado,
|
| 163 |
+
valor_mais_alto=valor_mais_alto,
|
| 164 |
+
gerar_pdf=True) # Passa a flag para ocultar botão no PDF
|
| 165 |
+
|
| 166 |
pdf = HTML(string=html_content).write_pdf()
|
| 167 |
|
| 168 |
response = make_response(pdf)
|
|
|
|
| 174 |
|
| 175 |
if __name__ == '__main__':
|
| 176 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
| 177 |
+
|