Download admin.py from itachi-ai/somewhere: direct link, hf CLI and curl.
- Browser
- Download file 5.86 kB
-
https://huggingface.co/spaces/itachi-ai/somewhere/resolve/main/admin.py
- Command line
-
hf download hf://spaces/itachi-ai/somewhere/admin.py
-
curl -L -o admin.py https://huggingface.co/spaces/itachi-ai/somewhere/resolve/main/admin.py
5.86 kB
| import os | |
| import streamlit as st | |
| from mongodb import add_post,get_post_titles, get_post_by_id, update_post, delete_post | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| USERNAME = os.getenv("USERNAME_") | |
| PASSWORD = os.getenv("PASSWORD_") | |
| def admin(): | |
| # Create a placeholder for the password input field | |
| user_placeholder = st.empty() | |
| password_placeholder = st.empty() | |
| # Show password input if it's not already correct | |
| username = user_placeholder.text_input("Enter Username") | |
| password = password_placeholder.text_input("Enter Password", type="password") | |
| # Check if the password is correct | |
| if password == PASSWORD and username == USERNAME: | |
| password_placeholder.empty() # Hide the password input | |
| user_placeholder.empty() # Hide the password input | |
| tab1, tab2, tab3 = st.tabs(["Read Post", "Add Post", "Update & Delete Post"]) | |
| with tab1: | |
| if st.session_state.get('selected_post_id', None): | |
| selected_post = get_post_by_id(st.session_state.selected_post_id) | |
| # Display the content of the selected post | |
| st.subheader(selected_post["title"]) | |
| st.divider() | |
| st.markdown(selected_post["content"], unsafe_allow_html =True) # Render the content as Markdown | |
| with tab2: | |
| st.subheader("Add a new Post") | |
| title = st.text_input("Post Title") | |
| # Text input area for Markdown content | |
| content = st.text_area("Post Content (Markdown supported)", height=800) | |
| status = st.selectbox("Status", ["private", "public", "achieve"], index=0) | |
| if st.button("Add Post"): | |
| if title and content: | |
| res = add_post(title, content, status, username) | |
| st.write(res) | |
| else: | |
| st.error("Please fill both the title and content.") | |
| with tab3: | |
| show_status = st.sidebar.selectbox("Status", ["private", "public", "achieve"], index = 1) | |
| post_titles = list(get_post_titles(show_status)) | |
| if post_titles: | |
| # Use session state to remember the selected post ID | |
| if "selected_post_id" not in st.session_state: | |
| st.session_state.selected_post_id = None | |
| # Display titles as clickable buttons in the sidebar | |
| selected_post = None | |
| for post in post_titles: | |
| # Check if the post is selected | |
| if st.sidebar.button(post["title"], key=f"button_{post['_id']}"): | |
| st.session_state.selected_post_id = post["_id"] | |
| # Only show the update section if a post is selected | |
| if st.session_state.selected_post_id: | |
| post_id = st.session_state.selected_post_id | |
| selected_post = get_post_by_id(post_id) | |
| if selected_post: | |
| # Display the content of the selected post | |
| new_title = st.text_input("New Title", value=selected_post["title"]) | |
| new_content = st.text_area("New Content (Markdown supported)", value=selected_post["content"], | |
| height=800) | |
| options = ["private", "public", "achieve"] | |
| new_status = st.selectbox("Status", options, index= options.index(selected_post['status']), key="status_update") | |
| left, right = st.columns(2) | |
| if left.button("Update Post", use_container_width=True, key=f"update_button_{post_id}"): | |
| if new_title and new_content: | |
| # Call the update_post function to save the changes to MongoDB | |
| res = update_post(post_id, new_title, new_content, new_status) | |
| st.success(res) # Display the result from the update function | |
| # Optionally, display updated content for the user to confirm the changes | |
| else: | |
| st.error("Please fill both the new title and content.") | |
| # Permanent delete logic | |
| delete_password = st.text_input("Password", type="password", key=f"delete_password_{post_id}") | |
| delete_button = right.button("Permanent Delete Post", use_container_width=True, | |
| key=f"delete_button_{post_id}") | |
| if delete_button: | |
| if delete_password == PASSWORD: | |
| res = delete_post(post_id) | |
| if res: | |
| st.success("Post deleted successfully!") | |
| st.session_state.selected_post_id = None # Reset selected post after deletion | |
| post_titles = list(get_post_titles(show_status)) # Refresh the list of posts | |
| else: | |
| st.error("An error occurred while deleting the post.") | |
| elif delete_password: # If password is filled but incorrect | |
| st.error("Incorrect password.") | |
| else: | |
| st.error("Post not found.") | |
| else: | |
| st.error("Please select a post first.") | |
| else: | |
| st.write("No posts available.") | |
| else: | |
| if password: # If password is entered but incorrect | |
| st.error("Incorrect password. Please try again.") | |
| else: # If no password is entered | |
| st.info("Please enter your Username & Password.") | |