LuisBlanche commited on
Commit
00aedc8
1 Parent(s): 4429d2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -18
app.py CHANGED
@@ -4,8 +4,6 @@ from io import BytesIO
4
  import mechanicalsoup
5
  import pandas as pd
6
  import requests
7
- from PIL import Image as PILImage
8
- from PIL import ImageDraw
9
  from reportlab.lib import colors
10
  from reportlab.lib.enums import TA_CENTER
11
  from reportlab.lib.pagesizes import A4
@@ -41,7 +39,7 @@ class PDFPoster:
41
  message_1: str = "Les votes de vos députés sont souvent différents de ce que les responsables de partis annoncent dans les médias. Les données de votes sont ouvertes!",
42
  message_2: str = "Les 30 juin, et 7 juin, renseignez vous, et votez en connaissance de cause !",
43
  ):
44
- df_subset = self.votes[self.votes["vote_topic"].isin(vote_list)]
45
 
46
  buffer = BytesIO()
47
  document = SimpleDocTemplate(buffer, pagesize=A4)
@@ -79,8 +77,8 @@ class PDFPoster:
79
  image.drawWidth = 5 * cm
80
 
81
  # Create a list of sentences
82
- sentences = df_subset["vote_topic"].tolist()
83
- votes = df_subset["for_or_against"].tolist()
84
 
85
  # Create the table data
86
  table_data = [["Sujet", "Vote"]]
@@ -280,14 +278,6 @@ css = """
280
  """
281
 
282
 
283
- def create_pdf_preview(pdf_buffer):
284
- # Create an image with PIL to simulate the first page of the PDF
285
- image = PILImage.new("RGB", (2480, 3508), color=(255, 255, 255)) # A4 at 300 dpi
286
- draw = ImageDraw.Draw(image)
287
- draw.text((10, 10), "This is a preview of the PDF content", fill=(0, 0, 0))
288
- return image
289
-
290
-
291
  def fetch_votes(deputy_name):
292
  pdfposter = PDFPoster(deputy_name)
293
  votes = pdfposter.retrieve_deputy_data()
@@ -312,10 +302,11 @@ def generate_poster(deputy_name, message_1, message_2, vote_list):
312
  with open(filename, "wb") as f:
313
  f.write(pdf_buffer.read())
314
 
315
- pdf_buffer.seek(0)
316
- preview_image = create_pdf_preview(pdf_buffer)
317
-
318
- return preview_image, filename
 
319
 
320
 
321
  with gr.Blocks(css=css) as demo:
@@ -352,6 +343,7 @@ with gr.Blocks(css=css) as demo:
352
 
353
  generate_button = gr.Button("Générer l'affiche ! ", scale=0)
354
  image_preview = gr.Image(label="Image")
 
355
  pdf_output = gr.File(label="Télécharger le PDF")
356
 
357
  fetch_button.click(fn=fetch_votes, inputs=deputy_name, outputs=vote_list)
@@ -359,7 +351,7 @@ with gr.Blocks(css=css) as demo:
359
  generate_button.click(
360
  fn=generate_poster,
361
  inputs=[deputy_name, message_1, message_2, vote_list],
362
- outputs=[image_preview, pdf_output],
363
  )
364
 
365
  demo.queue().launch()
 
4
  import mechanicalsoup
5
  import pandas as pd
6
  import requests
 
 
7
  from reportlab.lib import colors
8
  from reportlab.lib.enums import TA_CENTER
9
  from reportlab.lib.pagesizes import A4
 
39
  message_1: str = "Les votes de vos députés sont souvent différents de ce que les responsables de partis annoncent dans les médias. Les données de votes sont ouvertes!",
40
  message_2: str = "Les 30 juin, et 7 juin, renseignez vous, et votez en connaissance de cause !",
41
  ):
42
+ self.df_subset = self.votes[self.votes["vote_topic"].isin(vote_list)]
43
 
44
  buffer = BytesIO()
45
  document = SimpleDocTemplate(buffer, pagesize=A4)
 
77
  image.drawWidth = 5 * cm
78
 
79
  # Create a list of sentences
80
+ sentences = self.df_subset["vote_topic"].tolist()
81
+ votes = self.df_subset["for_or_against"].tolist()
82
 
83
  # Create the table data
84
  table_data = [["Sujet", "Vote"]]
 
278
  """
279
 
280
 
 
 
 
 
 
 
 
 
281
  def fetch_votes(deputy_name):
282
  pdfposter = PDFPoster(deputy_name)
283
  votes = pdfposter.retrieve_deputy_data()
 
302
  with open(filename, "wb") as f:
303
  f.write(pdf_buffer.read())
304
 
305
+ preview_df = pdfposter.df_subset
306
+ img_url = pdfposter.img_url
307
+ image_response = requests.get(img_url)
308
+ image_bytes = BytesIO(image_response.content)
309
+ return preview_df, image_bytes, filename
310
 
311
 
312
  with gr.Blocks(css=css) as demo:
 
343
 
344
  generate_button = gr.Button("Générer l'affiche ! ", scale=0)
345
  image_preview = gr.Image(label="Image")
346
+ df_preview = gr.Dataframe(label="Votes")
347
  pdf_output = gr.File(label="Télécharger le PDF")
348
 
349
  fetch_button.click(fn=fetch_votes, inputs=deputy_name, outputs=vote_list)
 
351
  generate_button.click(
352
  fn=generate_poster,
353
  inputs=[deputy_name, message_1, message_2, vote_list],
354
+ outputs=[df_preview, image_preview, pdf_output],
355
  )
356
 
357
  demo.queue().launch()