Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,37 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
DATA_FILE = os.path.join("data", "teamup_data.json")
|
| 7 |
-
|
| 8 |
ADMIN_CODE = os.getenv("ADMIN_CODE", "")
|
| 9 |
|
| 10 |
-
|
| 11 |
# Ensure data file exists
|
| 12 |
os.makedirs("data", exist_ok=True)
|
| 13 |
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 |
def submit_profile(name, discord, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea):
|
| 18 |
print("π’ Submit button clicked.")
|
| 19 |
-
print(f"Incoming: {discord=}, {city=}, {country=}, {languages=}, {laptop=}, {robot=}")
|
| 20 |
-
|
| 21 |
if not discord or not city or not country or not laptop or not robot:
|
| 22 |
return "β Please fill in all required fields."
|
| 23 |
if not languages or not isinstance(languages, list) or len(languages) == 0:
|
| 24 |
return "β Please select at least one language."
|
| 25 |
|
| 26 |
-
# Ensure country is stored as a string
|
| 27 |
if isinstance(country, list):
|
| 28 |
country = country[0] if country else ""
|
| 29 |
|
|
@@ -70,111 +79,41 @@ def submit_profile(name, discord, city, country, address, looking, onlinecheck,
|
|
| 70 |
with open(DATA_FILE, "w") as f:
|
| 71 |
json.dump(data, f, indent=2)
|
| 72 |
print(f"β
Successfully wrote {len(data)} profiles to {DATA_FILE}")
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
print(f"β Failed to write to {DATA_FILE}: {e}")
|
| 75 |
return "β Error saving your profile. Please try again."
|
| 76 |
|
| 77 |
return "β
Profile saved!"
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
def
|
| 81 |
with open(DATA_FILE, "r") as f:
|
| 82 |
data = json.load(f)
|
| 83 |
df = pd.DataFrame(data)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
if df.empty or "Country" not in df.columns or "Languages" not in df.columns:
|
| 89 |
-
return "<p>No participants found.</p>"
|
| 90 |
-
|
| 91 |
-
if selected_country != "All":
|
| 92 |
-
df = df[df["Country"] == selected_country]
|
| 93 |
-
|
| 94 |
-
if selected_city != "All":
|
| 95 |
-
df = df[df["City"] == selected_city]
|
| 96 |
-
|
| 97 |
-
if selected_language != "All":
|
| 98 |
-
df = df[df["Languages"].apply(lambda langs: selected_language in langs)]
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# Hide address
|
| 102 |
-
if "Address" in df.columns:
|
| 103 |
-
df = df.drop(columns=["Address"])
|
| 104 |
-
|
| 105 |
-
# Rename column headers for display only
|
| 106 |
-
display_names = {
|
| 107 |
-
"Discord": "Discord",
|
| 108 |
-
"Name": "Name",
|
| 109 |
-
"City": "City",
|
| 110 |
-
"Country": "Country",
|
| 111 |
-
"Looking for Team": "Looking for Team",
|
| 112 |
-
"Onlinecheck": "How?",
|
| 113 |
-
"Languages": "Languages",
|
| 114 |
-
"Laptop": "Laptop",
|
| 115 |
-
"Robot": "Robot",
|
| 116 |
-
"Skills": "Skills",
|
| 117 |
-
"Describe3": "Describe",
|
| 118 |
-
"Experience": "Experience",
|
| 119 |
-
"Project Idea": "Project Idea"
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
html = '<h3 style="margin-top:20px;">π Find your matching Teammates & Register your team <a href="https://forms.gle/gJEMGD4CEA2emhD18" target="_blank">here</a>ππ</h3>'
|
| 123 |
-
html += "<table style='width:100%; border-collapse: collapse;'>"
|
| 124 |
-
|
| 125 |
-
# Header row
|
| 126 |
-
html += "<tr>" + "".join(
|
| 127 |
-
f"<th style='border: 1px solid #ccc; padding: 6px;'>{display_names.get(col, col)}</th>"
|
| 128 |
-
for col in df.columns
|
| 129 |
-
) + "</tr>"
|
| 130 |
-
|
| 131 |
-
# Data rows
|
| 132 |
-
for _, row in df.iterrows():
|
| 133 |
-
html += "<tr>"
|
| 134 |
-
for col in df.columns:
|
| 135 |
-
val = row[col]
|
| 136 |
-
if col == "Discord":
|
| 137 |
-
val = f"<a href='https://discord.com/users/{val}' target='_blank'>{val}</a>"
|
| 138 |
-
html += f"<td style='border: 1px solid #eee; padding: 6px;'>{val}</td>"
|
| 139 |
-
html += "</tr>"
|
| 140 |
-
|
| 141 |
-
html += "</table>"
|
| 142 |
-
return html
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
def update_dropdowns():
|
| 148 |
-
with open(DATA_FILE, "r") as f:
|
| 149 |
-
data = json.load(f)
|
| 150 |
-
if not data:
|
| 151 |
-
return ["All"], ["All"], ["All"]
|
| 152 |
-
df = pd.DataFrame(data)
|
| 153 |
-
if "Country" not in df.columns or "Languages" not in df.columns or "City" not in df.columns:
|
| 154 |
-
return ["All"], ["All"], ["All"]
|
| 155 |
-
country_choices = sorted(list(df["Country"].dropna().unique()))
|
| 156 |
-
city_choices = sorted(list(df["City"].dropna().unique()))
|
| 157 |
language_set = set()
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
return (
|
| 161 |
-
["All"] + country_choices,
|
| 162 |
-
["All"] +
|
| 163 |
-
["All"] +
|
| 164 |
)
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
def delete_by_discord(discord, code):
|
| 169 |
-
if code != ADMIN_CODE:
|
| 170 |
-
return "β Invalid admin code."
|
| 171 |
-
with open(DATA_FILE, "r") as f:
|
| 172 |
-
data = json.load(f)
|
| 173 |
-
new_data = [d for d in data if d["Discord"].lower() != discord.lower()]
|
| 174 |
-
with open(DATA_FILE, "w") as f:
|
| 175 |
-
json.dump(new_data, f, indent=2)
|
| 176 |
-
return f"ποΈ Deleted user with Discord: {discord}"
|
| 177 |
-
|
| 178 |
with gr.Blocks() as demo:
|
| 179 |
gr.Markdown("# π LeRobot Worldwide Hackathon - Team-Up Dashboard")
|
| 180 |
gr.Markdown("1. Submit or update your profile to find matching teammates and contact them on Discord. (Required fields marked with *.) ")
|
|
|
|
| 2 |
import json
|
| 3 |
import pandas as pd
|
| 4 |
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
|
| 12 |
os.makedirs("data", exist_ok=True)
|
| 13 |
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 |
+
# Profile submission logic
|
| 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 |
|
|
|
|
| 79 |
with open(DATA_FILE, "w") as f:
|
| 80 |
json.dump(data, f, indent=2)
|
| 81 |
print(f"β
Successfully wrote {len(data)} profiles to {DATA_FILE}")
|
| 82 |
+
backup_data() # Backup after every update
|
| 83 |
except Exception as e:
|
| 84 |
print(f"β Failed to write to {DATA_FILE}: {e}")
|
| 85 |
return "β Error saving your profile. Please try again."
|
| 86 |
|
| 87 |
return "β
Profile saved!"
|
| 88 |
|
| 89 |
+
# Dropdown population functions
|
| 90 |
+
def update_country_choices():
|
| 91 |
+
country_choices = [
|
| 92 |
+
"United States", "Canada", "United Kingdom", "India", "Germany", "France",
|
| 93 |
+
"Australia", "Brazil", "Mexico", "Spain", "Italy", "China", "Russia", "Japan"
|
| 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 update_country_choices()
|
| 103 |
+
city_choices = sorted(df["City"].dropna().unique()) if "City" in df else []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
language_set = set()
|
| 105 |
+
if "Languages" in df:
|
| 106 |
+
for lang_list in df["Languages"].dropna():
|
| 107 |
+
if isinstance(lang_list, list):
|
| 108 |
+
language_set.update(lang_list)
|
| 109 |
+
elif isinstance(lang_list, str):
|
| 110 |
+
language_set.update(lang_list.split(", "))
|
| 111 |
return (
|
| 112 |
+
gr.update(choices=["All"] + list(country_choices), value="All"),
|
| 113 |
+
gr.update(choices=["All"] + list(city_choices), value="All"),
|
| 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 *.) ")
|