Hostel / app.py
Hidayatmahar's picture
Update app.py
1bea3bd verified
import gradio as gr
import pandas as pd
import random
# Hostels and rooms
hostels = ["Hostel A", "Hostel B", "Hostel C"]
rooms = list(range(1, 21)) # 20 rooms per hostel
# DataFrame to store allotments
allotments = pd.DataFrame(columns=["Name", "Roll No", "Father's Name", "Hostel", "Room No"])
# Function to allot hostel and room
def allot_hostel(name, roll_no, father_name):
global allotments
# Check if already allotted
existing = allotments[allotments["Roll No"] == roll_no]
if not existing.empty:
return f"Already allotted: {existing.iloc[0]['Hostel']} - Room {existing.iloc[0]['Room No']}"
# Random hostel & room
hostel = random.choice(hostels)
room = random.choice(rooms)
# Save in DataFrame
new_row = pd.DataFrame([[name, roll_no, father_name, hostel, room]],
columns=allotments.columns)
allotments = pd.concat([allotments, new_row], ignore_index=True)
return f"Allotted: {hostel}, Room {room}"
# Function to show all allotments
def show_allotments():
if allotments.empty:
return "No allotments yet."
return allotments
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 🏠 Hostel Allotment System")
with gr.Row():
name = gr.Textbox(label="Student Name")
roll_no = gr.Textbox(label="Roll Number")
father_name = gr.Textbox(label="Father's Name")
allot_btn = gr.Button("Allot Hostel")
output = gr.Textbox(label="Allotment Result")
show_btn = gr.Button("Show Allotted Data")
table = gr.Dataframe(label="Allotment Records")
allot_btn.click(allot_hostel, inputs=[name, roll_no, father_name], outputs=output)
show_btn.click(show_allotments, outputs=table)
demo.launch()