Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +83 -50
src/streamlit_app.py
CHANGED
|
@@ -658,51 +658,6 @@ def update_user_progress(user_id, topic_id, comment_id):
|
|
| 658 |
if local_con:
|
| 659 |
local_con.close()
|
| 660 |
|
| 661 |
-
# Helper function to handle comment submission UI and logic
|
| 662 |
-
def share_wisdom(prompt, allow_skip=False):
|
| 663 |
-
st.markdown(prompt)
|
| 664 |
-
new_comment_text = st.text_area(f"Your Insight {'that different from others above (Empty to skip)' if allow_skip else ''}", key="new_comment_input")
|
| 665 |
-
if st.button("Share Your Wisdom", key=f"share_wisdom_button_{random.randint(1, 11)}"):
|
| 666 |
-
if new_comment_text:
|
| 667 |
-
user_email = st.session_state.get('user_email', '')
|
| 668 |
-
user_id = find_or_create_user(user_email) # Ensure user exists
|
| 669 |
-
if user_id:
|
| 670 |
-
local_con = None
|
| 671 |
-
try:
|
| 672 |
-
local_con = duckdb.connect(database=DB_PATH, read_only=False)
|
| 673 |
-
comment_id = str(uuid.uuid4())
|
| 674 |
-
local_con.execute("""
|
| 675 |
-
INSERT INTO comments (id, topic_id, user_id, content)
|
| 676 |
-
VALUES (?, ?, ?, ?)
|
| 677 |
-
""", [comment_id, topic_id, user_id, new_comment_text])
|
| 678 |
-
|
| 679 |
-
# Append new comment to history
|
| 680 |
-
st.session_state.comment_history += f"\n\n💬 {new_comment_text}"
|
| 681 |
-
|
| 682 |
-
# Get next comment (could be the one just submitted)
|
| 683 |
-
next_comment_id, next_comment_content = get_random_unvoted_comment(user_id, topic_id)
|
| 684 |
-
st.session_state.current_comment_id = next_comment_id
|
| 685 |
-
st.session_state.current_comment_content = next_comment_content
|
| 686 |
-
|
| 687 |
-
# Update progress
|
| 688 |
-
update_user_progress(user_id, topic_id, next_comment_id)
|
| 689 |
-
|
| 690 |
-
st.session_state.new_comment_input = "" # Clear input box
|
| 691 |
-
st.rerun() # Rerun to update UI
|
| 692 |
-
|
| 693 |
-
except Exception as e:
|
| 694 |
-
st.error(f"Error sharing information: {e}")
|
| 695 |
-
finally:
|
| 696 |
-
if local_con:
|
| 697 |
-
local_con.close()
|
| 698 |
-
else:
|
| 699 |
-
st.error("Could not find or create user.")
|
| 700 |
-
elif allow_skip:
|
| 701 |
-
return
|
| 702 |
-
else:
|
| 703 |
-
st.warning("Please enter your thought.")
|
| 704 |
-
|
| 705 |
-
|
| 706 |
# --- Page Functions ---
|
| 707 |
|
| 708 |
def home_page():
|
|
@@ -834,7 +789,7 @@ def view_topic_page():
|
|
| 834 |
# Include functional information
|
| 835 |
st.markdown(f"**Shareable Quest Scroll ID:** `{topic_id}`")
|
| 836 |
# Construct shareable link using current app URL
|
| 837 |
-
app_url = st.query_params.get('base', ['
|
| 838 |
shareable_link = f"{app_url}?topic={topic_id}" if app_url else f"?topic={topic_id}"
|
| 839 |
st.markdown(f"**Shareable Scroll Link:** `{shareable_link}`")
|
| 840 |
|
|
@@ -933,8 +888,11 @@ def view_topic_page():
|
|
| 933 |
if show_new_area_message == True:
|
| 934 |
_, user_ids = get_user_cluster_label(user_id, get_ttl_hash(10))
|
| 935 |
new_area_name, desc = name_user_group(user_ids, topic_id)
|
| 936 |
-
|
| 937 |
-
|
|
|
|
|
|
|
|
|
|
| 938 |
st.session_state._show_new_area_message = False
|
| 939 |
st.markdown(f"[Collected new insight, {random_phrase}]:\n* {current_comment_content}")
|
| 940 |
|
|
@@ -993,7 +951,43 @@ def view_topic_page():
|
|
| 993 |
"As you walk through the streets, people gather, eager to hear your thoughts on the Emperor's dilemma. What advice do you give?"
|
| 994 |
]
|
| 995 |
# Pass the current topic_id to share_wisdom if needed, though it's not currently used there.
|
| 996 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 997 |
|
| 998 |
# Get next comment
|
| 999 |
# This should always get the next unvoted comment for the user in this topic.
|
|
@@ -1039,7 +1033,46 @@ def view_topic_page():
|
|
| 1039 |
|
| 1040 |
# --- Comment Submission ---
|
| 1041 |
with st.expander("Offer Your Counsel to the Emperor", expanded=False):
|
| 1042 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1043 |
st.markdown("---")
|
| 1044 |
|
| 1045 |
|
|
|
|
| 658 |
if local_con:
|
| 659 |
local_con.close()
|
| 660 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
# --- Page Functions ---
|
| 662 |
|
| 663 |
def home_page():
|
|
|
|
| 789 |
# Include functional information
|
| 790 |
st.markdown(f"**Shareable Quest Scroll ID:** `{topic_id}`")
|
| 791 |
# Construct shareable link using current app URL
|
| 792 |
+
app_url = st.query_params.get('base', ['http://localhost:8501/'])[0] # Get base URL if available
|
| 793 |
shareable_link = f"{app_url}?topic={topic_id}" if app_url else f"?topic={topic_id}"
|
| 794 |
st.markdown(f"**Shareable Scroll Link:** `{shareable_link}`")
|
| 795 |
|
|
|
|
| 888 |
if show_new_area_message == True:
|
| 889 |
_, user_ids = get_user_cluster_label(user_id, get_ttl_hash(10))
|
| 890 |
new_area_name, desc = name_user_group(user_ids, topic_id)
|
| 891 |
+
for statm in [
|
| 892 |
+
f"📚 You've collected **{len(comment_history.splitlines())}** insights this time.",
|
| 893 |
+
f"🗺️ And yet, your journey leads you to a new place: **{new_area_name}**! {desc}"]:
|
| 894 |
+
st.markdown(statm)
|
| 895 |
+
st.session_state.comment_history += f"\n\n{statm}"
|
| 896 |
st.session_state._show_new_area_message = False
|
| 897 |
st.markdown(f"[Collected new insight, {random_phrase}]:\n* {current_comment_content}")
|
| 898 |
|
|
|
|
| 951 |
"As you walk through the streets, people gather, eager to hear your thoughts on the Emperor's dilemma. What advice do you give?"
|
| 952 |
]
|
| 953 |
# Pass the current topic_id to share_wisdom if needed, though it's not currently used there.
|
| 954 |
+
st.markdown(random.choice(prompts))
|
| 955 |
+
new_comment_text = st.text_area("Your Insight that different from others above (Empty to skip)", key="tmp_new_comment_input")
|
| 956 |
+
if st.button("Share Your Wisdom"):
|
| 957 |
+
if new_comment_text and len(new_comment_text.strip()):
|
| 958 |
+
user_email = st.session_state.get('user_email', '')
|
| 959 |
+
user_id = find_or_create_user(user_email) # Ensure user exists
|
| 960 |
+
if user_id:
|
| 961 |
+
local_con = None
|
| 962 |
+
try:
|
| 963 |
+
local_con = duckdb.connect(database=DB_PATH, read_only=False)
|
| 964 |
+
comment_id = str(uuid.uuid4())
|
| 965 |
+
local_con.execute("""
|
| 966 |
+
INSERT INTO comments (id, topic_id, user_id, content)
|
| 967 |
+
VALUES (?, ?, ?, ?)
|
| 968 |
+
""", [comment_id, topic_id, user_id, new_comment_text])
|
| 969 |
+
|
| 970 |
+
# Append new comment to history
|
| 971 |
+
st.session_state.comment_history += f"\n\n💬 {new_comment_text}"
|
| 972 |
+
|
| 973 |
+
# Get next comment (could be the one just submitted)
|
| 974 |
+
next_comment_id, next_comment_content = get_random_unvoted_comment(user_id, topic_id)
|
| 975 |
+
st.session_state.current_comment_id = next_comment_id
|
| 976 |
+
st.session_state.current_comment_content = next_comment_content
|
| 977 |
+
|
| 978 |
+
# Update progress
|
| 979 |
+
update_user_progress(user_id, topic_id, next_comment_id)
|
| 980 |
+
|
| 981 |
+
st.session_state.tmp_new_comment_input = "" # Clear input box
|
| 982 |
+
st.rerun() # Rerun to update UI
|
| 983 |
+
|
| 984 |
+
except Exception as e:
|
| 985 |
+
st.error(f"Error sharing information: {e}")
|
| 986 |
+
finally:
|
| 987 |
+
if local_con:
|
| 988 |
+
local_con.close()
|
| 989 |
+
else:
|
| 990 |
+
st.error("Could not find or create user.")
|
| 991 |
|
| 992 |
# Get next comment
|
| 993 |
# This should always get the next unvoted comment for the user in this topic.
|
|
|
|
| 1033 |
|
| 1034 |
# --- Comment Submission ---
|
| 1035 |
with st.expander("Offer Your Counsel to the Emperor", expanded=False):
|
| 1036 |
+
st.markdown("Having heard the thoughts of others, what wisdom do you wish to share regarding the Emperor's concern?")
|
| 1037 |
+
new_comment_text = st.text_area(f"Your Insight", key="new_comment_input")
|
| 1038 |
+
if st.button("Share Your Wisdom"):
|
| 1039 |
+
if new_comment_text:
|
| 1040 |
+
user_email = st.session_state.get('user_email', '')
|
| 1041 |
+
user_id = find_or_create_user(user_email) # Ensure user exists
|
| 1042 |
+
if user_id:
|
| 1043 |
+
local_con = None
|
| 1044 |
+
try:
|
| 1045 |
+
local_con = duckdb.connect(database=DB_PATH, read_only=False)
|
| 1046 |
+
comment_id = str(uuid.uuid4())
|
| 1047 |
+
local_con.execute("""
|
| 1048 |
+
INSERT INTO comments (id, topic_id, user_id, content)
|
| 1049 |
+
VALUES (?, ?, ?, ?)
|
| 1050 |
+
""", [comment_id, topic_id, user_id, new_comment_text])
|
| 1051 |
+
|
| 1052 |
+
# Append new comment to history
|
| 1053 |
+
st.session_state.comment_history += f"\n\n💬 {new_comment_text}"
|
| 1054 |
+
|
| 1055 |
+
# Get next comment (could be the one just submitted)
|
| 1056 |
+
next_comment_id, next_comment_content = get_random_unvoted_comment(user_id, topic_id)
|
| 1057 |
+
st.session_state.current_comment_id = next_comment_id
|
| 1058 |
+
st.session_state.current_comment_content = next_comment_content
|
| 1059 |
+
|
| 1060 |
+
# Update progress
|
| 1061 |
+
update_user_progress(user_id, topic_id, next_comment_id)
|
| 1062 |
+
|
| 1063 |
+
st.session_state.new_comment_input = "" # Clear input box
|
| 1064 |
+
st.rerun() # Rerun to update UI
|
| 1065 |
+
|
| 1066 |
+
except Exception as e:
|
| 1067 |
+
st.error(f"Error sharing information: {e}")
|
| 1068 |
+
finally:
|
| 1069 |
+
if local_con:
|
| 1070 |
+
local_con.close()
|
| 1071 |
+
else:
|
| 1072 |
+
st.error("Could not find or create user.")
|
| 1073 |
+
else:
|
| 1074 |
+
st.warning("Please enter your thought.")
|
| 1075 |
+
|
| 1076 |
st.markdown("---")
|
| 1077 |
|
| 1078 |
|