-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython code.py
More file actions
149 lines (122 loc) · 4.74 KB
/
Copy pathPython code.py
File metadata and controls
149 lines (122 loc) · 4.74 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import sys
import requests
from PyQt5 import QtWidgets, uic, QtCore
APIKEY = "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"
UI_FILE = "Fichier QT.ui"
class SearchApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi(UI_FILE, self)
self.lineEdit_2 = self.findChild(QtWidgets.QLineEdit, "lineEdit_2")
self.pushButton = self.findChild(QtWidgets.QPushButton, "pushButton")
self.pushButton_2 = self.findChild(QtWidgets.QPushButton, "pushButton_2")
self.scrollArea = self.findChild(QtWidgets.QScrollArea, "scrollArea")
self.scroll_widget = self.scrollArea.widget()
self.verticalLayout_results = QtWidgets.QVBoxLayout(self.scroll_widget)
self.verticalLayout_results.setSpacing(15)
self.stackedWidget = self.findChild(QtWidgets.QStackedWidget, "stackedWidget")
self.setStyleSheet("""
QWidget {
background-color: #202124;
color: white;
}
QLineEdit {
background-color: #303134;
border-radius: 15px;
padding: 8px;
color: white;
}
QPushButton {
background-color: #303134;
border-radius: 10px;
padding: 6px;
}
QPushButton:hover {
background-color: #3c4043;
}
""")
self.lineEdit_2.setPlaceholderText("🔎 Rechercher...")
self.completer = QtWidgets.QCompleter([
"python", "qt designer", "api gratuite", "programmation",
"intelligence artificielle", "robotique", "arduino"
])
self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.lineEdit_2.setCompleter(self.completer)
self.pushButton.clicked.connect(self.do_search)
self.pushButton_2.clicked.connect(self.go_back)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
def clear_results(self):
while self.verticalLayout_results.count():
item = self.verticalLayout_results.takeAt(0)
widget = item.widget()
if widget:
widget.deleteLater()
def add_result(self, title, url, content, delay=0):
container = QtWidgets.QFrame()
container.setStyleSheet("""
QFrame {
background-color: #303134;
border-radius: 10px;
padding: 10px;
}
QFrame:hover {
background-color: #3c4043;
}
""")
layout = QtWidgets.QVBoxLayout(container)
title_label = QtWidgets.QLabel(f"<b>{title}</b>")
title_label.setStyleSheet("color: #8ab4f8; font-size: 14pt;")
title_label.setWordWrap(True)
url_label = QtWidgets.QLabel(f"<a href=\"{url}\">{url}</a>")
url_label.setOpenExternalLinks(True)
url_label.setStyleSheet("color: #34a853;")
content_label = QtWidgets.QLabel(content)
content_label.setWordWrap(True)
content_label.setStyleSheet("color: #bdc1c6;")
layout.addWidget(title_label)
layout.addWidget(url_label)
layout.addWidget(content_label)
container.setGraphicsEffect(QtWidgets.QGraphicsOpacityEffect())
animation = QtCore.QPropertyAnimation(container.graphicsEffect(), b"opacity")
animation.setDuration(500)
animation.setStartValue(0)
animation.setEndValue(1)
QtCore.QTimer.singleShot(delay, animation.start)
self.verticalLayout_results.addWidget(container)
def do_search(self):
query_text = self.lineEdit_2.text().strip()
if not query_text:
return
try:
url = "https://api.tavily.com/search"
payload = {
"api_key": APIKEY,
"query": query_text,
"search_depth": "basic",
"max_results": 5
}
response = requests.post(url, json=payload)
data = response.json()
except Exception as e:
QtWidgets.QMessageBox.warning(self, "Erreur API", str(e))
return
self.clear_results()
if "results" in data:
delay = 0
for r in data["results"]:
self.add_result(
r.get("title", ""),
r.get("url", ""),
r.get("content", ""),
delay
)
delay += 120
self.stackedWidget.setCurrentIndex(1)
def go_back(self):
self.stackedWidget.setCurrentIndex(0)
self.clear_results()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = SearchApp()
window.show()
sys.exit(app.exec_())