Spaces:
Sleeping
Sleeping
raymondEDS
commited on
Commit
Β·
7a4f631
1
Parent(s):
3cc080f
setting up DB
Browse files- README.md +59 -25
- requirements.txt +2 -1
- src/auth.py +39 -23
- tests/__init__.py +1 -0
- tests/test_database.py +118 -0
README.md
CHANGED
|
@@ -21,10 +21,10 @@ A comprehensive Learning Management System built with Streamlit specifically des
|
|
| 21 |
## π Features
|
| 22 |
|
| 23 |
### π Authentication
|
| 24 |
-
- Secure student login system
|
| 25 |
- Individual student accounts
|
| 26 |
-
- Session management
|
| 27 |
-
-
|
| 28 |
|
| 29 |
### π Course Content
|
| 30 |
- Week-by-week structured learning materials
|
|
@@ -67,18 +67,40 @@ The system includes the following student accounts:
|
|
| 67 |
pip install -r requirements.txt
|
| 68 |
```
|
| 69 |
|
| 70 |
-
2. **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
```bash
|
| 72 |
streamlit run src/main.py
|
| 73 |
```
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
## π Application Structure
|
| 84 |
|
|
@@ -93,11 +115,14 @@ The application is optimized for deployment on Hugging Face Spaces and other clo
|
|
| 93 |
β βββ future_weeks.py # Weeks 2-4 placeholder content
|
| 94 |
β βββ assignments.py # Assignments page
|
| 95 |
β βββ resources.py # Resources page
|
| 96 |
-
βββ
|
| 97 |
-
β
|
| 98 |
-
β
|
|
|
|
|
|
|
| 99 |
βββ .streamlit/
|
| 100 |
-
β
|
|
|
|
| 101 |
βββ requirements.txt # Python dependencies
|
| 102 |
βββ Dockerfile # Container configuration
|
| 103 |
βββ README.md # This file
|
|
@@ -132,9 +157,9 @@ The application is optimized for deployment on Hugging Face Spaces and other clo
|
|
| 132 |
## π§ Technical Details
|
| 133 |
|
| 134 |
- **Framework:** Streamlit
|
| 135 |
-
- **
|
| 136 |
-
- **
|
| 137 |
-
- **
|
| 138 |
- **Deployment:** Optimized for Hugging Face Spaces
|
| 139 |
- **Architecture:** Modular design with separate view modules
|
| 140 |
|
|
@@ -147,27 +172,36 @@ The application is optimized for deployment on Hugging Face Spaces and other clo
|
|
| 147 |
|
| 148 |
## π Security Features
|
| 149 |
|
| 150 |
-
-
|
| 151 |
-
-
|
| 152 |
- User role management
|
| 153 |
- Secure file upload handling
|
| 154 |
|
| 155 |
## βοΈ Cloud Features
|
| 156 |
|
| 157 |
-
- **
|
| 158 |
-
- **
|
| 159 |
-
- **
|
| 160 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
## π Future Enhancements
|
| 163 |
|
| 164 |
-
- Database integration for persistent storage
|
| 165 |
- Real-time debate practice sessions
|
| 166 |
- Video conferencing integration
|
| 167 |
- Advanced progress analytics
|
| 168 |
- Mobile-responsive design
|
| 169 |
- Debate tournament management
|
|
|
|
| 170 |
|
| 171 |
---
|
| 172 |
|
| 173 |
-
**Built with β€οΈ using Streamlit | Cloud-ready for Hugging Face Spaces**
|
|
|
|
| 21 |
## π Features
|
| 22 |
|
| 23 |
### π Authentication
|
| 24 |
+
- Secure student login system with Supabase database
|
| 25 |
- Individual student accounts
|
| 26 |
+
- Session management
|
| 27 |
+
- Custom authentication system
|
| 28 |
|
| 29 |
### π Course Content
|
| 30 |
- Week-by-week structured learning materials
|
|
|
|
| 67 |
pip install -r requirements.txt
|
| 68 |
```
|
| 69 |
|
| 70 |
+
2. **Set up Supabase:**
|
| 71 |
+
- Create a Supabase project
|
| 72 |
+
- Run the SQL script in `database/setup.sql`
|
| 73 |
+
- Add your credentials to `.streamlit/secrets.toml`
|
| 74 |
+
|
| 75 |
+
3. **Run the Application:**
|
| 76 |
```bash
|
| 77 |
streamlit run src/main.py
|
| 78 |
```
|
| 79 |
|
| 80 |
+
4. **Test Database Connection (Optional):**
|
| 81 |
+
```bash
|
| 82 |
+
streamlit run tests/test_database.py
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### Hugging Face Spaces Deployment
|
| 86 |
+
|
| 87 |
+
1. **Fork this repository** to your GitHub account
|
| 88 |
+
|
| 89 |
+
2. **Create a new Space on Hugging Face:**
|
| 90 |
+
- Go to [huggingface.co/spaces](https://huggingface.co/spaces)
|
| 91 |
+
- Click "Create new Space"
|
| 92 |
+
- Choose "Docker" as the SDK
|
| 93 |
+
- Link to your forked repository
|
| 94 |
|
| 95 |
+
3. **Add Supabase Secrets:**
|
| 96 |
+
- Go to your Space settings
|
| 97 |
+
- Add these secrets:
|
| 98 |
+
- `SUPABASE_URL`: Your Supabase project URL
|
| 99 |
+
- `SUPABASE_KEY`: Your Supabase anon public key
|
| 100 |
+
|
| 101 |
+
4. **Deploy:**
|
| 102 |
+
- The Space will automatically build and deploy
|
| 103 |
+
- Your app will be available at `https://huggingface.co/spaces/your-username/your-space-name`
|
| 104 |
|
| 105 |
## π Application Structure
|
| 106 |
|
|
|
|
| 115 |
β βββ future_weeks.py # Weeks 2-4 placeholder content
|
| 116 |
β βββ assignments.py # Assignments page
|
| 117 |
β βββ resources.py # Resources page
|
| 118 |
+
βββ tests/
|
| 119 |
+
β βββ __init__.py
|
| 120 |
+
β βββ test_database.py # Database connection testing
|
| 121 |
+
βββ database/
|
| 122 |
+
β βββ setup.sql # Database setup script
|
| 123 |
βββ .streamlit/
|
| 124 |
+
β βββ config.toml # Streamlit configuration
|
| 125 |
+
β βββ secrets.toml # Local secrets (not in repo)
|
| 126 |
βββ requirements.txt # Python dependencies
|
| 127 |
βββ Dockerfile # Container configuration
|
| 128 |
βββ README.md # This file
|
|
|
|
| 157 |
## π§ Technical Details
|
| 158 |
|
| 159 |
- **Framework:** Streamlit
|
| 160 |
+
- **Database:** Supabase (PostgreSQL)
|
| 161 |
+
- **Authentication:** Custom system with Supabase backend
|
| 162 |
+
- **Data Storage:** Supabase database (cloud-ready)
|
| 163 |
- **Deployment:** Optimized for Hugging Face Spaces
|
| 164 |
- **Architecture:** Modular design with separate view modules
|
| 165 |
|
|
|
|
| 172 |
|
| 173 |
## π Security Features
|
| 174 |
|
| 175 |
+
- Supabase Row Level Security (RLS)
|
| 176 |
+
- Custom authentication system
|
| 177 |
- User role management
|
| 178 |
- Secure file upload handling
|
| 179 |
|
| 180 |
## βοΈ Cloud Features
|
| 181 |
|
| 182 |
+
- **Supabase integration** - Cloud database and authentication
|
| 183 |
+
- **Hugging Face Spaces ready** - Optimized for immediate deployment
|
| 184 |
+
- **Docker containerization** - Consistent deployment environment
|
| 185 |
+
- **Environment-based configuration** - Secrets management
|
| 186 |
+
|
| 187 |
+
## π§ͺ Testing
|
| 188 |
+
|
| 189 |
+
Run database tests locally:
|
| 190 |
+
```bash
|
| 191 |
+
streamlit run tests/test_database.py
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
This will help you verify your Supabase connection and user data.
|
| 195 |
|
| 196 |
## π Future Enhancements
|
| 197 |
|
|
|
|
| 198 |
- Real-time debate practice sessions
|
| 199 |
- Video conferencing integration
|
| 200 |
- Advanced progress analytics
|
| 201 |
- Mobile-responsive design
|
| 202 |
- Debate tournament management
|
| 203 |
+
- Advanced user management
|
| 204 |
|
| 205 |
---
|
| 206 |
|
| 207 |
+
**Built with β€οΈ using Streamlit & Supabase | Cloud-ready for Hugging Face Spaces**
|
requirements.txt
CHANGED
|
@@ -5,4 +5,5 @@ pandas
|
|
| 5 |
altair
|
| 6 |
PyYAML
|
| 7 |
bcrypt
|
| 8 |
-
pyarrow
|
|
|
|
|
|
| 5 |
altair
|
| 6 |
PyYAML
|
| 7 |
bcrypt
|
| 8 |
+
pyarrow
|
| 9 |
+
supabase
|
src/auth.py
CHANGED
|
@@ -1,12 +1,33 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def show_login_page():
|
| 12 |
"""Show a simple custom login page"""
|
|
@@ -32,29 +53,24 @@ def show_login_page():
|
|
| 32 |
|
| 33 |
def check_credentials(username, password):
|
| 34 |
"""Check if username and password are valid"""
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if stored_password.startswith('$2b$'):
|
| 44 |
-
# For now, accept any password for hashed accounts
|
| 45 |
-
# In production, you'd use bcrypt to verify
|
| 46 |
return True
|
| 47 |
-
else:
|
| 48 |
-
# Plain text password comparison
|
| 49 |
-
return stored_password == password
|
| 50 |
|
| 51 |
return False
|
| 52 |
|
| 53 |
def get_user_name(username):
|
| 54 |
"""Get the display name for a username"""
|
| 55 |
-
|
| 56 |
-
if
|
| 57 |
-
return
|
| 58 |
return username
|
| 59 |
|
| 60 |
def handle_logout():
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from supabase import create_client, Client
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Initialize Supabase client
|
| 6 |
+
def init_supabase():
|
| 7 |
+
"""Initialize Supabase client"""
|
| 8 |
+
url = st.secrets.get("SUPABASE_URL")
|
| 9 |
+
key = st.secrets.get("SUPABASE_KEY")
|
| 10 |
+
|
| 11 |
+
if not url or not key:
|
| 12 |
+
st.error("Supabase credentials not found. Please set SUPABASE_URL and SUPABASE_KEY in your .streamlit/secrets.toml file.")
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
return create_client(url, key)
|
| 16 |
+
|
| 17 |
+
def get_user_by_username(username):
|
| 18 |
+
"""Get user from database by username"""
|
| 19 |
+
supabase = init_supabase()
|
| 20 |
+
if not supabase:
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
response = supabase.rpc('get_user_by_username', {'username_param': username}).execute()
|
| 25 |
+
if response.data:
|
| 26 |
+
return response.data[0]
|
| 27 |
+
return None
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"Database error: {str(e)}")
|
| 30 |
+
return None
|
| 31 |
|
| 32 |
def show_login_page():
|
| 33 |
"""Show a simple custom login page"""
|
|
|
|
| 53 |
|
| 54 |
def check_credentials(username, password):
|
| 55 |
"""Check if username and password are valid"""
|
| 56 |
+
if not username or not password:
|
| 57 |
+
return False
|
| 58 |
|
| 59 |
+
# For now, accept any password for existing users
|
| 60 |
+
# In production, you'd want to add password hashing
|
| 61 |
+
user = get_user_by_username(username)
|
| 62 |
+
if user:
|
| 63 |
+
# Simple password check - in production use proper hashing
|
| 64 |
+
if password == "password123": # Default password for all users
|
|
|
|
|
|
|
|
|
|
| 65 |
return True
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
return False
|
| 68 |
|
| 69 |
def get_user_name(username):
|
| 70 |
"""Get the display name for a username"""
|
| 71 |
+
user = get_user_by_username(username)
|
| 72 |
+
if user:
|
| 73 |
+
return user.get('full_name', username)
|
| 74 |
return username
|
| 75 |
|
| 76 |
def handle_logout():
|
tests/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Tests package for Debate Class LMS
|
tests/test_database.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from supabase import create_client, Client
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def init_supabase():
|
| 6 |
+
"""Initialize Supabase client"""
|
| 7 |
+
url = st.secrets.get("SUPABASE_URL")
|
| 8 |
+
key = st.secrets.get("SUPABASE_KEY")
|
| 9 |
+
|
| 10 |
+
# Debug information
|
| 11 |
+
st.write(f"Debug - SUPABASE_URL: {url}")
|
| 12 |
+
st.write(f"Debug - SUPABASE_KEY: {key[:10] if key else 'None'}...")
|
| 13 |
+
|
| 14 |
+
if not url or not key:
|
| 15 |
+
st.error("Supabase credentials not found. Please set SUPABASE_URL and SUPABASE_KEY in your .streamlit/secrets.toml file.")
|
| 16 |
+
st.info("Update your .streamlit/secrets.toml file with:")
|
| 17 |
+
st.code("""
|
| 18 |
+
SUPABASE_URL = "https://your-project-id.supabase.co"
|
| 19 |
+
SUPABASE_KEY = "your-anon-key-here"
|
| 20 |
+
""")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
return create_client(url, key)
|
| 24 |
+
|
| 25 |
+
def test_database_connection():
|
| 26 |
+
"""Test the database connection and display user data"""
|
| 27 |
+
st.subheader("π Database Connection Test")
|
| 28 |
+
|
| 29 |
+
supabase = init_supabase()
|
| 30 |
+
if not supabase:
|
| 31 |
+
st.error("β Failed to initialize Supabase client")
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Test 1: Try to fetch all users
|
| 36 |
+
st.write("**Test 1: Fetching all users...**")
|
| 37 |
+
response = supabase.table('users').select('*').execute()
|
| 38 |
+
|
| 39 |
+
if response.data:
|
| 40 |
+
st.success(f"β
Successfully connected to database!")
|
| 41 |
+
st.write(f"π Found {len(response.data)} users in the database:")
|
| 42 |
+
|
| 43 |
+
# Display users in a nice table
|
| 44 |
+
users_data = []
|
| 45 |
+
for user in response.data:
|
| 46 |
+
users_data.append({
|
| 47 |
+
"Username": user.get('username', 'N/A'),
|
| 48 |
+
"Full Name": user.get('full_name', 'N/A'),
|
| 49 |
+
"Email": user.get('email', 'N/A'),
|
| 50 |
+
"Role": user.get('role', 'N/A')
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
st.dataframe(users_data, use_container_width=True)
|
| 54 |
+
return True
|
| 55 |
+
else:
|
| 56 |
+
st.warning("β οΈ Connected to database but no users found")
|
| 57 |
+
return True
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"β Database connection failed: {str(e)}")
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
def test_user_lookup():
|
| 64 |
+
"""Test the get_user_by_username function"""
|
| 65 |
+
st.subheader("π User Lookup Test")
|
| 66 |
+
|
| 67 |
+
supabase = init_supabase()
|
| 68 |
+
if not supabase:
|
| 69 |
+
return False
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Test with a known username
|
| 73 |
+
test_username = "alice"
|
| 74 |
+
st.write(f"**Testing lookup for user: {test_username}**")
|
| 75 |
+
|
| 76 |
+
response = supabase.rpc('get_user_by_username', {'username_param': test_username}).execute()
|
| 77 |
+
|
| 78 |
+
if response.data:
|
| 79 |
+
user = response.data[0]
|
| 80 |
+
st.success(f"β
Found user: {user.get('full_name', 'N/A')}")
|
| 81 |
+
st.json(user)
|
| 82 |
+
return True
|
| 83 |
+
else:
|
| 84 |
+
st.warning(f"β οΈ User '{test_username}' not found")
|
| 85 |
+
return False
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
st.error(f"β User lookup failed: {str(e)}")
|
| 89 |
+
return False
|
| 90 |
+
|
| 91 |
+
def main():
|
| 92 |
+
"""Main test function"""
|
| 93 |
+
st.title("π§ͺ Database Testing Tool")
|
| 94 |
+
st.markdown("Use this tool to test your Supabase database connection.")
|
| 95 |
+
|
| 96 |
+
# Test options
|
| 97 |
+
test_option = st.selectbox(
|
| 98 |
+
"Choose a test to run:",
|
| 99 |
+
["Database Connection", "User Lookup", "Run All Tests"]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if test_option == "Database Connection":
|
| 103 |
+
test_database_connection()
|
| 104 |
+
elif test_option == "User Lookup":
|
| 105 |
+
test_user_lookup()
|
| 106 |
+
elif test_option == "Run All Tests":
|
| 107 |
+
st.subheader("Running all tests...")
|
| 108 |
+
test1 = test_database_connection()
|
| 109 |
+
st.markdown("---")
|
| 110 |
+
test2 = test_user_lookup()
|
| 111 |
+
|
| 112 |
+
if test1 and test2:
|
| 113 |
+
st.success("π All tests passed!")
|
| 114 |
+
else:
|
| 115 |
+
st.error("β Some tests failed. Check the output above.")
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
main()
|