-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_functions.py
More file actions
59 lines (51 loc) · 2.14 KB
/
Copy pathhelper_functions.py
File metadata and controls
59 lines (51 loc) · 2.14 KB
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
import streamlit as st
import re
import mysql.connector
import json
# --- HELPER FUNCTION: Parse Score ---
def parse_score_from_feedback(feedback_text):
match = re.search(r'\*\*?ציון:\*\*?\s*(\d+)\s*/\s*5', feedback_text)
if match:
return int(match.group(1))
return None
# --- HELPER FUNCTION: Log to Database (Connect-on-Demand version) ---
def log_event_to_mysql(session_id, event_type, details_dict, topic=None, difficulty=None, scope=None, score=None):
"""
Establishes a new connection, logs an event, and closes the connection.
"""
conn = None # Initialize conn to None to ensure it's available in the finally block
try:
# 1. Establish a new connection for this specific event
conn = mysql.connector.connect(
host=st.secrets["mysql"]["host"],
port=st.secrets["mysql"]["port"],
user=st.secrets["mysql"]["user"],
password=st.secrets["mysql"]["password"],
database=st.secrets["mysql"]["database"]
)
# 2. Use a 'with' statement for the cursor to ensure it's closed
with conn.cursor() as cursor:
details_json = json.dumps(details_dict, ensure_ascii=False)
insert_query = """
INSERT INTO session_logs (session_id, event_type, details, topic, difficulty, scope, score)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
record = (session_id, event_type, details_json, topic, difficulty, scope, score)
cursor.execute(insert_query, record)
conn.commit()
except mysql.connector.Error as err:
# Log the error to the Streamlit console for debugging
st.error(f"Database Error: {err}")
finally:
# 3. IMPORTANT: Always close the connection
if conn and conn.is_connected():
conn.close()
# --- HELPER FUNCTION: Display RTL Text ---
def st_rtl_write(text: str):
"""
A helper function to display text in Streamlit with RTL direction and right alignment.
"""
st.markdown(
f'<div style="direction: rtl; text-align: right;">{text}</div>',
unsafe_allow_html=True
)