sujitv14's picture
Update src/streamlit_app.py
e4f707b verified
Raw History Blame Contribute Delete
7.33 kB
import streamlit as st
import os
import json
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import gspread
from google.oauth2.service_account import Credentials
# ==================================
# πŸ” GOOGLE SHEETS CONNECTION
# ==================================
creds_dict = json.loads(os.environ["GOOGLE_CREDENTIALS"])
credentials = Credentials.from_service_account_info(
creds_dict,
scopes=[
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive"
]
)
client = gspread.authorize(credentials)
sheet = client.open("Student Marks").sheet1
# ==================================
# 🎨 PREMIUM UI CSS
# ==================================
st.markdown("""
<style>
.header {
display: flex;
align-items: center;
justify-content: center;
gap: 15px;
}
.header img {
width: 60px;
}
.header-text {
font-size: 30px;
font-weight: bold;
color: #2c3e50;
}
.sub-text {
text-align: center;
font-size: 16px;
color: gray;
}
.card {
padding: 20px;
border-radius: 12px;
background-color: #f8f9fa;
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
</style>
""", unsafe_allow_html=True)
# ==================================
# 🏫 HEADER
# ==================================
st.markdown("""
<div class="header">
<img src="https://cdn-icons-png.flaticon.com/512/3135/3135755.png">
<div class="header-text">Your College Name</div>
</div>
<div class="sub-text">
Department of Computer Engineering<br>
HTML Lab Digital Evaluation System
</div>
<hr>
""", unsafe_allow_html=True)
# ==================================
# 🧠 HTML EVALUATION
# ==================================
def evaluate_html(content):
score = 0
feedback = []
soup = BeautifulSoup(content, 'html.parser')
if soup.html:
score += 1
else:
feedback.append("Missing <html> tag")
if soup.head and soup.title:
score += 1
else:
feedback.append("Missing <head> or <title>")
if soup.body:
score += 1
else:
feedback.append("Missing <body>")
tags = ['p', 'h1', 'img', 'a', 'table']
used = [tag for tag in tags if soup.find(tag)]
if len(used) >= 3:
score += 1
else:
feedback.append("Use at least 3 different HTML tags")
if len(soup.text.strip()) > 20:
score += 1
else:
feedback.append("Content too short")
return score, feedback
# ==================================
# πŸ“Š GOOGLE SHEET FUNCTIONS
# ==================================
def get_all_data():
return sheet.get_all_records()
def insert_data(data):
sheet.append_row(data)
# ==================================
# 🎭 ROLE SELECTION
# ==================================
st.markdown("### 🎭 Select Your Role")
role = st.radio("", ["πŸ‘¨β€πŸŽ“ Student", "πŸ‘¨β€πŸ« Teacher"], horizontal=True)
# ==================================
# πŸ‘¨β€πŸŽ“ STUDENT SECTION
# ==================================
if role == "πŸ‘¨β€πŸŽ“ Student":
st.markdown('<div class="card">', unsafe_allow_html=True)
st.subheader("πŸ“€ Student Submission")
col1, col2 = st.columns(2)
with col1:
name = st.text_input("Student Name")
roll = st.text_input("Roll Number")
with col2:
experiment = st.text_input("Experiment No")
uploaded_file = st.file_uploader("Upload HTML File", type=["html"])
if st.button("πŸš€ Submit"):
if not (name and roll and experiment and uploaded_file):
st.error("Please fill all fields")
else:
content = uploaded_file.read().decode("utf-8")
performance, feedback = evaluate_html(content)
submission = 5
attendance = 0
oral = 0
total = submission + performance
records = get_all_data()
df = pd.DataFrame(records)
if not df.empty and ((df["Roll No"] == roll) & (df["Experiment No"] == experiment)).any():
st.error("Already submitted!")
else:
data = [
name, roll, experiment,
attendance, oral,
submission, performance,
total, str(datetime.now())
]
insert_data(data)
st.success("βœ… Submitted Successfully!")
if feedback:
st.warning("⚠ Feedback:")
for f in feedback:
st.write(f"- {f}")
st.markdown('</div>', unsafe_allow_html=True)
# ==================================
# πŸ‘¨β€πŸ« TEACHER SECTION
# ==================================
if role == "πŸ‘¨β€πŸ« Teacher":
pwd = st.text_input("πŸ” Enter Password", type="password")
if pwd != "admin123":
st.warning("Enter correct password")
st.stop()
st.markdown('<div class="card">', unsafe_allow_html=True)
st.subheader("πŸ§‘β€πŸ« Teacher Panel")
st.subheader("Update Attendance & Oral")
roll_input = st.text_input("Roll No")
exp_input = st.text_input("Experiment No")
attendance_input = st.slider("Attendance", 0, 5, 0)
oral_input = st.slider("Oral", 0, 5, 0)
if st.button("Update Marks"):
records = sheet.get_all_records()
found = False
for i, row in enumerate(records, start=2):
if str(row["Roll No"]) == roll_input and str(row["Experiment No"]) == exp_input:
performance = int(row["Performance"])
submission = int(row["Submission"])
total = attendance_input + oral_input + submission + performance
sheet.update_cell(i, 4, attendance_input)
sheet.update_cell(i, 5, oral_input)
sheet.update_cell(i, 8, total)
st.success("βœ… Marks Updated!")
found = True
break
if not found:
st.error("Record not found!")
st.markdown('</div>', unsafe_allow_html=True)
# ==================================
# πŸ“Š DASHBOARD
# ==================================
st.markdown("### πŸ“Š Performance Dashboard")
data = get_all_data()
df = pd.DataFrame(data)
if not df.empty:
col1, col2 = st.columns(2)
with col1:
st.info("πŸ“ˆ Average Marks per Experiment")
exp_avg = df.groupby("Experiment No")["Total"].mean()
st.bar_chart(exp_avg)
with col2:
st.info("πŸ† Top Students")
top_students = df.sort_values(by="Total", ascending=False).head(5)
st.dataframe(top_students)
st.subheader("πŸ“‰ Marks Distribution")
fig, ax = plt.subplots()
ax.hist(df["Total"])
ax.set_title("Marks Distribution")
st.pyplot(fig)
st.subheader("πŸ“‹ All Records")
st.dataframe(df)
else:
st.info("No data available")
# ==================================
# 🧾 FOOTER
# ==================================
st.markdown("""
<hr>
<div style='text-align: center; color: gray;'>
Developed by Department of Computer Engineering <br>
Β© 2026 Your College Name | All Rights Reserved
</div>
""", unsafe_allow_html=True)