Spaces:
Sleeping
Sleeping
Update view_pdf.py
Browse files- view_pdf.py +70 -70
view_pdf.py
CHANGED
|
@@ -1,70 +1,70 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from pymongo import MongoClient
|
| 3 |
-
import os
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
|
| 7 |
-
# Load environment variables
|
| 8 |
-
load_dotenv()
|
| 9 |
-
MONGO_URI = os.getenv("MONGO_URI")
|
| 10 |
-
DB_NAME = os.getenv("DB_NAME")
|
| 11 |
-
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
|
| 12 |
-
|
| 13 |
-
mongo_client = MongoClient(MONGO_URI)
|
| 14 |
-
db = mongo_client[DB_NAME]
|
| 15 |
-
collection = db[COLLECTION_NAME]
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def format_date(timestamp):
|
| 19 |
-
"""Convert timestamp to a readable date format."""
|
| 20 |
-
return datetime.fromtimestamp(timestamp).strftime("%B %d, %Y")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Custom CSS to control image and expander container width and styling
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def view_pdfs():
|
| 27 |
-
if st.button("Back"):
|
| 28 |
-
st.session_state.page = "upload_main"
|
| 29 |
-
st.rerun()
|
| 30 |
-
st.title("Your Uploaded PDFs")
|
| 31 |
-
|
| 32 |
-
# Fetch all uploaded images from MongoDB
|
| 33 |
-
pdfs = list(collection.find({"type": "PDF"}))
|
| 34 |
-
|
| 35 |
-
if not pdfs:
|
| 36 |
-
st.write("You have not uploaded any PDFs yet.")
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
# Display images in a grid (4 images per row)
|
| 40 |
-
cols = st.columns(4)
|
| 41 |
-
for idx, pdf in enumerate(pdfs):
|
| 42 |
-
col = cols[idx % 4]
|
| 43 |
-
|
| 44 |
-
with col:
|
| 45 |
-
|
| 46 |
-
# Expander for image details
|
| 47 |
-
filename=pdf.get('name','N/A')
|
| 48 |
-
with st.expander(f"{filename}"):
|
| 49 |
-
st.write(f"**File Name:** {pdf.get('name', 'N/A')}")
|
| 50 |
-
st.write(f"**Date Uploaded:** {format_date(pdf.get('upload_date', datetime.now().timestamp()))}")
|
| 51 |
-
|
| 52 |
-
# Display tags if available
|
| 53 |
-
tags = ", ".join(pdf.get("tags", []))
|
| 54 |
-
st.write(f"**Tags:** {tags if tags else 'No tags'}")
|
| 55 |
-
|
| 56 |
-
# Display categories if available
|
| 57 |
-
categories = ", ".join(pdf.get("categories", []))
|
| 58 |
-
st.write(f"**Categories:** {categories if categories else 'No categories'}")
|
| 59 |
-
|
| 60 |
-
# Download link
|
| 61 |
-
st.markdown(
|
| 62 |
-
f"<a href='{pdf['object_url']}' class='download-link' download>Download PDF</a>",
|
| 63 |
-
unsafe_allow_html=True
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Move to a new row after every 4 images
|
| 67 |
-
if (idx + 1) % 4 == 0:
|
| 68 |
-
st.write("") # Line break to move to the next row
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pymongo import MongoClient
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
| 10 |
+
DB_NAME = os.getenv("DB_NAME")
|
| 11 |
+
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
|
| 12 |
+
|
| 13 |
+
mongo_client = MongoClient(MONGO_URI)
|
| 14 |
+
db = mongo_client[DB_NAME]
|
| 15 |
+
collection = db[COLLECTION_NAME]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def format_date(timestamp):
|
| 19 |
+
"""Convert timestamp to a readable date format."""
|
| 20 |
+
return datetime.fromtimestamp(timestamp).strftime("%B %d, %Y")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Custom CSS to control image and expander container width and styling
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def view_pdfs():
|
| 27 |
+
if st.button("Back"):
|
| 28 |
+
st.session_state.page = "upload_main"
|
| 29 |
+
st.rerun()
|
| 30 |
+
st.title("Your Uploaded PDFs")
|
| 31 |
+
|
| 32 |
+
# Fetch all uploaded images from MongoDB
|
| 33 |
+
pdfs = list(collection.find({"type": "PDF","status":"processed"}))
|
| 34 |
+
|
| 35 |
+
if not pdfs:
|
| 36 |
+
st.write("You have not uploaded any PDFs yet.")
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
# Display images in a grid (4 images per row)
|
| 40 |
+
cols = st.columns(4)
|
| 41 |
+
for idx, pdf in enumerate(pdfs):
|
| 42 |
+
col = cols[idx % 4]
|
| 43 |
+
|
| 44 |
+
with col:
|
| 45 |
+
|
| 46 |
+
# Expander for image details
|
| 47 |
+
filename=pdf.get('name','N/A')
|
| 48 |
+
with st.expander(f"{filename}"):
|
| 49 |
+
st.write(f"**File Name:** {pdf.get('name', 'N/A')}")
|
| 50 |
+
st.write(f"**Date Uploaded:** {format_date(pdf.get('upload_date', datetime.now().timestamp()))}")
|
| 51 |
+
|
| 52 |
+
# Display tags if available
|
| 53 |
+
tags = ", ".join(pdf.get("tags", []))
|
| 54 |
+
st.write(f"**Tags:** {tags if tags else 'No tags'}")
|
| 55 |
+
|
| 56 |
+
# Display categories if available
|
| 57 |
+
categories = ", ".join(pdf.get("categories", []))
|
| 58 |
+
st.write(f"**Categories:** {categories if categories else 'No categories'}")
|
| 59 |
+
|
| 60 |
+
# Download link
|
| 61 |
+
st.markdown(
|
| 62 |
+
f"<a href='{pdf['object_url']}' class='download-link' download>Download PDF</a>",
|
| 63 |
+
unsafe_allow_html=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Move to a new row after every 4 images
|
| 67 |
+
if (idx + 1) % 4 == 0:
|
| 68 |
+
st.write("") # Line break to move to the next row
|
| 69 |
+
|
| 70 |
+
|