Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# Step 1:
|
2 |
import gradio as gr
|
3 |
from fpdf import FPDF
|
4 |
import pandas as pd
|
@@ -12,34 +12,30 @@ def submit_form(name, whatsapp, membership, protest):
|
|
12 |
|
13 |
# Save CSV in /tmp/ folder (Hugging Face Spaces temporary storage)
|
14 |
csv_file = "/tmp/submitted_data.csv"
|
|
|
|
|
15 |
if not os.path.isfile(csv_file):
|
16 |
df.to_csv(csv_file, index=False)
|
17 |
else:
|
18 |
df.to_csv(csv_file, mode='a', header=False, index=False)
|
19 |
|
20 |
-
# Create a PDF object and add form data
|
21 |
pdf = FPDF()
|
22 |
pdf.add_page()
|
23 |
pdf.set_font("Arial", size=12)
|
24 |
-
pdf.cell(200, 10, txt=f"Name: {'_'*50}", ln=True)
|
25 |
-
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {'_'*40}", ln=True)
|
26 |
-
pdf.cell(200, 10, txt=f"Are you a member of YEP?: {'_'*30}", ln=True)
|
27 |
-
pdf.cell(200, 10, txt=f"Did you join the protest?: {'_'*35}", ln=True)
|
28 |
-
|
29 |
-
pdf.ln(10) # Line break for spacing
|
30 |
pdf.cell(200, 10, txt=f"Name: {name}", ln=True)
|
31 |
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {whatsapp}", ln=True)
|
32 |
pdf.cell(200, 10, txt=f"Membership of YEP: {membership}", ln=True)
|
33 |
pdf.cell(200, 10, txt=f"Participation in Protest: {protest}", ln=True)
|
34 |
|
35 |
# Save PDF to /tmp/ folder (backend storage)
|
36 |
-
pdf_output_path = "/tmp/
|
37 |
pdf.output(pdf_output_path)
|
38 |
|
39 |
-
# Return
|
40 |
-
return
|
41 |
|
42 |
-
# Step 3: Set up the Gradio interface
|
43 |
with gr.Blocks() as demo:
|
44 |
gr.Markdown("## Submit Your Information")
|
45 |
|
@@ -51,11 +47,11 @@ with gr.Blocks() as demo:
|
|
51 |
|
52 |
submit = gr.Button("Submit")
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
|
57 |
# Link the button to the function
|
58 |
-
submit.click(submit_form, inputs=[name, whatsapp, membership, protest], outputs=
|
59 |
|
60 |
# Step 4: Launch the Gradio app
|
61 |
demo.launch()
|
|
|
1 |
+
# Step 1: Install necessary libraries
|
2 |
import gradio as gr
|
3 |
from fpdf import FPDF
|
4 |
import pandas as pd
|
|
|
12 |
|
13 |
# Save CSV in /tmp/ folder (Hugging Face Spaces temporary storage)
|
14 |
csv_file = "/tmp/submitted_data.csv"
|
15 |
+
|
16 |
+
# Check if the file exists, append data if it does, otherwise create a new one
|
17 |
if not os.path.isfile(csv_file):
|
18 |
df.to_csv(csv_file, index=False)
|
19 |
else:
|
20 |
df.to_csv(csv_file, mode='a', header=False, index=False)
|
21 |
|
22 |
+
# Create a PDF object and add form data (backend storage only)
|
23 |
pdf = FPDF()
|
24 |
pdf.add_page()
|
25 |
pdf.set_font("Arial", size=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
pdf.cell(200, 10, txt=f"Name: {name}", ln=True)
|
27 |
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {whatsapp}", ln=True)
|
28 |
pdf.cell(200, 10, txt=f"Membership of YEP: {membership}", ln=True)
|
29 |
pdf.cell(200, 10, txt=f"Participation in Protest: {protest}", ln=True)
|
30 |
|
31 |
# Save PDF to /tmp/ folder (backend storage)
|
32 |
+
pdf_output_path = "/tmp/form_data_{}.pdf".format(name)
|
33 |
pdf.output(pdf_output_path)
|
34 |
|
35 |
+
# Return a success message without showing the PDF
|
36 |
+
return "Thank you! Your submission has been recorded."
|
37 |
|
38 |
+
# Step 3: Set up the Gradio interface
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("## Submit Your Information")
|
41 |
|
|
|
47 |
|
48 |
submit = gr.Button("Submit")
|
49 |
|
50 |
+
# Output message box for submission confirmation
|
51 |
+
output_message = gr.Textbox(label="Output Message", visible=True)
|
52 |
|
53 |
# Link the button to the function
|
54 |
+
submit.click(fn=submit_form, inputs=[name, whatsapp, membership, protest], outputs=output_message)
|
55 |
|
56 |
# Step 4: Launch the Gradio app
|
57 |
demo.launch()
|