DawnC commited on
Commit
a94e600
1 Parent(s): ede2332

Update search_history.py

Browse files
Files changed (1) hide show
  1. search_history.py +146 -52
search_history.py CHANGED
@@ -8,6 +8,108 @@ class SearchHistoryComponent:
8
  """初始化搜索歷史組件"""
9
  self.history_manager = UserHistoryManager()
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def format_history_html(self, history_data: Optional[List[Dict]] = None) -> str:
12
  try:
13
  if history_data is None:
@@ -15,34 +117,20 @@ class SearchHistoryComponent:
15
 
16
  if not history_data:
17
  return """
18
- <div style='
19
- text-align: center;
20
- padding: 40px 20px;
21
- color: #666;
22
- background: linear-gradient(to right, rgba(66, 153, 225, 0.05), rgba(72, 187, 120, 0.05));
23
- border-radius: 10px;
24
- margin: 20px 0;
25
- '>
26
- <p style='
27
- font-size: 1.1em;
28
- margin: 0;
29
- background: linear-gradient(90deg, #4299e1, #48bb78);
30
- -webkit-background-clip: text;
31
- -webkit-text-fill-color: transparent;
32
- font-weight: 600;
33
- '>
34
- No search history yet. Try making some breed recommendations!
35
- </p>
36
  </div>
37
  """
38
 
39
  html = "<div class='history-container'>"
40
-
 
41
  for entry in reversed(history_data):
42
  timestamp = entry.get('timestamp', 'Unknown time')
43
  search_type = entry.get('search_type', 'criteria')
44
- results = entry.get('results', [])
45
-
 
46
  html += f"""
47
  <div class="history-entry">
48
  <div class="history-header" style="border-left: 4px solid #4299e1; padding-left: 10px;">
@@ -53,50 +141,56 @@ class SearchHistoryComponent:
53
  </div>
54
  """
55
 
 
56
  if search_type == "criteria":
57
  prefs = entry.get('preferences', {})
58
  html += f"""
59
  <div class="params-list" style="background: #f8fafc; padding: 16px; border-radius: 8px; margin-bottom: 16px;">
60
- <h4 style="font-size: 1.1em; font-weight: 600; color: #2D3748; margin-bottom: 12px; border-bottom: 2px solid #E2E8F0; padding-bottom: 8px;">Search Parameters:</h4>
61
- <ul style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; list-style: none; padding: 0;">
62
- <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Living Space:</span> {prefs.get('living_space', 'N/A')}</li>
63
- <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Exercise Time:</span> {prefs.get('exercise_time', 'N/A')} minutes</li>
64
- <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Grooming:</span> {prefs.get('grooming_commitment', 'N/A')}</li>
65
- <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Experience:</span> {prefs.get('experience_level', 'N/A')}</li>
66
- <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Children at Home:</span> {"Yes" if prefs.get('has_children') else "No"}</li>
67
- <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Noise Tolerance:</span> {prefs.get('noise_tolerance', 'N/A')}</li>
68
  </ul>
69
  </div>
70
  """
71
-
72
- # 結果顯示邏輯
73
- html += """
74
- <div class="results-list">
75
- <h4 style="font-size: 1.1em; font-weight: 600; color: #2D3748; margin-bottom: 12px; border-bottom: 2px solid #E2E8F0; padding-bottom: 8px;">Top 10 Breed Matches:</h4>
76
- <div class="breed-list">
77
- """
78
-
79
- if results:
80
- for i, result in enumerate(results[:10], 1):
81
- breed_name = result.get('breed', 'Unknown breed').replace('_', ' ')
82
- score = result.get('overall_score', result.get('final_score', 0))
83
- html += f"""
 
 
 
 
84
  <div class="breed-item" style="margin-bottom: 8px;">
85
  <div class="breed-info" style="display: flex; align-items: center; justify-content: space-between; padding: 8px; background: #f8fafc; border-radius: 6px;">
86
  <span class="breed-rank" style="background: linear-gradient(135deg, #4299e1, #48bb78); color: white; padding: 4px 10px; border-radius: 6px; font-weight: 600; min-width: 40px; text-align: center;">#{i}</span>
87
- <span class="breed-name" style="font-weight: 500; color: #2D3748; font-size: 1.05em; margin: 0 12px;">{breed_name}</span>
88
- <span class="breed-score" style="background: #F0FFF4; color: #48BB78; padding: 4px 8px; border-radius: 4px; font-weight: 600;">{score*100:.1f}%</span>
89
  </div>
90
  </div>
91
- """
92
-
93
- html += """
 
94
  </div>
95
- </div>
96
- </div>
97
- """
98
 
99
- html += "</div>"
 
 
100
  return html
101
 
102
  except Exception as e:
@@ -107,7 +201,7 @@ class SearchHistoryComponent:
107
  Error formatting history. Please try refreshing the page.
108
  <br>Error details: {str(e)}
109
  </div>
110
- """
111
 
112
  def clear_history(self) -> str:
113
  """清除所有搜尋歷史"""
 
8
  """初始化搜索歷史組件"""
9
  self.history_manager = UserHistoryManager()
10
 
11
+ # def format_history_html(self, history_data: Optional[List[Dict]] = None) -> str:
12
+ # try:
13
+ # if history_data is None:
14
+ # history_data = self.history_manager.get_history()
15
+
16
+ # if not history_data:
17
+ # return """
18
+ # <div style='
19
+ # text-align: center;
20
+ # padding: 40px 20px;
21
+ # color: #666;
22
+ # background: linear-gradient(to right, rgba(66, 153, 225, 0.05), rgba(72, 187, 120, 0.05));
23
+ # border-radius: 10px;
24
+ # margin: 20px 0;
25
+ # '>
26
+ # <p style='
27
+ # font-size: 1.1em;
28
+ # margin: 0;
29
+ # background: linear-gradient(90deg, #4299e1, #48bb78);
30
+ # -webkit-background-clip: text;
31
+ # -webkit-text-fill-color: transparent;
32
+ # font-weight: 600;
33
+ # '>
34
+ # No search history yet. Try making some breed recommendations!
35
+ # </p>
36
+ # </div>
37
+ # """
38
+
39
+ # html = "<div class='history-container'>"
40
+
41
+ # for entry in reversed(history_data):
42
+ # timestamp = entry.get('timestamp', 'Unknown time')
43
+ # search_type = entry.get('search_type', 'criteria')
44
+ # results = entry.get('results', [])
45
+
46
+ # html += f"""
47
+ # <div class="history-entry">
48
+ # <div class="history-header" style="border-left: 4px solid #4299e1; padding-left: 10px;">
49
+ # <span class="timestamp">🕒 {timestamp}</span>
50
+ # <span class="search-type" style="color: #4299e1; font-weight: bold; margin-left: 10px;">
51
+ # Search History
52
+ # </span>
53
+ # </div>
54
+ # """
55
+
56
+ # if search_type == "criteria":
57
+ # prefs = entry.get('preferences', {})
58
+ # html += f"""
59
+ # <div class="params-list" style="background: #f8fafc; padding: 16px; border-radius: 8px; margin-bottom: 16px;">
60
+ # <h4 style="font-size: 1.1em; font-weight: 600; color: #2D3748; margin-bottom: 12px; border-bottom: 2px solid #E2E8F0; padding-bottom: 8px;">Search Parameters:</h4>
61
+ # <ul style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; list-style: none; padding: 0;">
62
+ # <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Living Space:</span> {prefs.get('living_space', 'N/A')}</li>
63
+ # <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Exercise Time:</span> {prefs.get('exercise_time', 'N/A')} minutes</li>
64
+ # <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Grooming:</span> {prefs.get('grooming_commitment', 'N/A')}</li>
65
+ # <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Experience:</span> {prefs.get('experience_level', 'N/A')}</li>
66
+ # <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Children at Home:</span> {"Yes" if prefs.get('has_children') else "No"}</li>
67
+ # <li><span class="param-label" style="font-weight: 600; color: #2D3748;">Noise Tolerance:</span> {prefs.get('noise_tolerance', 'N/A')}</li>
68
+ # </ul>
69
+ # </div>
70
+ # """
71
+
72
+ # # 結果顯示邏輯
73
+ # html += """
74
+ # <div class="results-list">
75
+ # <h4 style="font-size: 1.1em; font-weight: 600; color: #2D3748; margin-bottom: 12px; border-bottom: 2px solid #E2E8F0; padding-bottom: 8px;">Top 10 Breed Matches:</h4>
76
+ # <div class="breed-list">
77
+ # """
78
+
79
+ # if results:
80
+ # for i, result in enumerate(results[:10], 1):
81
+ # breed_name = result.get('breed', 'Unknown breed').replace('_', ' ')
82
+ # score = result.get('overall_score', result.get('final_score', 0))
83
+ # html += f"""
84
+ # <div class="breed-item" style="margin-bottom: 8px;">
85
+ # <div class="breed-info" style="display: flex; align-items: center; justify-content: space-between; padding: 8px; background: #f8fafc; border-radius: 6px;">
86
+ # <span class="breed-rank" style="background: linear-gradient(135deg, #4299e1, #48bb78); color: white; padding: 4px 10px; border-radius: 6px; font-weight: 600; min-width: 40px; text-align: center;">#{i}</span>
87
+ # <span class="breed-name" style="font-weight: 500; color: #2D3748; font-size: 1.05em; margin: 0 12px;">{breed_name}</span>
88
+ # <span class="breed-score" style="background: #F0FFF4; color: #48BB78; padding: 4px 8px; border-radius: 4px; font-weight: 600;">{score*100:.1f}%</span>
89
+ # </div>
90
+ # </div>
91
+ # """
92
+
93
+ # html += """
94
+ # </div>
95
+ # </div>
96
+ # </div>
97
+ # """
98
+
99
+ # html += "</div>"
100
+ # return html
101
+
102
+ # except Exception as e:
103
+ # print(f"Error formatting history: {str(e)}")
104
+ # print(traceback.format_exc())
105
+ # return f"""
106
+ # <div style='text-align: center; padding: 20px; color: #dc2626;'>
107
+ # Error formatting history. Please try refreshing the page.
108
+ # <br>Error details: {str(e)}
109
+ # </div>
110
+ # """
111
+
112
+
113
  def format_history_html(self, history_data: Optional[List[Dict]] = None) -> str:
114
  try:
115
  if history_data is None:
 
117
 
118
  if not history_data:
119
  return """
120
+ <div style='text-align: center; padding: 40px 20px;'>
121
+ <p>No search history yet. Try making some breed recommendations!</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  </div>
123
  """
124
 
125
  html = "<div class='history-container'>"
126
+
127
+ # 對歷史記錄進行反轉,最新的顯示在前面
128
  for entry in reversed(history_data):
129
  timestamp = entry.get('timestamp', 'Unknown time')
130
  search_type = entry.get('search_type', 'criteria')
131
+ results = entry.get('results', []) # 確保我們有結果資料
132
+
133
+ # 顯示時間戳記和搜尋類型
134
  html += f"""
135
  <div class="history-entry">
136
  <div class="history-header" style="border-left: 4px solid #4299e1; padding-left: 10px;">
 
141
  </div>
142
  """
143
 
144
+ # 顯示搜尋參數
145
  if search_type == "criteria":
146
  prefs = entry.get('preferences', {})
147
  html += f"""
148
  <div class="params-list" style="background: #f8fafc; padding: 16px; border-radius: 8px; margin-bottom: 16px;">
149
+ <h4 style="margin-bottom: 12px;">Search Parameters:</h4>
150
+ <ul style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px;">
151
+ <li>Living Space: {prefs.get('living_space', 'N/A')}</li>
152
+ <li>Exercise Time: {prefs.get('exercise_time', 'N/A')} minutes</li>
153
+ <li>Grooming: {prefs.get('grooming_commitment', 'N/A')}</li>
154
+ <li>Experience: {prefs.get('experience_level', 'N/A')}</li>
155
+ <li>Children at Home: {"Yes" if prefs.get('has_children') else "No"}</li>
156
+ <li>Noise Tolerance: {prefs.get('noise_tolerance', 'N/A')}</li>
157
  </ul>
158
  </div>
159
  """
160
+
161
+ # 關鍵修改:確保結果部分始終顯示
162
+ if results: # 只有在有結果時才顯示結果區域
163
+ html += """
164
+ <div class="results-list" style="margin-top: 16px;">
165
+ <h4 style="margin-bottom: 12px;">Top 10 Breed Matches:</h4>
166
+ <div class="breed-list">
167
+ """
168
+
169
+ # 顯示每個推薦結果
170
+ for i, result in enumerate(results[:10], 1):
171
+ breed = result.get('breed', 'Unknown breed')
172
+ score = result.get('overall_score', 0) # 改用 overall_score
173
+ if isinstance(score, (int, float)): # 確保分數是數字
174
+ score = float(score) * 100 # 轉換為百分比
175
+
176
+ html += f"""
177
  <div class="breed-item" style="margin-bottom: 8px;">
178
  <div class="breed-info" style="display: flex; align-items: center; justify-content: space-between; padding: 8px; background: #f8fafc; border-radius: 6px;">
179
  <span class="breed-rank" style="background: linear-gradient(135deg, #4299e1, #48bb78); color: white; padding: 4px 10px; border-radius: 6px; font-weight: 600; min-width: 40px; text-align: center;">#{i}</span>
180
+ <span class="breed-name" style="font-weight: 500; color: #2D3748; margin: 0 12px;">{breed.replace('_', ' ')}</span>
181
+ <span class="breed-score" style="background: #F0FFF4; color: #48BB78; padding: 4px 8px; border-radius: 4px; font-weight: 600;">{score:.1f}%</span>
182
  </div>
183
  </div>
184
+ """
185
+
186
+ html += """
187
+ </div>
188
  </div>
189
+ """
 
 
190
 
191
+ html += "</div>" # 關閉 history-entry div
192
+
193
+ html += "</div>" # 關閉 history-container div
194
  return html
195
 
196
  except Exception as e:
 
201
  Error formatting history. Please try refreshing the page.
202
  <br>Error details: {str(e)}
203
  </div>
204
+ """
205
 
206
  def clear_history(self) -> str:
207
  """清除所有搜尋歷史"""