import gradio as gr import json import pandas as pd import os import shutil import datetime # Path to the data file DATA_FILE = os.path.join("data", "teamup_data.json") # Load the ADMIN_CODE environment variable ADMIN_CODE = os.getenv("ADMIN_CODE", "") # Ensure data file exists os.makedirs("data", exist_ok=True) if not os.path.exists(DATA_FILE) or os.path.getsize(DATA_FILE) == 0: with open(DATA_FILE, "w") as f: json.dump([], f) # Function to create a backup of the data def backup_data(): source_file = DATA_FILE backup_dir = './data/backup' os.makedirs(backup_dir, exist_ok=True) timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S') backup_file = os.path.join(backup_dir, f'teamup_data_backup_{timestamp}.json') shutil.copy(source_file, backup_file) print(f"✅ Backup created at {backup_file}") # Function to submit or update a profile def submit_profile(name, discord, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea): print("🟢 Submit button clicked.") print(f"Incoming: {discord=}, {city=}, {country=}, {languages=}, {laptop=}, {robot=}") if not discord or not city or not country or not laptop or not robot: return "❌ Please fill in all required fields." if not languages or not isinstance(languages, list) or len(languages) == 0: return "❌ Please select at least one language." # Ensure country is stored as a string (in case form allows lists) if isinstance(country, list): country = country[0] if country else "" with open(DATA_FILE, "r") as f: data = json.load(f) for d in data: if d["Discord"].lower() == discord.lower(): d.update({ "Name": name, "City": city, "Country": country, "Address": address, "Looking for Team": looking, "Onlinecheck": onlinecheck, "Languages": languages, "Laptop": laptop, "Robot": robot, "Skills": skills, "Describe3": describe3, "Experience": experience, "Project Idea": idea }) break else: data.append({ "Name": name, "Discord": discord, "City": city, "Country": country, "Address": address, "Looking for Team": looking, "Onlinecheck": onlinecheck, "Languages": languages, "Laptop": laptop, "Robot": robot, "Skills": skills, "Describe3": describe3, "Experience": experience, "Project Idea": idea }) try: with open(DATA_FILE, "w") as f: json.dump(data, f, indent=2) print(f"✅ Successfully wrote {len(data)} profiles to {DATA_FILE}") backup_data() # Backup after every update except Exception as e: print(f"❌ Failed to write to {DATA_FILE}: {e}") return "❌ Error saving your profile. Please try again." return "✅ Profile saved!" # Function to filter participants by fields def filter_by_fields(selected_country, selected_city, selected_language): with open(DATA_FILE, "r") as f: data = json.load(f) df = pd.DataFrame(data) # Convert list values to readable strings df["Languages"] = df["Languages"].apply(lambda langs: ", ".join(langs) if isinstance(langs, list) else langs) if df.empty or "Country" not in df.columns or "Languages" not in df.columns: return "
No participants found.
" if selected_country != "All": df = df[df["Country"] == selected_country] if selected_city != "All": df = df[df["City"] == selected_city] if selected_language != "All": df = df[df["Languages"].apply(lambda langs: selected_language in langs)] # Hide address if "Address" in df.columns: df = df.drop(columns=["Address"]) # Rename column headers for display only display_names = { "Discord": "Discord", "Name": "Name", "City": "City", "Country": "Country", "Looking for Team": "Looking for Team", "Onlinecheck": "How?", "Languages": "Languages", "Laptop": "Laptop", "Robot": "Robot", "Skills": "Skills", "Describe3": "Describe", "Experience": "Experience", "Project Idea": "Project Idea" } html = '| {display_names.get(col, col)} | " for col in df.columns ) + "
|---|
| {val} | " html += "