MoSPI / upload_main.py
akshansh36's picture
Upload 9 files
4ab6643 verified
import streamlit as st
from pymongo import MongoClient
import os
from dotenv import load_dotenv
load_dotenv()
import time
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
AWS_BUCKET_NAME = os.getenv("AWS_BUCKET_NAME")
MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = os.getenv("DB_NAME")
COLLECTION_NAME = os.getenv("COMPANY_COLLECTION_NAME")
mongo_client = MongoClient(MONGO_URI)
db = mongo_client[DB_NAME]
collection = db[COLLECTION_NAME]
def upload_main_page():
#
if st.button("Back",key="Back_button"):
st.session_state.page = "home"
st.rerun()
st.markdown("---")
col1, col2 = st.columns([1,6])
with col1:
if st.button("Upload Image",key="upload_image_button"):
st.session_state.page="upload_image"
st.rerun()
# Placeholder for image upload function
with col2:
if st.button("View Images",key="view_image_button"):
st.session_state.page = "view_image"
st.rerun()
# Row 2: Buttons for PDF actions
col3, col4 = st.columns([1,6])
with col3:
if st.button("Upload PDF",key="upload_pdf_button"):
st.session_state.page="upload_pdf"
st.rerun()
with col4:
if st.button("View PDFs",key="view_pdf_button"):
st.session_state.page="view_pdf"
st.rerun()
# Line break
st.markdown("---")
# Section for company description
st.subheader("Tell us about your company in a few lines")
# Fetch the "about_company" document from MongoDB
about_company_doc = collection.find_one({"type": "about_company"})
if about_company_doc and "company_description" in about_company_doc:
# If company_description exists, show it and allow for updates
st.write("### Current Description")
st.write(about_company_doc["company_description"]) # Display current description (non-editable)
# Show an empty text box for new input
new_description = st.text_area("Enter new description to update", "")
if st.button("Update",key="update_company"):
# Update the company description in MongoDB
collection.update_one(
{"_id": about_company_doc["_id"]},
{"$set": {"company_description": new_description}}
)
st.success("Company description updated successfully.")
time.sleep(3)
st.rerun()
else:
# If company_description does not exist, show an empty text box for initial input
new_description = st.text_area("Enter company description", "")
if st.button("Save",key="save_description"):
# Insert a new company description field in MongoDB
if about_company_doc:
# Update the existing document
collection.update_one(
{"_id": about_company_doc["_id"]},
{"$set": {"company_description": new_description}}
)
else:
# Insert a new document if it doesn't exist
collection.insert_one({"type": "about_company", "company_description": new_description})
st.success("Company description saved successfully.")
st.markdown("---")
# Section for adding and displaying tags
st.subheader("Manage Tags and Categories")
# Part 1: Tags
st.write("#### Add Tags")
# Fetch tags document
tags_doc = collection.find_one({"type": "tags"})
current_tags = tags_doc["tags"] if tags_doc and "tags" in tags_doc else []
# Display current tags with horizontal alignment and black-blue text color
if current_tags:
st.write("Current Tags:")
tag_html = " ".join([
f"<span style='display: inline-block; padding: 5px 10px; margin: 5px; border-radius: 15px; background-color: #e0e0e0; color: #0000FF;'>{tag}</span>"
for tag in current_tags
])
st.markdown(tag_html, unsafe_allow_html=True)
else:
st.write("No tags available.")
# Input box to add new tags
new_tags = st.text_input("Enter new tags separated by commas", "")
if st.button("Add Tags",key="add_tags"):
# Split the input tags by commas, strip whitespace, and remove any duplicates
tags_to_add = list(set([tag.strip() for tag in new_tags.split(",") if tag.strip()]))
# Update the MongoDB document with new tags
if tags_doc:
# Update the existing tags document
collection.update_one(
{"_id": tags_doc["_id"]},
{"$addToSet": {"tags": {"$each": tags_to_add}}}
)
else:
# Insert a new tags document if it doesn't exist
collection.insert_one({"type": "tags", "tags": tags_to_add})
st.success("Tags added successfully.")
st.rerun() # Refresh the page to display updated tags
# Part 2: Categories
st.write("#### Add Categories")
# Fetch categories document
categories_doc = collection.find_one({"type": "categories"})
current_categories = categories_doc["categories"] if categories_doc and "categories" in categories_doc else []
# Display current categories with horizontal alignment and black-blue text color
if current_categories:
st.write("Current Categories:")
category_html = " ".join([
f"<span style='display: inline-block; padding: 5px 10px; margin: 5px; border-radius: 15px; background-color: #e0e0e0; color: #0000FF;'>{category}</span>"
for category in current_categories
])
st.markdown(category_html, unsafe_allow_html=True)
else:
st.write("No categories available.")
# Input box to add new categories
new_categories = st.text_input("Enter new categories separated by commas", "")
if st.button("Add Categories",key="add_categories"):
# Split the input categories by commas, strip whitespace, and remove any duplicates
categories_to_add = list(set([category.strip() for category in new_categories.split(",") if category.strip()]))
# Update the MongoDB document with new categories
if categories_doc:
# Update the existing categories document
collection.update_one(
{"_id": categories_doc["_id"]},
{"$addToSet": {"categories": {"$each": categories_to_add}}}
)
else:
# Insert a new categories document if it doesn't exist
collection.insert_one({"type": "categories", "categories": categories_to_add})
st.success("Categories added successfully.")
st.rerun() # Refresh the page to display updated categories