From d0803e899a07a36ebfba292a9bf17f67d905c7c7 Mon Sep 17 00:00:00 2001 From: Huyen Nguyen <65232736+huyenNguyen20@users.noreply.github.com> Date: Thu, 15 Jun 2023 18:03:26 +0700 Subject: [PATCH] Features/edit convo title (#20) ## What Add an edit icon next to a chat history item on the sidebar to let users edit a generated conversation title ## Why - Feature parity with ChatGPT - Auto generated titles aren't very good sometimes ## How - Hack CSS - NOTE: if streamlit version is upgraded, css classnames could be generated ## Screenshots/Loom ![iScreen Shoter - Google Chrome - 230612162016](https://github.com/CoderPush/chatlit/assets/65232736/ff43d8a0-6169-4f29-906f-d8d83dcbe276) ![iScreen Shoter - Google Chrome - 230612161919](https://github.com/CoderPush/chatlit/assets/65232736/b97136b5-6017-4f92-a628-5ef326d5bf53) --- firestore_utils.py | 10 ++++++ utils.py | 86 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/firestore_utils.py b/firestore_utils.py index 697ec51..696e76d 100644 --- a/firestore_utils.py +++ b/firestore_utils.py @@ -56,3 +56,13 @@ def delete_convo(convo_id): document_ref = db.collection("conversations").document(convo_id) document_ref.delete() print(f"Deleted document with ID: {convo_id}") + + +def edit_convo(convo_id, new_label): + try: + db = get_firestore_db() + document_ref = db.collection("conversations").document(convo_id) + document_ref.update({"title": new_label}) + print(f"Updated document with ID: {convo_id}") + except firestore.NotFound as e: + print(f"Document with ID {convo_id} not found") diff --git a/utils.py b/utils.py index b3c7dc0..3cd3f4a 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,4 @@ -from firestore_utils import delete_convo +from firestore_utils import delete_convo, edit_convo def link_button(st, text, path): @@ -38,20 +38,49 @@ def link_row(st, text, path, selected=False): def button_row(st, cid, conversation, selected=False): title = conversation.get("title", cid) container = st.sidebar.container() + with container: - col1, col2 = st.columns([5, 1]) + col1, col2, col3 = st.columns([6, 1, 1], gap="small") with col1: - convo_button = st.button( - title, key=f"button_{cid}", disabled=selected, use_container_width=True - ) - if convo_button: - st.session_state["cid"] = cid + is_edit = st.session_state.get(f"edit_convo_button_{cid}", False) + + if not is_edit: + convo_button = st.button( + title, + key=f"button_{cid}", + disabled=selected, + use_container_width=True, + ) + if convo_button: + st.session_state["cid"] = cid + st.experimental_rerun() + else: + st.text_input( + "Edit Label", + title, + key=f"new_title_{cid}", + max_chars=30, + label_visibility="collapsed", + ) + + new_title = st.session_state.get(f"new_title_{cid}", "") + if new_title and new_title != title: + edit_convo(cid, new_label=new_title) + st.session_state[f"new_title_{cid}"] = "" st.experimental_rerun() with col2: + st.button( + ":pencil2:", + key=f"edit_convo_button_{cid}", + disabled=selected, + use_container_width=True, + ) + + with col3: delete_button = st.button( - "🗑️", + ":wastebasket:", key=f"delete_convo_button_{cid}", disabled=selected, use_container_width=True, @@ -60,6 +89,47 @@ def button_row(st, cid, conversation, selected=False): delete_convo(cid) st.experimental_rerun() + css = """ + + """ + + st.sidebar.markdown(css, unsafe_allow_html=True) + def get_key_from_params(st, key): params = st.experimental_get_query_params()