Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,10 @@ import os
|
|
5 |
import shutil
|
6 |
import datetime
|
7 |
|
|
|
8 |
DATA_FILE = os.path.join("data", "teamup_data.json")
|
|
|
|
|
9 |
ADMIN_CODE = os.getenv("ADMIN_CODE", "")
|
10 |
|
11 |
# Ensure data file exists
|
@@ -14,25 +17,29 @@ if not os.path.exists(DATA_FILE) or os.path.getsize(DATA_FILE) == 0:
|
|
14 |
with open(DATA_FILE, "w") as f:
|
15 |
json.dump([], f)
|
16 |
|
17 |
-
# Function to create a backup
|
18 |
def backup_data():
|
19 |
source_file = DATA_FILE
|
20 |
backup_dir = './data/backup'
|
21 |
os.makedirs(backup_dir, exist_ok=True)
|
|
|
22 |
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
23 |
backup_file = os.path.join(backup_dir, f'teamup_data_backup_{timestamp}.json')
|
|
|
24 |
shutil.copy(source_file, backup_file)
|
25 |
print(f"β
Backup created at {backup_file}")
|
26 |
|
27 |
-
#
|
28 |
def submit_profile(name, discord, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea):
|
29 |
print("π’ Submit button clicked.")
|
|
|
|
|
30 |
if not discord or not city or not country or not laptop or not robot:
|
31 |
return "β Please fill in all required fields."
|
32 |
if not languages or not isinstance(languages, list) or len(languages) == 0:
|
33 |
return "β Please select at least one language."
|
34 |
|
35 |
-
# Ensure country is stored as a string
|
36 |
if isinstance(country, list):
|
37 |
country = country[0] if country else ""
|
38 |
|
@@ -86,20 +93,77 @@ def submit_profile(name, discord, city, country, address, looking, onlinecheck,
|
|
86 |
|
87 |
return "β
Profile saved!"
|
88 |
|
89 |
-
#
|
90 |
-
def
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
]
|
95 |
-
return country_choices
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
def update_dropdown_choices():
|
98 |
with open(DATA_FILE, "r") as f:
|
99 |
data = json.load(f)
|
100 |
df = pd.DataFrame(data)
|
101 |
|
102 |
-
country_choices = sorted(df["Country"].dropna().unique()) if "Country" in df else
|
103 |
city_choices = sorted(df["City"].dropna().unique()) if "City" in df else []
|
104 |
language_set = set()
|
105 |
if "Languages" in df:
|
@@ -114,10 +178,12 @@ def update_dropdown_choices():
|
|
114 |
gr.update(choices=["All"] + sorted(language_set), value="All")
|
115 |
)
|
116 |
|
|
|
117 |
with gr.Blocks() as demo:
|
118 |
gr.Markdown("# π LeRobot Worldwide Hackathon - Team-Up Dashboard")
|
119 |
gr.Markdown("1. Submit or update your profile to find matching teammates and contact them on Discord. (Required fields marked with *.) ")
|
120 |
|
|
|
121 |
with gr.Row():
|
122 |
with gr.Column():
|
123 |
name = gr.Text(label="Name")
|
@@ -150,68 +216,8 @@ with gr.Blocks() as demo:
|
|
150 |
outputs=[status]
|
151 |
)
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
def update_dropdown_choices():
|
156 |
-
with open(DATA_FILE, "r") as f:
|
157 |
-
data = json.load(f)
|
158 |
-
df = pd.DataFrame(data)
|
159 |
-
|
160 |
-
country_choices = sorted(df["Country"].dropna().unique()) if "Country" in df else []
|
161 |
-
city_choices = sorted(df["City"].dropna().unique()) if "City" in df else []
|
162 |
-
language_set = set()
|
163 |
-
if "Languages" in df:
|
164 |
-
for lang_list in df["Languages"].dropna():
|
165 |
-
if isinstance(lang_list, list):
|
166 |
-
language_set.update(lang_list)
|
167 |
-
elif isinstance(lang_list, str):
|
168 |
-
language_set.update(lang_list.split(", "))
|
169 |
-
return (
|
170 |
-
gr.update(choices=["All"] + list(country_choices), value="All"),
|
171 |
-
gr.update(choices=["All"] + list(city_choices), value="All"),
|
172 |
-
gr.update(choices=["All"] + sorted(language_set), value="All")
|
173 |
-
)
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
def download_csv(code):
|
178 |
-
if code != ADMIN_CODE:
|
179 |
-
raise gr.Error("β Invalid admin code.")
|
180 |
-
with open(DATA_FILE, "r") as f:
|
181 |
-
data = json.load(f)
|
182 |
-
df = pd.DataFrame(data)
|
183 |
-
csv_path = os.path.join("data", "teamup_export.csv")
|
184 |
-
df.to_csv(csv_path, index=False)
|
185 |
-
return csv_path
|
186 |
-
|
187 |
-
with demo:
|
188 |
-
demo.load(
|
189 |
-
fn=lambda: filter_by_fields("All", "All", "All"),
|
190 |
-
inputs=[],
|
191 |
-
outputs=[table_html]
|
192 |
-
)
|
193 |
-
|
194 |
-
demo.load(fn=update_dropdown_choices, outputs=[country_filter, city_filter, language_filter])
|
195 |
-
|
196 |
country_filter.change(fn=filter_by_fields, inputs=[country_filter, city_filter, language_filter], outputs=table_html)
|
197 |
city_filter.change(fn=filter_by_fields, inputs=[country_filter, city_filter, language_filter], outputs=table_html)
|
198 |
language_filter.change(fn=filter_by_fields, inputs=[country_filter, city_filter, language_filter], outputs=table_html)
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
gr.Markdown("---\n### π‘οΈ Admin Panel (delete by Discord)")
|
203 |
-
admin_discord = gr.Text(label="Discord Username")
|
204 |
-
admin_code = gr.Text(label="Admin Code", type="password")
|
205 |
-
del_btn = gr.Button("Delete Profile")
|
206 |
-
del_status = gr.Textbox(label="Status", interactive=False)
|
207 |
-
del_btn.click(delete_by_discord, inputs=[admin_discord, admin_code], outputs=del_status)
|
208 |
-
|
209 |
-
# π CSV Download Section (admin-only)
|
210 |
-
gr.Markdown("---\n### π₯ Admin Export CSV")
|
211 |
-
export_code = gr.Text(label="Admin Code", type="password")
|
212 |
-
download_btn = gr.Button("Generate and Download CSV")
|
213 |
-
download_file = gr.File(label="CSV Export", interactive=False)
|
214 |
-
download_btn.click(fn=download_csv, inputs=[export_code], outputs=[download_file])
|
215 |
-
|
216 |
demo.launch()
|
217 |
-
|
|
|
5 |
import shutil
|
6 |
import datetime
|
7 |
|
8 |
+
# Path to the data file
|
9 |
DATA_FILE = os.path.join("data", "teamup_data.json")
|
10 |
+
|
11 |
+
# Load the ADMIN_CODE environment variable
|
12 |
ADMIN_CODE = os.getenv("ADMIN_CODE", "")
|
13 |
|
14 |
# Ensure data file exists
|
|
|
17 |
with open(DATA_FILE, "w") as f:
|
18 |
json.dump([], f)
|
19 |
|
20 |
+
# Function to create a backup of the data
|
21 |
def backup_data():
|
22 |
source_file = DATA_FILE
|
23 |
backup_dir = './data/backup'
|
24 |
os.makedirs(backup_dir, exist_ok=True)
|
25 |
+
|
26 |
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
27 |
backup_file = os.path.join(backup_dir, f'teamup_data_backup_{timestamp}.json')
|
28 |
+
|
29 |
shutil.copy(source_file, backup_file)
|
30 |
print(f"β
Backup created at {backup_file}")
|
31 |
|
32 |
+
# Function to submit or update a profile
|
33 |
def submit_profile(name, discord, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea):
|
34 |
print("π’ Submit button clicked.")
|
35 |
+
print(f"Incoming: {discord=}, {city=}, {country=}, {languages=}, {laptop=}, {robot=}")
|
36 |
+
|
37 |
if not discord or not city or not country or not laptop or not robot:
|
38 |
return "β Please fill in all required fields."
|
39 |
if not languages or not isinstance(languages, list) or len(languages) == 0:
|
40 |
return "β Please select at least one language."
|
41 |
|
42 |
+
# Ensure country is stored as a string (in case form allows lists)
|
43 |
if isinstance(country, list):
|
44 |
country = country[0] if country else ""
|
45 |
|
|
|
93 |
|
94 |
return "β
Profile saved!"
|
95 |
|
96 |
+
# Function to filter participants by fields
|
97 |
+
def filter_by_fields(selected_country, selected_city, selected_language):
|
98 |
+
with open(DATA_FILE, "r") as f:
|
99 |
+
data = json.load(f)
|
100 |
+
df = pd.DataFrame(data)
|
|
|
|
|
101 |
|
102 |
+
# Convert list values to readable strings
|
103 |
+
df["Languages"] = df["Languages"].apply(lambda langs: ", ".join(langs) if isinstance(langs, list) else langs)
|
104 |
+
|
105 |
+
if df.empty or "Country" not in df.columns or "Languages" not in df.columns:
|
106 |
+
return "<p>No participants found.</p>"
|
107 |
+
|
108 |
+
if selected_country != "All":
|
109 |
+
df = df[df["Country"] == selected_country]
|
110 |
+
|
111 |
+
if selected_city != "All":
|
112 |
+
df = df[df["City"] == selected_city]
|
113 |
+
|
114 |
+
if selected_language != "All":
|
115 |
+
df = df[df["Languages"].apply(lambda langs: selected_language in langs)]
|
116 |
+
|
117 |
+
# Hide address
|
118 |
+
if "Address" in df.columns:
|
119 |
+
df = df.drop(columns=["Address"])
|
120 |
+
|
121 |
+
# Rename column headers for display only
|
122 |
+
display_names = {
|
123 |
+
"Discord": "Discord",
|
124 |
+
"Name": "Name",
|
125 |
+
"City": "City",
|
126 |
+
"Country": "Country",
|
127 |
+
"Looking for Team": "Looking for Team",
|
128 |
+
"Onlinecheck": "How?",
|
129 |
+
"Languages": "Languages",
|
130 |
+
"Laptop": "Laptop",
|
131 |
+
"Robot": "Robot",
|
132 |
+
"Skills": "Skills",
|
133 |
+
"Describe3": "Describe",
|
134 |
+
"Experience": "Experience",
|
135 |
+
"Project Idea": "Project Idea"
|
136 |
+
}
|
137 |
+
|
138 |
+
html = '<h3 style="margin-top:20px;">π Find your matching Teammates & Register your team <a href="https://forms.gle/gJEMGD4CEA2emhD18" target="_blank">here</a>ππ</h3>'
|
139 |
+
html += "<table style='width:100%; border-collapse: collapse;'>"
|
140 |
+
|
141 |
+
# Header row
|
142 |
+
html += "<tr>" + "".join(
|
143 |
+
f"<th style='border: 1px solid #ccc; padding: 6px;'>{display_names.get(col, col)}</th>"
|
144 |
+
for col in df.columns
|
145 |
+
) + "</tr>"
|
146 |
+
|
147 |
+
# Data rows
|
148 |
+
for _, row in df.iterrows():
|
149 |
+
html += "<tr>"
|
150 |
+
for col in df.columns:
|
151 |
+
val = row[col]
|
152 |
+
if col == "Discord":
|
153 |
+
val = f"<a href='https://discord.com/users/{val}' target='_blank'>{val}</a>"
|
154 |
+
html += f"<td style='border: 1px solid #eee; padding: 6px;'>{val}</td>"
|
155 |
+
html += "</tr>"
|
156 |
+
|
157 |
+
html += "</table>"
|
158 |
+
return html
|
159 |
+
|
160 |
+
# Function to update the dropdown choices
|
161 |
def update_dropdown_choices():
|
162 |
with open(DATA_FILE, "r") as f:
|
163 |
data = json.load(f)
|
164 |
df = pd.DataFrame(data)
|
165 |
|
166 |
+
country_choices = sorted(df["Country"].dropna().unique()) if "Country" in df else []
|
167 |
city_choices = sorted(df["City"].dropna().unique()) if "City" in df else []
|
168 |
language_set = set()
|
169 |
if "Languages" in df:
|
|
|
178 |
gr.update(choices=["All"] + sorted(language_set), value="All")
|
179 |
)
|
180 |
|
181 |
+
# Setup Gradio interface
|
182 |
with gr.Blocks() as demo:
|
183 |
gr.Markdown("# π LeRobot Worldwide Hackathon - Team-Up Dashboard")
|
184 |
gr.Markdown("1. Submit or update your profile to find matching teammates and contact them on Discord. (Required fields marked with *.) ")
|
185 |
|
186 |
+
# Input fields and buttons setup
|
187 |
with gr.Row():
|
188 |
with gr.Column():
|
189 |
name = gr.Text(label="Name")
|
|
|
216 |
outputs=[status]
|
217 |
)
|
218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
country_filter.change(fn=filter_by_fields, inputs=[country_filter, city_filter, language_filter], outputs=table_html)
|
220 |
city_filter.change(fn=filter_by_fields, inputs=[country_filter, city_filter, language_filter], outputs=table_html)
|
221 |
language_filter.change(fn=filter_by_fields, inputs=[country_filter, city_filter, language_filter], outputs=table_html)
|
222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
demo.launch()
|
|