File size: 4,027 Bytes
1d8ed3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d95637
1d8ed3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
from app.utils import debug_log


def show_questions(questons_to_show):
    """
    Displays the generated questions in a scrollable container-style format.
    """
    with st.container():
        if not questons_to_show:
            debug_log("No questions to display.")
            return
    
        for idx, question_item in enumerate(questons_to_show):
            question_text = question_item['question']
            answer_text = question_item['answer']

            col1, col2 = st.columns([0.9, 0.1])
            
            with col1:
                st.html(f"<p style='font-size:18px; margin:0;'>{idx+1}. {question_text}</p>")
                col1_ = st.columns([2, 2])[0]
                with col1_:
                    with st.expander("๐Ÿ’ก Show Answer"):
                        st.write(answer_text)

            with col2:
                st.checkbox("๐Ÿ“Œ", key=f"select_{idx}", value=True)

            st.markdown("")


def sync_selected_questions_to_download(chapter_or_query, questions_dict):
    """Syncs checked questions to the download list."""
    if chapter_or_query is None:
        st.error("No chapter or query selected!")
        return

    if chapter_or_query not in st.session_state['questions_to_download']:
        st.session_state['questions_to_download'][chapter_or_query] = []

    current_selected = st.session_state['questions_to_download'][chapter_or_query]

    for idx, question in enumerate(questions_dict):
        current_question = {'question': question['question'], 'answer': question['answer']}
        checkbox_key = f"select_{idx}"
        is_selected = st.session_state.get(checkbox_key, False)

        if is_selected and current_question not in current_selected:
            current_selected.append(current_question)
        elif not is_selected and current_question in current_selected:
            current_selected.remove(current_question)

    st.success(f"Selected questions synced for chapter or query '{chapter_or_query}'.")


def clear_selected_questions():
    """Clears all selected questions from session state."""
    st.session_state['questions_to_download'] = {}
    st.success("Cleared all selected questions.")


def show_download_controls(chapter_or_query, questions_dict):
    """Displays buttons to sync or clear selected questions."""
    col1_download, col2_download, _ = st.columns([0.3, 0.3, 0.4])

    with col1_download:
        st.html("""
        <style>
        /* Style for all buttons or target specific ones */
        div.stButton > button:first-child {
            background-color: #f0f0f0 !important;
            border: none !important;
            border-radius: 8px !important;
            color: #333 !important;
            font-family: 'Work Sans', sans-serif !important;
            font-weight: 500 !important;
            padding: 0.5rem 1rem !important;
            transition: all 0.2s ease !important;
        }

        div.stButton > button:first-child:hover {
            background-color: #e0e0e0 !important;
            transform: translateY(-1px) !important;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
        }

        div.stButton > button:first-child:active {
            transform: translateY(0px) !important;
            background-color: #d0d0d0 !important;
        }
        </style>
        """)
        if st.button("Sync Selected Questions to Download"):
            sync_selected_questions_to_download(chapter_or_query, questions_dict)

    with col2_download:
        if st.button("Clear Selected Questions"):
            clear_selected_questions()


def debug_show_selected_questions():
    """Debug printout of currently selected questions per chapter."""
    if st.session_state.get('questions_to_download'):
        debug_log("โœ… Selected Questions")
        for chapter, questions_list in st.session_state['questions_to_download'].items():
            debug_log(f"{chapter}")
            for q in questions_list:
                debug_log(q['question'])