Persano commited on
Commit
f6d248f
·
verified ·
1 Parent(s): 939ff54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -103,10 +103,11 @@ def index():
103
 
104
  return render_template("index.html")
105
 
 
 
 
106
  @app.route("/download_pdf", methods=["POST"])
107
  def download_pdf():
108
- from flask import render_template_string
109
-
110
  capital = float(request.form["capital"])
111
  studio_ret = float(request.form["studio_ret"])
112
  valorizacao = float(request.form["valorizacao"])
@@ -142,7 +143,7 @@ def download_pdf():
142
  investimento_mais_valorizado = max(investimentos_finais, key=investimentos_finais.get)
143
  valor_mais_alto = investimentos_finais[investimento_mais_valorizado]
144
 
145
- # Gráfico
146
  plt.figure(figsize=(10, 6))
147
  plt.plot(anos, studio, label="Studio", marker="o")
148
  plt.plot(anos, franquia, label="Franquia", marker="o")
@@ -163,31 +164,35 @@ def download_pdf():
163
  buf.close()
164
  plt.close()
165
 
166
- tabela_html = df.to_html(index=False, classes="table table-striped", float_format="R${:,.2f}".format)
167
-
168
- rendered_html = render_template("index.html",
169
- capital=capital,
170
- studio_ret=studio_ret,
171
- valorizacao=valorizacao,
172
- franquia_ret=franquia_ret,
173
- acoes_ret=acoes_ret,
174
- renda_fixa=renda_fixa,
175
- inflacao=inflacao,
176
- grafico=grafico_base64,
177
- tabela=tabela_html,
178
- investimento_mais_valorizado=investimento_mais_valorizado,
179
- valor_mais_alto=valor_mais_alto,
180
- gerar_pdf=True)
 
 
181
 
182
  pdf = io.BytesIO()
183
- pisa.CreatePDF(io.StringIO(rendered_html), dest=pdf)
184
  pdf.seek(0)
 
 
 
 
185
  return send_file(pdf, as_attachment=True, download_name="relatorio_simulacao.pdf", mimetype="application/pdf")
186
 
187
 
188
 
189
- if __name__ == '__main__':
190
- app.run(host='0.0.0.0', port=7860, debug=True)
191
 
192
 
193
 
@@ -196,3 +201,7 @@ if __name__ == '__main__':
196
 
197
 
198
 
 
 
 
 
 
103
 
104
  return render_template("index.html")
105
 
106
+
107
+
108
+
109
  @app.route("/download_pdf", methods=["POST"])
110
  def download_pdf():
 
 
111
  capital = float(request.form["capital"])
112
  studio_ret = float(request.form["studio_ret"])
113
  valorizacao = float(request.form["valorizacao"])
 
143
  investimento_mais_valorizado = max(investimentos_finais, key=investimentos_finais.get)
144
  valor_mais_alto = investimentos_finais[investimento_mais_valorizado]
145
 
146
+ # Gerar gráfico (base64)
147
  plt.figure(figsize=(10, 6))
148
  plt.plot(anos, studio, label="Studio", marker="o")
149
  plt.plot(anos, franquia, label="Franquia", marker="o")
 
164
  buf.close()
165
  plt.close()
166
 
167
+ tabela_html = df.to_html(index=False, classes="", float_format="R${:,.2f}".format)
168
+
169
+ # Renderizar template simplificado para PDF
170
+ rendered_html = render_template(
171
+ "relatorio_pdf.html",
172
+ capital=capital,
173
+ studio_ret=studio_ret,
174
+ valorizacao=valorizacao,
175
+ franquia_ret=franquia_ret,
176
+ acoes_ret=acoes_ret,
177
+ renda_fixa=renda_fixa,
178
+ inflacao=inflacao,
179
+ grafico=grafico_base64,
180
+ tabela=tabela_html,
181
+ investimento_mais_valorizado=investimento_mais_valorizado,
182
+ valor_mais_alto=valor_mais_alto,
183
+ )
184
 
185
  pdf = io.BytesIO()
186
+ pisa_status = pisa.CreatePDF(rendered_html, dest=pdf)
187
  pdf.seek(0)
188
+
189
+ if pisa_status.err:
190
+ return "Erro ao gerar PDF", 500
191
+
192
  return send_file(pdf, as_attachment=True, download_name="relatorio_simulacao.pdf", mimetype="application/pdf")
193
 
194
 
195
 
 
 
196
 
197
 
198
 
 
201
 
202
 
203
 
204
+
205
+ if __name__ == '__main__':
206
+ app.run(host='0.0.0.0', port=7860, debug=True)
207
+