Update app.py
Browse files
app.py
CHANGED
@@ -139,5 +139,119 @@ def index():
|
|
139 |
)
|
140 |
return render_template("index.html")
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
if __name__ == '__main__':
|
143 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
139 |
)
|
140 |
return render_template("index.html")
|
141 |
|
142 |
+
|
143 |
+
@app.route("/download_pdf", methods=["POST"])
|
144 |
+
def download_pdf():
|
145 |
+
capital = float(request.form["capital"])
|
146 |
+
studio_ret = float(request.form["studio_ret"])
|
147 |
+
valorizacao = float(request.form["valorizacao"])
|
148 |
+
franquia_ret = float(request.form["franquia_ret"])
|
149 |
+
acoes_ret = float(request.form["acoes_ret"])
|
150 |
+
renda_fixa = float(request.form["renda_fixa"])
|
151 |
+
inflacao = float(request.form["inflacao"])
|
152 |
+
|
153 |
+
anos = list(range(1, 6))
|
154 |
+
|
155 |
+
patrimonio_studio = [capital * ((1 + valorizacao / 100) ** ano) for ano in anos]
|
156 |
+
renda_acumulada_studio = [capital * (((1 + studio_ret / 100) ** (12 * ano)) - 1) for ano in anos]
|
157 |
+
studio_total = [p + r for p, r in zip(patrimonio_studio, renda_acumulada_studio)]
|
158 |
+
franquia = [capital + (franquia_ret * ano) for ano in anos]
|
159 |
+
acoes = [capital * ((1 + acoes_ret / 100) ** ano) for ano in anos]
|
160 |
+
renda_fixa_valores = [capital * ((1 + renda_fixa / 100) ** ano) for ano in anos]
|
161 |
+
|
162 |
+
dados = {
|
163 |
+
"Ano": anos,
|
164 |
+
"Studio (Patrimônio + Renda)": studio_total,
|
165 |
+
"Franquia": franquia,
|
166 |
+
"Ações": acoes,
|
167 |
+
"Renda Fixa": renda_fixa_valores
|
168 |
+
}
|
169 |
+
df = pd.DataFrame(dados)
|
170 |
+
|
171 |
+
investimentos_finais = {
|
172 |
+
"Studio": studio_total[-1],
|
173 |
+
"Franquia": franquia[-1],
|
174 |
+
"Ações": acoes[-1],
|
175 |
+
"Renda Fixa": renda_fixa_valores[-1],
|
176 |
+
}
|
177 |
+
investimento_mais_valorizado = max(investimentos_finais, key=investimentos_finais.get)
|
178 |
+
valor_mais_alto = investimentos_finais[investimento_mais_valorizado]
|
179 |
+
|
180 |
+
# Gerar gráfico PNG
|
181 |
+
plt.figure(figsize=(8, 5))
|
182 |
+
plt.plot(anos, studio_total, label="Studio", marker="o")
|
183 |
+
plt.plot(anos, franquia, label="Franquia", marker="o")
|
184 |
+
plt.plot(anos, acoes, label="Ações", marker="o")
|
185 |
+
plt.plot(anos, renda_fixa_valores, label="Renda Fixa", marker="o")
|
186 |
+
plt.title("Projeção de Investimentos (5 anos)")
|
187 |
+
plt.xlabel("Ano")
|
188 |
+
plt.ylabel("Valor (R$)")
|
189 |
+
plt.legend()
|
190 |
+
plt.grid(True)
|
191 |
+
plt.gca().yaxis.set_major_formatter(FuncFormatter(lambda x, _: f"R${x:,.0f}".replace(",", ".")))
|
192 |
+
plt.tight_layout()
|
193 |
+
|
194 |
+
img_buf = io.BytesIO()
|
195 |
+
plt.savefig(img_buf, format='png')
|
196 |
+
img_buf.seek(0)
|
197 |
+
img_base64 = base64.b64encode(img_buf.getvalue()).decode("utf-8")
|
198 |
+
img_buf.close()
|
199 |
+
plt.close()
|
200 |
+
|
201 |
+
df_formatado = df.copy()
|
202 |
+
for col in df.columns:
|
203 |
+
if col != "Ano":
|
204 |
+
df_formatado[col] = df_formatado[col].apply(formatar_brl)
|
205 |
+
tabela_html = df_formatado.to_html(index=False, border=0)
|
206 |
+
|
207 |
+
analise = gerar_analise(investimentos_finais, capital)
|
208 |
+
|
209 |
+
html = f"""
|
210 |
+
<html>
|
211 |
+
<head>
|
212 |
+
<meta charset="utf-8">
|
213 |
+
<style>
|
214 |
+
body {{ font-family: Arial, sans-serif; font-size: 14px; }}
|
215 |
+
h2 {{ color: #2c3e50; }}
|
216 |
+
table {{ width: 100%; border-collapse: collapse; margin-top: 20px; }}
|
217 |
+
th, td {{ border: 1px solid #ccc; padding: 6px; text-align: right; }}
|
218 |
+
th {{ background-color: #f0f0f0; }}
|
219 |
+
.destaque {{ background: #e8f5e9; padding: 10px; margin-top: 20px; border-left: 6px solid #2e7d32; }}
|
220 |
+
</style>
|
221 |
+
</head>
|
222 |
+
<body>
|
223 |
+
<h2>Relatório de Simulação de Investimentos</h2>
|
224 |
+
<p>Data da Simulação: {datetime.today().strftime('%d/%m/%Y')}</p>
|
225 |
+
<p>Capital Inicial: <strong>{formatar_brl(capital)}</strong></p>
|
226 |
+
<div class="destaque">{analise}</div>
|
227 |
+
<img src="data:image/png;base64,{img_base64}" width="100%" />
|
228 |
+
{tabela_html}
|
229 |
+
</body>
|
230 |
+
</html>
|
231 |
+
"""
|
232 |
+
|
233 |
+
pdf_buffer = io.BytesIO()
|
234 |
+
pisa_status = pisa.CreatePDF(io.StringIO(html), dest=pdf_buffer)
|
235 |
+
|
236 |
+
if pisa_status.err:
|
237 |
+
return "Erro ao gerar PDF", 500
|
238 |
+
|
239 |
+
pdf_buffer.seek(0)
|
240 |
+
return send_file(
|
241 |
+
pdf_buffer,
|
242 |
+
mimetype="application/pdf",
|
243 |
+
as_attachment=True,
|
244 |
+
download_name="relatorio_investimentos.pdf"
|
245 |
+
)
|
246 |
+
|
247 |
+
|
248 |
+
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
+
|
254 |
+
|
255 |
+
|
256 |
if __name__ == '__main__':
|
257 |
app.run(host='0.0.0.0', port=7860, debug=True)
|