-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
891 lines (754 loc) · 31.1 KB
/
Copy pathchatbot.py
File metadata and controls
891 lines (754 loc) · 31.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# Must be imported before PyQt5 — importing torch after PyQt on Windows
# triggers a known DLL initialization bug (see pytorch/pytorch#166628).
import torch # noqa: F401
import sys
import os
import pyperclip
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QTextEdit,
QPushButton, QComboBox, QLabel, QMessageBox, QStackedWidget, QFrame,
QTreeWidget, QTreeWidgetItem, QTabWidget, QProgressBar, QSizePolicy,
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QRegExp
from PyQt5.QtGui import QPainter, QPen, QColor, QFont, QSyntaxHighlighter, QTextCharFormat
from lessons import lessons
from grader.grading import grade_submission, GradeResult
from progress.db import (
init_db, record_attempt, get_struggling_topics, get_dashboard_stats,
get_completed_topics, DEFAULT_LEARNER_ID,
)
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "progress.db")
# ---------------------------------------------------------------- Color palette
COLORS = {
"bg": "#0f1117",
"panel": "#171a24",
"card": "#1c2030",
"border": "#2a2e3d",
"text": "#e5e7eb",
"text_dim": "#9ca3af",
"green": "#22c55e",
"orange": "#f59e0b",
"purple": "#a855f7",
"blue": "#3b82f6",
"red": "#ef4444",
}
LEVEL_COLORS = {
"Beginner": COLORS["green"],
"Intermediate": COLORS["orange"],
"Advanced": COLORS["purple"],
}
STYLESHEET = f"""
QWidget {{
background-color: {COLORS["bg"]};
color: {COLORS["text"]};
font-family: "Segoe UI", "Cantarell", sans-serif;
font-size: 13px;
}}
QFrame#card, QFrame#sidebar, QFrame#topBar {{
background-color: {COLORS["panel"]};
border: 1px solid {COLORS["border"]};
border-radius: 10px;
}}
QLabel#pageTitle {{
font-size: 20px;
font-weight: bold;
}}
QLabel#sectionTitle {{
font-size: 14px;
font-weight: bold;
color: {COLORS["text_dim"]};
}}
QLabel#statNumber {{
font-size: 22px;
font-weight: bold;
}}
QLabel#statLabel {{
color: {COLORS["text_dim"]};
font-size: 11px;
}}
QLabel#levelBadge {{
background-color: {COLORS["green"]};
color: #0f1117;
border-radius: 8px;
padding: 2px 10px;
font-weight: bold;
}}
QPushButton {{
background-color: {COLORS["card"]};
border: 1px solid {COLORS["border"]};
border-radius: 8px;
padding: 8px 14px;
}}
QPushButton:hover {{
border-color: {COLORS["blue"]};
}}
QPushButton#primaryButton {{
background-color: {COLORS["green"]};
color: #0f1117;
font-weight: bold;
border: none;
}}
QPushButton#primaryButton:hover {{
background-color: #16a34a;
}}
QComboBox, QTextEdit {{
background-color: {COLORS["card"]};
border: 1px solid {COLORS["border"]};
border-radius: 8px;
padding: 6px;
}}
QTreeWidget {{
background-color: {COLORS["panel"]};
border: none;
outline: none;
}}
QTreeWidget::item {{
padding: 5px 4px;
border-radius: 6px;
font-size: 12px;
}}
QTreeWidget::item:selected {{
background-color: {COLORS["card"]};
color: {COLORS["green"]};
}}
QTabWidget::pane {{
border: 1px solid {COLORS["border"]};
border-radius: 8px;
top: -1px;
}}
QTabBar::tab {{
background-color: {COLORS["panel"]};
padding: 8px 16px;
margin-right: 4px;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
color: {COLORS["text_dim"]};
}}
QTabBar::tab:selected {{
background-color: {COLORS["card"]};
color: {COLORS["green"]};
font-weight: bold;
}}
QProgressBar {{
background-color: {COLORS["card"]};
border: none;
border-radius: 6px;
height: 12px;
text-align: center;
color: {COLORS["text"]};
}}
QProgressBar::chunk {{
border-radius: 6px;
}}
QProgressBar#beginnerBar::chunk {{ background-color: {COLORS["green"]}; }}
QProgressBar#intermediateBar::chunk {{ background-color: {COLORS["orange"]}; }}
QProgressBar#advancedBar::chunk {{ background-color: {COLORS["purple"]}; }}
QScrollBar:vertical {{
background: {COLORS["panel"]};
width: 14px;
margin: 0px;
border-radius: 7px;
}}
QScrollBar::handle:vertical {{
background: {COLORS["border"]};
min-height: 24px;
border-radius: 6px;
margin: 2px;
}}
QScrollBar::handle:vertical:hover {{
background: {COLORS["blue"]};
}}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{
height: 0px;
border: none;
}}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{
background: none;
}}
QScrollBar:horizontal {{
background: {COLORS["panel"]};
height: 14px;
margin: 0px;
border-radius: 7px;
}}
QScrollBar::handle:horizontal {{
background: {COLORS["border"]};
min-width: 24px;
border-radius: 6px;
margin: 2px;
}}
QScrollBar::handle:horizontal:hover {{
background: {COLORS["blue"]};
}}
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {{
width: 0px;
border: none;
}}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {{
background: none;
}}
"""
# ---------------------------------------------------------------- Syntax highlighting
class PythonHighlighter(QSyntaxHighlighter):
"""
A lightweight, regex-based Python syntax highlighter — not a full
tokenizer, but enough to give the code sample a professional look
(keywords, strings, comments, numbers colored distinctly).
"""
def __init__(self, document):
super().__init__(document)
self._rules = []
keyword_format = QTextCharFormat()
keyword_format.setForeground(QColor("#c678dd"))
keyword_format.setFontWeight(QFont.Bold)
keywords = [
"def", "return", "if", "elif", "else", "for", "while", "in", "not",
"and", "or", "class", "import", "from", "as", "try", "except",
"finally", "raise", "with", "yield", "lambda", "pass", "break",
"continue", "global", "nonlocal", "is", "None", "True", "False",
"self", "assert", "del", "async", "await",
]
for kw in keywords:
self._rules.append((QRegExp(r"\b" + kw + r"\b"), keyword_format))
string_format = QTextCharFormat()
string_format.setForeground(QColor("#98c379"))
self._rules.append((QRegExp(r'"[^"\\]*(\\.[^"\\]*)*"'), string_format))
self._rules.append((QRegExp(r"'[^'\\]*(\\.[^'\\]*)*'"), string_format))
number_format = QTextCharFormat()
number_format.setForeground(QColor("#d19a66"))
self._rules.append((QRegExp(r"\b[0-9]+\.?[0-9]*\b"), number_format))
# Comments last, so a '#' inside a string doesn't get overridden by this rule
comment_format = QTextCharFormat()
comment_format.setForeground(QColor("#5c6370"))
comment_format.setFontItalic(True)
self._rules.append((QRegExp(r"#[^\n]*"), comment_format))
def highlightBlock(self, text):
for pattern, fmt in self._rules:
index = pattern.indexIn(text)
while index >= 0:
length = pattern.matchedLength()
self.setFormat(index, length, fmt)
index = pattern.indexIn(text, index + length)
class PythonCodeEditor(QTextEdit):
"""
A QTextEdit that auto-indents new lines the way Python source
normally looks: pressing Enter keeps the same indentation as the
line you were on, and adds one extra level (4 spaces) if that line
ends with a colon (the start of an if/for/def/class/... block).
"""
def keyPressEvent(self, event):
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
cursor = self.textCursor()
current_line = cursor.block().text()
# Leading whitespace of the current line is carried over as-is
indent = current_line[:len(current_line) - len(current_line.lstrip(" "))]
# One extra level after a line that opens a new block
if current_line.rstrip().endswith(":"):
indent += " "
super().keyPressEvent(event) # actually insert the newline
self.insertPlainText(indent) # then insert the computed indent
return
super().keyPressEvent(event)
# ---------------------------------------------------------------- Circular progress widget
class CircularProgress(QWidget):
"""A simple ring-shaped progress indicator with a centered percentage label."""
def __init__(self, value=0, size=120, thickness=12, color=COLORS["green"], parent=None):
super().__init__(parent)
self._value = value
self._size = size
self._thickness = thickness
self._color = QColor(color)
self.setFixedSize(size, size)
def set_value(self, value):
self._value = max(0, min(100, value))
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
margin = self._thickness / 2
rect = self.rect().adjusted(int(margin), int(margin), -int(margin), -int(margin))
bg_pen = QPen(QColor(COLORS["border"]))
bg_pen.setWidth(self._thickness)
bg_pen.setCapStyle(Qt.RoundCap)
painter.setPen(bg_pen)
painter.drawArc(rect, 0, 360 * 16)
fg_pen = QPen(self._color)
fg_pen.setWidth(self._thickness)
fg_pen.setCapStyle(Qt.RoundCap)
painter.setPen(fg_pen)
span = int(self._value / 100 * 360 * 16)
painter.drawArc(rect, 90 * 16, -span)
painter.setPen(QColor(COLORS["text"]))
font = painter.font()
font.setPointSize(15)
font.setBold(True)
painter.setFont(font)
painter.drawText(self.rect(), Qt.AlignCenter, f"{int(self._value)}%")
def _stat_card(number_text: str, label_text: str) -> QFrame:
card = QFrame()
card.setObjectName("card")
layout = QVBoxLayout(card)
number = QLabel(number_text)
number.setObjectName("statNumber")
number.setAlignment(Qt.AlignCenter)
label = QLabel(label_text)
label.setObjectName("statLabel")
label.setAlignment(Qt.AlignCenter)
layout.addWidget(number)
layout.addWidget(label)
return card
# ---------------------------------------------------------------- Grading worker (background thread)
class GradingWorker(QThread):
"""
Runs grade_submission (which involves running the sandbox and can take
up to a few seconds) on a separate thread — so the main UI doesn't
freeze while the code is being checked.
"""
finished = pyqtSignal(object) # GradeResult
def __init__(self, topic: str, user_code: str):
super().__init__()
self.topic = topic
self.user_code = user_code
def run(self):
result = grade_submission(self.topic, self.user_code, lessons)
self.finished.emit(result)
class AIWorker(QThread):
"""
Runs answer_question on a separate thread — so the main UI doesn't
freeze while waiting for a response.
"""
finished = pyqtSignal(dict)
def __init__(self, question: str, current_topic: str):
super().__init__()
self.question = question
self.current_topic = current_topic
def run(self):
from ai_tutor.qa import answer_question
try:
result = answer_question(self.question, current_topic=self.current_topic)
except Exception as e:
result = {
"answer": f"⚠️ Couldn't reach the AI tutor: {e}",
"grounded": False,
"sources": [],
}
self.finished.emit(result)
# ---------------------------------------------------------------- Dashboard page
class DashboardPage(QWidget):
def __init__(self, on_start_learning):
super().__init__()
self._on_start_learning = on_start_learning
layout = QVBoxLayout(self)
layout.setContentsMargins(24, 24, 24, 24)
layout.setSpacing(16)
title = QLabel("👋 Welcome back!")
title.setObjectName("pageTitle")
layout.addWidget(title)
subtitle = QLabel("Let's continue your Python learning journey.")
subtitle.setStyleSheet(f"color: {COLORS['text_dim']};")
layout.addWidget(subtitle)
# --- Level progress card ---
level_card = QFrame()
level_card.setObjectName("card")
level_layout = QVBoxLayout(level_card)
level_title = QLabel("Your Level Progress")
level_title.setObjectName("sectionTitle")
level_layout.addWidget(level_title)
self._level_bars = {}
self._level_labels = {}
for level, bar_id in (("Beginner", "beginnerBar"), ("Intermediate", "intermediateBar"), ("Advanced", "advancedBar")):
row = QHBoxLayout()
name_label = QLabel(level)
name_label.setFixedWidth(100)
bar = QProgressBar()
bar.setObjectName(bar_id)
bar.setRange(0, 100)
bar.setTextVisible(False)
count_label = QLabel("0 / 0")
count_label.setFixedWidth(70)
row.addWidget(name_label)
row.addWidget(bar)
row.addWidget(count_label)
level_layout.addLayout(row)
self._level_bars[level] = bar
self._level_labels[level] = count_label
layout.addWidget(level_card)
# --- Overall ring + stat cards ---
stats_row = QHBoxLayout()
stats_row.setSpacing(16)
ring_card = QFrame()
ring_card.setObjectName("card")
ring_layout = QVBoxLayout(ring_card)
ring_title = QLabel("Overall Progress")
ring_title.setObjectName("sectionTitle")
ring_title.setAlignment(Qt.AlignCenter)
self._ring = CircularProgress(value=0)
ring_inner = QHBoxLayout()
ring_inner.addStretch()
ring_inner.addWidget(self._ring)
ring_inner.addStretch()
ring_layout.addWidget(ring_title)
ring_layout.addLayout(ring_inner)
stats_row.addWidget(ring_card, stretch=1)
cards_grid = QGridLayout()
cards_grid.setSpacing(12)
self._lessons_card = _stat_card("0", "Lessons Completed")
self._exercises_card = _stat_card("0", "Exercises Solved")
self._correct_card = _stat_card("0%", "Correct Answers")
self._streak_card = _stat_card("0 🔥", "Day Streak")
cards_grid.addWidget(self._lessons_card, 0, 0)
cards_grid.addWidget(self._exercises_card, 0, 1)
cards_grid.addWidget(self._correct_card, 1, 0)
cards_grid.addWidget(self._streak_card, 1, 1)
cards_wrapper = QWidget()
cards_wrapper.setLayout(cards_grid)
stats_row.addWidget(cards_wrapper, stretch=2)
layout.addLayout(stats_row)
start_button = QPushButton("Start Learning →")
start_button.setObjectName("primaryButton")
start_button.clicked.connect(self._on_start_learning)
layout.addWidget(start_button)
# --- Topics that need more practice ---
practice_card = QFrame()
practice_card.setObjectName("card")
practice_layout = QVBoxLayout(practice_card)
practice_title = QLabel("📊 Topics to Practice")
practice_title.setObjectName("sectionTitle")
practice_layout.addWidget(practice_title)
self._practice_list_layout = QVBoxLayout()
practice_layout.addLayout(self._practice_list_layout)
layout.addWidget(practice_card, stretch=1)
def _rebuild_practice_list(self):
# clear any previously shown rows before repopulating
while self._practice_list_layout.count():
child = self._practice_list_layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
struggling = get_struggling_topics(DB_PATH, learner_id=DEFAULT_LEARNER_ID)
if not struggling:
placeholder = QLabel(
"Complete a few exercises and personalized suggestions will show up here."
)
placeholder.setStyleSheet(f"color: {COLORS['text_dim']};")
placeholder.setWordWrap(True)
self._practice_list_layout.addWidget(placeholder)
return
for stat in struggling:
pct = round(stat.pass_rate * 100)
text = f"• {stat.topic} — {pct}% success ({stat.total_attempts} attempts)"
if stat.most_common_error:
text += f" — most common error: {stat.most_common_error}"
row = QLabel(text)
row.setWordWrap(True)
self._practice_list_layout.addWidget(row)
def refresh(self):
stats = get_dashboard_stats(DB_PATH, lessons, learner_id=DEFAULT_LEARNER_ID)
for level, bar in self._level_bars.items():
total, done = stats["level_progress"].get(level, (0, 0))
percent = int(done / total * 100) if total else 0
bar.setValue(percent)
self._level_labels[level].setText(f"{done} / {total}")
self._ring.set_value(stats["overall_percent"])
self._update_card(self._lessons_card, str(stats["lessons_completed"]))
self._update_card(self._exercises_card, str(stats["exercises_solved"]))
self._update_card(self._correct_card, f"{int(stats['correct_percent'])}%")
self._update_card(self._streak_card, f"{stats['day_streak']} 🔥")
self._rebuild_practice_list()
@staticmethod
def _update_card(card: QFrame, number_text: str):
number_label = card.findChild(QLabel, "statNumber")
if number_label:
number_label.setText(number_text)
# ---------------------------------------------------------------- Lesson page
class LessonPage(QWidget):
def __init__(self, on_back_to_dashboard):
super().__init__()
self._on_back_to_dashboard = on_back_to_dashboard
self._current_topic = None
self._worker = None # kept as a reference so GC doesn't collect it mid-run
self._ai_worker = None # same reason, for the AI worker
self._ai_request_id = 0 # incremented each time the lesson changes, to ignore stale responses
self._failed_attempts = 0 # resets each time a new lesson loads; tracks consecutive wrong tries
root_layout = QHBoxLayout(self)
root_layout.setContentsMargins(16, 16, 16, 16)
root_layout.setSpacing(16)
# --- Sidebar ---
sidebar = QFrame()
sidebar.setObjectName("sidebar")
sidebar.setFixedWidth(290)
sidebar_layout = QVBoxLayout(sidebar)
back_button = QPushButton("← Back to Dashboard")
back_button.clicked.connect(self._on_back_to_dashboard)
sidebar_layout.addWidget(back_button)
self._tree = QTreeWidget()
self._tree.setHeaderHidden(True)
self._tree.itemClicked.connect(self._on_tree_item_clicked)
sidebar_layout.addWidget(self._tree)
root_layout.addWidget(sidebar)
# --- Main content ---
content = QVBoxLayout()
content.setSpacing(12)
header_row = QHBoxLayout()
self._title_label = QLabel("Select a topic")
self._title_label.setObjectName("pageTitle")
self._level_badge = QLabel("")
self._level_badge.setObjectName("levelBadge")
header_row.addWidget(self._title_label)
header_row.addWidget(self._level_badge)
header_row.addStretch()
content.addLayout(header_row)
self._tabs = QTabWidget()
self._explanation_view = QTextEdit()
self._explanation_view.setReadOnly(True)
self._tabs.addTab(self._explanation_view, "📖 Explanation")
code_tab = QWidget()
code_layout = QVBoxLayout(code_tab)
self._code_view = QTextEdit()
self._code_view.setReadOnly(True)
self._code_view.setFontFamily("Consolas")
self._highlighter = PythonHighlighter(self._code_view.document())
self._copy_button = QPushButton("📋 Copy code")
self._copy_button.clicked.connect(self.copy_code)
code_layout.addWidget(self._code_view)
code_layout.addWidget(self._copy_button)
self._tabs.addTab(code_tab, "💻 Code Example")
exercise_tab = QWidget()
exercise_layout = QVBoxLayout(exercise_tab)
self._exercise_label = QLabel("")
self._exercise_label.setWordWrap(True)
exercise_layout.addWidget(self._exercise_label)
exercise_layout.addWidget(QLabel("Your answer:"))
self._answer_input = PythonCodeEditor()
self._answer_input.setFontFamily("Consolas")
self._answer_highlighter = PythonHighlighter(self._answer_input.document())
exercise_layout.addWidget(self._answer_input)
button_row = QHBoxLayout()
self._check_button = QPushButton("Check Answer ✅")
self._check_button.setObjectName("primaryButton")
self._check_button.clicked.connect(self.check_answer)
button_row.addWidget(self._check_button)
self._show_solution_button = QPushButton("Show Solution 🔍")
self._show_solution_button.clicked.connect(self.reveal_solution)
self._show_solution_button.setVisible(False) # only appears after repeated wrong attempts
button_row.addWidget(self._show_solution_button)
exercise_layout.addLayout(button_row)
self._status_label = QLabel("")
exercise_layout.addWidget(self._status_label)
self._result_view = QTextEdit()
self._result_view.setReadOnly(True)
exercise_layout.addWidget(self._result_view)
self._tabs.addTab(exercise_tab, "📝 Exercise")
ai_tab = QWidget()
ai_layout = QVBoxLayout(ai_tab)
ai_layout.addWidget(QLabel("Ask a question about this lesson:"))
self._ai_question_input = QTextEdit()
self._ai_question_input.setFixedHeight(60)
ai_layout.addWidget(self._ai_question_input)
self._ai_ask_button = QPushButton("Ask AI 🤖")
self._ai_ask_button.setObjectName("primaryButton")
self._ai_ask_button.clicked.connect(self.ask_ai)
ai_layout.addWidget(self._ai_ask_button)
self._ai_status_label = QLabel("")
ai_layout.addWidget(self._ai_status_label)
self._ai_source_badge = QLabel("")
self._ai_source_badge.setWordWrap(True)
ai_layout.addWidget(self._ai_source_badge)
self._ai_answer_view = QTextEdit()
self._ai_answer_view.setReadOnly(True)
ai_layout.addWidget(self._ai_answer_view)
self._tabs.addTab(ai_tab, "🤖 Ask AI")
content.addWidget(self._tabs)
root_layout.addLayout(content, stretch=1)
self._populate_tree()
# ---------------------------------------------------------------- Sidebar / navigation
def _populate_tree(self):
self._tree.clear()
completed = get_completed_topics(DB_PATH, learner_id=DEFAULT_LEARNER_ID)
for level in ("Beginner", "Intermediate", "Advanced"):
level_item = QTreeWidgetItem([level])
level_item.setFlags(Qt.ItemIsEnabled) # not selectable, just a section header
font = level_item.font(0)
font.setBold(True)
level_item.setFont(0, font)
level_item.setForeground(0, QColor(LEVEL_COLORS[level]))
for topic, data in lessons.items():
if data["level"] != level:
continue
mark = "✓ " if topic in completed else "○ "
topic_item = QTreeWidgetItem([mark + topic])
topic_item.setData(0, Qt.UserRole, topic)
topic_item.setToolTip(0, topic)
level_item.addChild(topic_item)
self._tree.addTopLevelItem(level_item)
level_item.setExpanded(True)
def _on_tree_item_clicked(self, item, _column):
topic = item.data(0, Qt.UserRole)
if topic:
self.show_lesson(topic)
def refresh(self):
"""Called whenever this page becomes visible, to reflect any new completions."""
self._populate_tree()
# ---------------------------------------------------------------- Lesson display
def show_lesson(self, topic: str):
self._current_topic = topic
lesson = lessons[topic]
self._title_label.setText(topic)
self._level_badge.setText(lesson["level"])
self._level_badge.setStyleSheet(
f"background-color: {LEVEL_COLORS[lesson['level']]}; color: #0f1117; "
f"border-radius: 8px; padding: 2px 10px; font-weight: bold;"
)
self._explanation_view.setPlainText(lesson["text"])
self._code_view.setPlainText(lesson["code"])
self._exercise_label.setText(lesson["exercise"])
self._result_view.clear()
self._status_label.setText("")
self._answer_input.clear()
self._failed_attempts = 0
self._show_solution_button.setVisible(False)
self._ai_question_input.clear()
self._ai_answer_view.clear()
self._ai_status_label.setText("")
self._ai_source_badge.setText("")
self._ai_ask_button.setEnabled(True)
self._ai_request_id += 1
auto_gradable = lesson.get("auto_gradable", True)
self._check_button.setEnabled(auto_gradable)
if not auto_gradable:
self._result_view.setPlainText(
"ℹ️ This exercise has a graphical interface (GUI) and can't be "
"checked automatically. Run your code and see the result yourself."
)
self._tabs.setCurrentIndex(0)
def copy_code(self):
if not self._current_topic:
return
code = lessons[self._current_topic]["code"]
try:
pyperclip.copy(code)
self._status_label.setText("✅ Code copied to clipboard.")
except Exception:
self._status_label.setText(
"⚠️ Couldn't copy to clipboard (xclip/xsel is probably not installed)."
)
# ---------------------------------------------------------------- AI tutor
def ask_ai(self):
question = self._ai_question_input.toPlainText().strip()
if not question:
self._ai_status_label.setText("⚠️ Type a question first.")
return
self._ai_ask_button.setEnabled(False)
self._ai_status_label.setText("⏳ Thinking... (this can take a few seconds)")
self._ai_source_badge.setText("")
self._ai_answer_view.clear()
request_id = self._ai_request_id
self._ai_worker = AIWorker(question, current_topic=self._current_topic)
self._ai_worker.finished.connect(
lambda result: self._on_ai_finished(request_id, result)
)
self._ai_worker.start()
def _on_ai_finished(self, request_id: int, result: dict):
if request_id != self._ai_request_id:
return # student switched lessons before this response arrived; discard it
self._ai_ask_button.setEnabled(True)
self._ai_status_label.setText("")
self._ai_answer_view.setPlainText(result["answer"])
if result["grounded"]:
sources = ", ".join(result["sources"])
self._ai_source_badge.setText(f"📚 Based on course material: {sources}")
else:
self._ai_source_badge.setText("🌐 General knowledge (not covered in this course)")
# ---------------------------------------------------------------- Grading
def check_answer(self):
if not self._current_topic:
return
user_code = self._answer_input.toPlainText()
if not user_code.strip():
self._status_label.setText("⚠️ Write an answer first.")
return
self._check_button.setEnabled(False)
self._status_label.setText("⏳ Running your code safely... (this takes a few seconds)")
topic = self._current_topic
self._worker = GradingWorker(topic, user_code)
self._worker.finished.connect(
lambda result: self._on_grading_finished(topic, user_code, result)
)
self._worker.start()
def _on_grading_finished(self, topic: str, user_code: str, result: GradeResult):
self._check_button.setEnabled(lessons[topic].get("auto_gradable", True))
self._status_label.setText("")
if result.infra_error:
self._result_view.setPlainText(f"⚠️ Internal system error: {result.infra_error}")
return
# Record the attempt FIRST, before touching the sidebar or dashboard.
# Refreshing the tree before this line was the bug: it would read
# completed topics from the database before this attempt was saved,
# so the checkmark only showed up after the *next* exercise was
# checked (which triggered the following refresh, by which point
# this attempt had already been recorded).
record_attempt(DB_PATH, topic, result, code_length=len(user_code))
explanation = lessons[topic].get("explain_answer", "")
if result.passed:
msg = "✅ Nice! Your output was correct."
if explanation:
msg += f"\n\n💡 {explanation}"
self._result_view.setPlainText(msg)
self._failed_attempts = 0
self._show_solution_button.setVisible(False)
self._populate_tree() # refresh checkmarks in the sidebar
else:
self._failed_attempts += 1
msg = "❌ Your answer isn't quite right yet.\n"
if result.user_error:
msg += f"\nRuntime error:\n{result.user_error}"
elif result.diff_hint:
msg += f"\n{result.diff_hint}"
if explanation:
msg += f"\n\n💡 Hint: {explanation}"
if self._failed_attempts >= 2:
self._show_solution_button.setVisible(True)
msg += "\n\nStill stuck? You can click \"Show Solution\" below."
self._result_view.setPlainText(msg)
def reveal_solution(self):
if not self._current_topic:
return
confirm = QMessageBox.question(
self,
"Show Solution?",
"Are you sure? Trying a bit more on your own usually helps you "
"learn better — but if you're truly stuck, here's the reference "
"solution.",
QMessageBox.Yes | QMessageBox.No,
)
if confirm != QMessageBox.Yes:
return
solution_code = lessons[self._current_topic]["answer"]
self._result_view.setPlainText(f"🔍 Reference solution:\n\n{solution_code}")
# ---------------------------------------------------------------- Main window
class ChatBotApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Python Learning Chatbot")
self.setGeometry(150, 80, 900, 650)
init_db(DB_PATH)
self._stack = QStackedWidget()
self._dashboard_page = DashboardPage(on_start_learning=self.show_lesson_page)
self._lesson_page = LessonPage(on_back_to_dashboard=self.show_dashboard_page)
self._stack.addWidget(self._dashboard_page)
self._stack.addWidget(self._lesson_page)
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self._stack)
self.show_dashboard_page()
def show_dashboard_page(self):
self._dashboard_page.refresh()
self._stack.setCurrentWidget(self._dashboard_page)
def show_lesson_page(self):
self._lesson_page.refresh()
self._stack.setCurrentWidget(self._lesson_page)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet(STYLESHEET)
window = ChatBotApp()
window.show()
sys.exit(app.exec_())