Spaces:
Sleeping
Sleeping
Update database_connection.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import requestsimport pandas as pdfrom typing import Dict, List, Optionalimport jsonclass DatabaseConnection:def __init__(self, base_url: str = "https://database-dhe2.onrender.com"):self.base_url = base_urlself.session = requests.Session()def get_student_feedback_counts(self) -> pd.DataFrame:"""Fetch student feedback data from the database"""try:url = f"{self.base_url}/student_feedback_counts"response = self.session.get(url)response.raise_for_status()data = response.json()if isinstance(data, list):return pd.DataFrame(data)else:return pd.DataFrame([data])except Exception as e:print(f"Error fetching data: {e}")return pd.DataFrame()def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,rating: int, hobbies: str) -> bool:"""Add new feedback to the database"""try:url = f"{self.base_url}/student_feedback_counts"data = {"course": course,"stanine": stanine,"gwa": gwa,"strand": strand,"rating": rating,"hobbies": hobbies}response = self.session.post(url, json=data)response.raise_for_status()return Trueexcept Exception as e:print(f"Error adding feedback: {e}")return Falsedef update_feedback_count(self, feedback_id: int, count: int) -> bool:"""Update the count for existing feedback"""try:url = f"{self.base_url}/student_feedback_counts/{feedback_id}"data = {"count": count}response = self.session.put(url, json=data)response.raise_for_status()return Trueexcept Exception as e:print(f"Error updating feedback count: {e}")return False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import requestsimport pandas as pdfrom typing import Dict, List, Optionalimport jsonclass DatabaseConnection:def __init__(self, base_url: str = "https://database-dhe2.onrender.com"):self.base_url = base_urlself.session = requests.Session()def get_student_feedback_counts(self) -> pd.DataFrame:"""Fetch student feedback data from the database"""try:url = f"{self.base_url}/student_feedback_counts"response = self.session.get(url)response.raise_for_status()data = response.json()if isinstance(data, list):return pd.DataFrame(data)elif isinstance(data, dict) and 'feedback_counts' in data:# Handle nested structurefeedback_data = data['feedback_counts']if isinstance(feedback_data, list):return pd.DataFrame(feedback_data)else:return pd.DataFrame([feedback_data])else:return pd.DataFrame([data])except Exception as e:print(f"Error fetching data: {e}")return pd.DataFrame()def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,rating: int, hobbies: str) -> bool:"""Add new feedback to the database"""try:url = f"{self.base_url}/student_feedback_counts"data = {"course": course,"stanine": stanine,"gwa": gwa,"strand": strand,"rating": rating,"hobbies": hobbies}response = self.session.post(url, json=data)response.raise_for_status()return Trueexcept Exception as e:print(f"Error adding feedback: {e}")return Falsedef update_feedback_count(self, feedback_id: int, count: int) -> bool:"""Update the count for existing feedback"""try:url = f"{self.base_url}/student_feedback_counts/{feedback_id}"data = {"count": count}response = self.session.put(url, json=data)response.raise_for_status()return Trueexcept Exception as e:print(f"Error updating feedback count: {e}")return Falsedef get_available_courses(self) -> List[str]:"""Fetch available courses from the database"""try:url = f"{self.base_url}/courses"response = self.session.get(url)response.raise_for_status()data = response.json()if isinstance(data, list):# Extract course names from the datacourses = []for item in data:if isinstance(item, dict) and 'name' in item:courses.append(item['name'])elif isinstance(item, str):courses.append(item)return courseselse:return []except Exception as e:print(f"Error fetching courses: {e}")return []
database_connection.py+87-57