MoSPI / view_pdf.py
akshansh36's picture
Update view_pdf.py
dd6d8ed verified
import streamlit as st
from pymongo import MongoClient
import os
from dotenv import load_dotenv
from datetime import datetime
# Load environment variables
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = os.getenv("DB_NAME")
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
mongo_client = MongoClient(MONGO_URI)
db = mongo_client[DB_NAME]
collection = db[COLLECTION_NAME]
def format_date(timestamp):
"""Convert timestamp to a readable date format."""
return datetime.fromtimestamp(timestamp).strftime("%B %d, %Y")
# Custom CSS to control image and expander container width and styling
def view_pdfs():
if st.button("Back"):
st.session_state.page = "upload_main"
st.rerun()
st.title("Your Uploaded PDFs")
# Fetch all uploaded images from MongoDB
pdfs = list(collection.find({"type": "PDF","status":"processed"}))
if not pdfs:
st.write("You have not uploaded any PDFs yet.")
return
# Display images in a grid (4 images per row)
cols = st.columns(4)
for idx, pdf in enumerate(pdfs):
col = cols[idx % 4]
with col:
# Expander for image details
filename=pdf.get('name','N/A')
with st.expander(f"{filename}"):
st.write(f"**File Name:** {pdf.get('name', 'N/A')}")
st.write(f"**Date Uploaded:** {format_date(pdf.get('upload_date', datetime.now().timestamp()))}")
# Display tags if available
tags = ", ".join(pdf.get("tags", []))
st.write(f"**Tags:** {tags if tags else 'No tags'}")
# Display categories if available
categories = ", ".join(pdf.get("categories", []))
st.write(f"**Categories:** {categories if categories else 'No categories'}")
# Download link
st.markdown(
f"<a href='{pdf['object_url']}' class='download-link' download>Download PDF</a>",
unsafe_allow_html=True
)
# Move to a new row after every 4 images
if (idx + 1) % 4 == 0:
st.write("") # Line break to move to the next row