LuisBlanche commited on
Commit
2473a5d
1 Parent(s): 306834c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -23,6 +23,8 @@ from reportlab.platypus import (
23
  )
24
  from unidecode import unidecode
25
  import base64
 
 
26
 
27
 
28
 
@@ -45,8 +47,8 @@ class PDFPoster:
45
  ):
46
  df_subset = self.votes[self.votes["vote_topic"].isin(vote_list)]
47
 
48
- pdf_filename = f"/data/{self.deputy_name}.pdf"
49
- document = SimpleDocTemplate(pdf_filename, pagesize=A4)
50
 
51
  # Set up the styles
52
  styles = getSampleStyleSheet()
@@ -161,11 +163,8 @@ class PDFPoster:
161
  buffer = BytesIO()
162
 
163
  buffer.seek(0)
164
- pdf_data = buffer.read()
165
- buffer.close()
166
- pdf_base64 = base64.b64encode(pdf_data).decode('utf-8')
167
 
168
- return pdf_base64, pdf_filename
169
 
170
  def get_deputy_votes_page(self):
171
  """Fetches the webpage containing the voting records of a specified deputy.
@@ -305,12 +304,16 @@ def generate_poster(deputy_name, message_1, message_2, vote_list):
305
 
306
  pdfposter = PDFPoster(deputy_name)
307
  pdfposter.retrieve_deputy_data()
308
- pdf_base64, pdf_filename = pdfposter.generate_poster(vote_list, message_1, message_2)
 
 
 
 
 
 
 
309
 
310
-
311
- # Create the iframe HTML to display the PDF
312
- iframe_html = f'<iframe src="data:application/pdf;base64,{pdf_base64}" width="100%" height="800px"></iframe>'
313
- return gr.HTML(iframe_html), pdf_filename
314
 
315
  with gr.Blocks(css=css) as demo:
316
 
@@ -346,8 +349,8 @@ with gr.Blocks(css=css) as demo:
346
  )
347
 
348
  generate_button = gr.Button("Générer l'affiche ! ", scale=0)
349
- result = gr.HTML()
350
- download = gr.File()
351
 
352
  fetch_button.click(
353
  fn=fetch_votes,
@@ -358,7 +361,7 @@ with gr.Blocks(css=css) as demo:
358
  generate_button.click(
359
  fn=generate_poster,
360
  inputs=[deputy_name, message_1, message_2, vote_list],
361
- outputs=[result, download]
362
  )
363
 
364
  demo.queue().launch()
 
23
  )
24
  from unidecode import unidecode
25
  import base64
26
+ from pdf2image import convert_from_bytes
27
+
28
 
29
 
30
 
 
47
  ):
48
  df_subset = self.votes[self.votes["vote_topic"].isin(vote_list)]
49
 
50
+ buffer = BytesIO()
51
+ document = SimpleDocTemplate(buffer, pagesize=A4)
52
 
53
  # Set up the styles
54
  styles = getSampleStyleSheet()
 
163
  buffer = BytesIO()
164
 
165
  buffer.seek(0)
 
 
 
166
 
167
+ return pdf_base64
168
 
169
  def get_deputy_votes_page(self):
170
  """Fetches the webpage containing the voting records of a specified deputy.
 
304
 
305
  pdfposter = PDFPoster(deputy_name)
306
  pdfposter.retrieve_deputy_data()
307
+ pdfbuffer = pdfposter.generate_poster(vote_list, message_1, message_2)
308
+ images = convert_from_bytes(pdf_buffer.getvalue())
309
+
310
+ image_paths = []
311
+ for i, image in enumerate(images):
312
+ image_path = f"./static/{deputy_name}_page_{i+1}.png"
313
+ image.save(image_path, "PNG")
314
+ image_paths.append(image_path)
315
 
316
+ return image_paths, pdf_buffer.getvalue()
 
 
 
317
 
318
  with gr.Blocks(css=css) as demo:
319
 
 
349
  )
350
 
351
  generate_button = gr.Button("Générer l'affiche ! ", scale=0)
352
+ images_output = gr.Gallery(label="Image")
353
+ pdf_output = gr.File(label="Télécharger le PDF")
354
 
355
  fetch_button.click(
356
  fn=fetch_votes,
 
361
  generate_button.click(
362
  fn=generate_poster,
363
  inputs=[deputy_name, message_1, message_2, vote_list],
364
+ outputs=[images_output, pdf_output]
365
  )
366
 
367
  demo.queue().launch()