i want a streamlit app where people can take photo and explain what the problem. The photi must be store in a space or in database and i want add the date and time of the photo. The person can write a little descriptoion (500 words) of the proble. The person is obliged to take his mail
To upload files, please first save the app
import streamlit as st
import sqlite3
from datetime import datetime
import os
from PIL import Image
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text
from sqlalchemy.orm import DeclarativeBase, Session
# Database setup
class Base(DeclarativeBase):
pass
class ProblemReport(Base):
__tablename__ = 'problem_reports'
id = Column(Integer, primary_key=True)
email = Column(String(255), nullable=False)
description = Column(Text, nullable=False)
photo_path = Column(String(500), nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
# Create engine and tables
engine = create_engine("sqlite:///problems.sqlite")
Base.metadata.create_all(bind=engine)
# Create photos directory if it doesn't exist
if not os.path.exists("photos"):
os.makedirs("photos")
st.title("Problem Reporter")
st.write("Report a problem by taking a photo and describing the issue")
# Form for problem reporting
with st.form("problem_report_form"):
st.header("Report a Problem")
# Email field (required)
email = st.text_input("Email Address *", placeholder="your.email@example.com")
# Photo upload
uploaded_file = st.file_uploader("Take or Upload a Photo *", type=['png', 'jpg', 'jpeg'])
# Description field
description = st.text_area(
"Problem Description *",
placeholder="Describe the problem in detail (max 500 words)...",
max_chars=500,
height=150
)
# Submit button
submitted = st.form_submit_button("Submit Report")
if submitted:
# Validation
if not email:
st.error("Email address is required!")
elif not uploaded_file:
st.error("Photo is required!")
elif not description:
st.error("Problem description is required!")
else:
try:
# Generate timestamp for filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Save the uploaded image
image = Image.open(uploaded_file)
photo_filename = f"photo_{timestamp}_{uploaded_file.name}"
photo_path = os.path.join("photos", photo_filename)
image.save(photo_path)
# Save to database
with Session(engine) as session:
new_report = ProblemReport(
email=email,
description=description,
photo_path=photo_path,
created_at=datetime.now()
)
session.add(new_report)
session.commit()
st.success("✅ Problem report submitted successfully!")
st.balloons()
# Display confirmation details
st.subheader("Report Details")
st.write(f"**Email:** {email}")
st.write(f"**Date & Time:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
st.write(f"**Description:** {description}")
st.write("**Photo:**")
st.image(image, caption="Uploaded Photo", use_column_width=True)
except Exception as e:
st.error(f"Error submitting report: {str(e)}")
# View previous reports section
st.header("Previous Reports")
if st.button("View All Reports"):
try:
with Session(engine) as session:
reports = session.query(ProblemReport).order_by(ProblemReport.created_at.desc()).all()
if reports:
for report in reports:
with st.expander(f"Report from {report.email} - {report.created_at.strftime('%Y-%m-%d %H:%M')}"):
st.write(f"**Email:** {report.email}")
st.write(f"**Date:** {report.created_at.strftime('%Y-%m-%d %H:%M:%S')}")
st.write(f"**Description:** {report.description}")
# Display photo if it exists
if os.path.exists(report.photo_path):
try:
image = Image.open(report.photo_path)
st.image(image, caption="Problem Photo", use_column_width=True)
except Exception as e:
st.write(f"Error loading photo: {str(e)}")
else:
st.write("Photo not found")
else:
st.write("No reports found.")
except Exception as e:
st.error(f"Error loading reports: {str(e)}")
# Statistics
st.header("Statistics")
try:
with Session(engine) as session:
total_reports = session.query(ProblemReport).count()
st.metric("Total Reports", total_reports)
except Exception as e:
st.error(f"Error loading statistics: {str(e)}")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?