bounthong commited on
Commit
2d838d6
·
1 Parent(s): f0b496f

Add PostgreSQL backend for lessons

Browse files
Files changed (2) hide show
  1. app.py +83 -284
  2. requirements.txt +3 -2
app.py CHANGED
@@ -1,12 +1,16 @@
1
  """
2
  Lao Python Learning Platform
3
  Web-based Python learning for Lao secondary students
 
4
  """
5
 
 
6
  import gradio as gr
7
  from markdown_it import MarkdownIt
 
 
8
 
9
- # Pre-render markdown to HTML using markdown-it (bundled with Gradio)
10
  md = MarkdownIt("commonmark", {"html": True})
11
 
12
  def render_md(text):
@@ -14,291 +18,47 @@ def render_md(text):
14
  html = md.render(text)
15
  return f'<div class="lesson-html">{html}</div>'
16
 
17
- # Custom CSS for Lao script + lesson styling
18
- custom_css = """
19
- @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Lao:wght@400;500;600;700&display=swap');
20
-
21
- body, .gradio-container, textarea, input, button, select, .tab-item, .block {
22
- font-family: 'Noto Sans Lao', sans-serif !important;
23
- }
24
-
25
- .lesson-html {
26
- font-family: 'Noto Sans Lao', sans-serif !important;
27
- line-height: 1.8;
28
- padding: 10px;
29
- }
30
- .lesson-html h1 { font-size: 1.8em; font-weight: 700; margin-bottom: 0.5em; color: #1a1a2e; }
31
- .lesson-html h2 { font-size: 1.4em; font-weight: 600; margin-top: 1.2em; margin-bottom: 0.4em; color: #16213e; }
32
- .lesson-html p { margin-bottom: 0.8em; }
33
- .lesson-html ul, .lesson-html ol { margin-left: 1.5em; margin-bottom: 0.8em; }
34
- .lesson-html li { margin-bottom: 0.3em; }
35
- .lesson-html code {
36
- font-family: 'Courier New', monospace !important;
37
- background: #f0f0f0;
38
- padding: 2px 6px;
39
- border-radius: 4px;
40
- font-size: 0.9em;
41
- }
42
- .lesson-html pre {
43
- background: #1e1e2e;
44
- color: #cdd6f4;
45
- padding: 16px;
46
- border-radius: 8px;
47
- overflow-x: auto;
48
- margin-bottom: 1em;
49
- }
50
- .lesson-html pre code {
51
- background: none;
52
- padding: 0;
53
- color: inherit;
54
- }
55
- """
56
-
57
- # Lesson content in Lao
58
- LESSONS = {
59
- "1. ພື້ນຖານ Python": {
60
- "content": """# ບົດທີ 1: ພື້ນຖານ Python
61
-
62
- ## 1.1 ການພິມຂໍ້ຄວາມ (Print)
63
- ໃນ Python, ເຮົາໃຊ້ `print()` ເພື່ອສະແດງຜົນ:
64
-
65
- ```python
66
- print("ສະບາຍດີ!")
67
- print(10 + 5)
68
- ```
69
-
70
- ## 1.2 ຕົວປ່ຽນ (Variables)
71
- ຕົວປ່ຽນແມ່ນສະຖານທີ່ເກັບຂໍ້ມູນ:
72
-
73
- ```python
74
- name = "ສົມພູ"
75
- age = 15
76
- height = 170.5
77
-
78
- print(name)
79
- print(age)
80
- ```
81
-
82
- ## 1.3 ປະເພດຂໍ້ມູນ (Data Types)
83
- - `str` - ຂໍ້ຄວາມ
84
- - `int` - ຈຳນວນເຕັມ
85
- - `float` - ຈຳນວນທົດສະນິຍົມ
86
- - `bool` - ຄ່າຈິງ/ເທືອ
87
-
88
- ## 1.4 ການຄິດໄລ່ (Arithmetic)
89
- ```python
90
- a = 10
91
- b = 3
92
- print(a + b) # 13
93
- print(a - b) # 7
94
- print(a * b) # 30
95
- print(a / b) # 3.333...
96
- print(a // b) # 3 (ຫານລວງ)
97
- print(a % b) # 1 (ເສດ)
98
- print(a ** b) # 1000 (ລົງກຳລັງ)
99
- ```
100
- """,
101
- "example": """# ລອງຂຽນໂຄງການທຳອິດ
102
- # (input() requires stdin — in the Gradio playground it's bypassed; use the
103
- # calculator example for an interactive demo)
104
- name = "ໂລກ Python" # ໃຊ້ຕົວຢ່າງ
105
- print("ສະບາຍດີ", name)
106
- print("ຍິນດີຕ້ອນຮັບສູ່ໂລກ Python!")
107
- """
108
- },
109
-
110
- "2. ໃຊ້ງານເງື່ອນໄຂ": {
111
- "content": """# ບົດທີ 2: ໃຊ້ງານເງື່ອນໄຂ
112
-
113
- ## 2.1 If / Elif / Else
114
- ```python
115
- age = 18
116
-
117
- if age >= 18:
118
- print("ເຈົ້າເປັນຄົນຜູ້ໃຫຍ່")
119
- elif age >= 15:
120
- print("ເຈົ້າເປັນໜຸ່ມ")
121
- else:
122
- print("ເຈົ້າເປັນເດັກນ້ອຍ")
123
- ```
124
-
125
- ## 2.2 ເງື່ອນໄຂຄອມປາເລີ
126
- - `==` (ເທົ່າກັບ)
127
- - `!=` (ບໍ່ເທົ່າກັບ)
128
- - `>` (ຫຼາຍກວ່າ)
129
- - `<` (ໜ້ອຍກວ່າ)
130
- - `>=` (ຫຼາຍກວ່າ ຫຼື ເທົ່າກັບ)
131
- - `<=` (ໜ້ອຍກວ່າ ຫຼື ເທົ່າກັບ)
132
- - `and`, `or`, `not`
133
-
134
- ## 2.3 ຕົວຢ່າງ
135
- ```python
136
- score = 85
137
-
138
- if score >= 80:
139
- print("A")
140
- elif score >= 70:
141
- print("B")
142
- elif score >= 60:
143
- print("C")
144
- else:
145
- print("F")
146
- ```
147
- """,
148
- "example": """# ກວດສອບອາຍຸ (fixed for playground; remove the input() call)
149
- age = 20 # ໃສ່ຕົວເລກທີ່ຕ້ອງການ
150
-
151
- if age < 0:
152
- print("ອາຍຸບໍ່ຖືກຕ້ອງ")
153
- elif age < 13:
154
- print("ເດັກນ້ອຍ")
155
- elif age < 18:
156
- print("ໜຸ່ມ")
157
- else:
158
- print("ຜູ້ໃຫຍ່")
159
- """
160
- },
161
-
162
- "3. ລູກສອນ (Loops)": {
163
- "content": """# ບົດທີ 3: ລູກສອນ
164
-
165
- ## 3.1 For Loop
166
- ```python
167
- # ປຸ່ມ 5 ຄັ້ງ
168
- for i in range(5):
169
- print("ຄັ້ງທີ", i)
170
-
171
- # ນັບຈາກ 1 ຫາ 10
172
- for num in range(1, 11):
173
- print(num)
174
- ```
175
-
176
- ## 3.2 While Loop
177
- ```python
178
- count = 0
179
- while count < 5:
180
- print("count =", count)
181
- count += 1
182
- ```
183
-
184
- ## 3.3 Break ແລະ Continue
185
- ```python
186
- for i in range(10):
187
- if i == 3:
188
- continue # ຫຼີກລ່ຽງ 3
189
- if i == 7:
190
- break # ຢຸດທີ່ 7
191
- print(i)
192
- ```
193
- """,
194
- "example": """# ນັບຈົນກວ່າຈະພົບ 100 (no input needed)
195
- total = 0
196
- i = 1
197
- while total < 100:
198
- total += i
199
- i += 1
200
- print("ຜົນບວກ:", total)
201
- """
202
- },
203
-
204
- "4. ລິສັດ (Lists)": {
205
- "content": """# ບົດທີ 4: ລິສັດ
206
-
207
- ## 4.1 ສ້າງລິສັດ
208
- ```python
209
- fruits = ["ແຟັສ", "ຕະຫຼອດ", "ມັນ"]
210
- numbers = [1, 2, 3, 4, 5]
211
- mixed = [1, "ສີ", 3.14, True]
212
- ```
213
-
214
- ## 4.2 ເຂົ້າເຖິງອົງປະກອບ
215
- ```python
216
- fruits = ["ແຟັສ", "ຕະຫຼອດ", "ມັນ"]
217
- print(fruits[0]) # ແຟັສ
218
- print(fruits[-1]) # ມັນ
219
- ```
220
-
221
- ## 4.3 ລຽງລິສັດ
222
- ```python
223
- nums = [3, 1, 4, 1, 5, 9]
224
- nums.sort()
225
- print(nums) # [1, 1, 3, 4, 5, 9]
226
-
227
- nums.reverse()
228
- print(nums) # [9, 5, 4, 3, 1, 1]
229
- ```
230
-
231
- ## 4.4 ເພີ່ມ/ຕັດ
232
- ```python
233
- fruits = ["ແຟັສ", "ຕະຫຼອດ"]
234
- fruits.append("ມັນ") # ເພີ່ມຢູ່ທ້າຍ
235
- fruits.insert(0, "ເລືອກ") # ເພີ່ມຢູ່ຕຳແໜ່ງ
236
- fruits.remove("ແຟັສ") # ຕັດອອກ
237
- popped = fruits.pop() # ຕັດ ແລະ ກັບຄືນ
238
- ```
239
- """,
240
- "example": """# ຈັດການລາຍການ (no input — demo loop)
241
- shopping = ["ເປັດ", "ໄກ່"]
242
- print("ເລີ່ມຕົ້ນ:", shopping)
243
- shopping.append("ໄຂ່")
244
- shopping.pop()
245
- print("ຈົບ:", shopping)
246
- """
247
- },
248
-
249
- "5. ຟັງຊັນ (Functions)": {
250
- "content": """# ບົດທີ 5: ຟັງຊັນ
251
-
252
- ## 5.1 ສ້າງຟັງຊັນ
253
- ```python
254
- def say_hello():
255
- print("ສະບາຍດີ!")
256
-
257
- say_hello()
258
- ```
259
-
260
- ## 5.2 ຟັງຊັນທີ່ມີພາaráມິເຕີ
261
- ```python
262
- def greet(name):
263
- print("ສະບາຍດີ", name)
264
-
265
- greet("ສົມພູ")
266
- ```
267
-
268
- ## 5.3 ກັບຄືນຄ່າ
269
- ```python
270
- def add(a, b):
271
- return a + b
272
-
273
- result = add(5, 3)
274
- print(result) # 8
275
- ```
276
-
277
- ## 5.4 Default Parameters
278
- ```python
279
- def power(base, exp=2):
280
- return base ** exp
281
 
282
- print(power(3)) # 9
283
- print(power(2, 10)) # 1024
284
- ```
285
- """,
286
- "example": """# ຄຳນວນພື້ນທີ່ວົງມົນ (no input — fixed radius)
287
- def calculate_circle_area(radius):
288
- pi = 3.14159
289
- area = pi * radius ** 2
290
- return area
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
- r = 5.0 # ລັດສະໝີ
293
- print("ພື້ນທີ່:", calculate_circle_area(r))
294
- """
295
- }
296
- }
297
 
298
  # Pre-render all lessons to HTML at startup
299
  LESSON_HTML = {name: render_md(data["content"]) for name, data in LESSONS.items()}
300
 
301
- # Example codes for the code playground
302
  EXAMPLE_CODES = {
303
  "Hello World": 'print("ສະບາຍດີ! ລົດ Python")',
304
  "Calculator": """# ເຄື່ອງຄິດໄລ່ງ່າຍໆ (run once with fixed numbers)
@@ -350,7 +110,47 @@ def select_lesson(lesson_name):
350
  return LESSON_HTML[lesson_name]
351
  return ""
352
 
353
- first_lesson = list(LESSONS.keys())[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
 
355
  with gr.Blocks(title="ຮຽນ Python") as app:
356
  gr.Markdown("# 🐍 ຮຽນ Python")
@@ -363,7 +163,7 @@ with gr.Blocks(title="ຮຽນ Python") as app:
363
  )
364
  # Use gr.HTML instead of gr.Markdown to bypass Gradio 6.x lazy hydration bug
365
  lesson_content = gr.HTML(
366
- value=LESSON_HTML[first_lesson],
367
  show_label=False
368
  )
369
 
@@ -413,7 +213,6 @@ with gr.Blocks(title="ຮຽນ Python") as app:
413
  """)
414
 
415
  if __name__ == "__main__":
416
- import os
417
  import argparse
418
  parser = argparse.ArgumentParser()
419
  parser.add_argument("--share", action="store_true", help="Create a temporary Gradio public link")
@@ -426,4 +225,4 @@ if __name__ == "__main__":
426
  server_port=port,
427
  share=args.share,
428
  css=custom_css,
429
- )
 
1
  """
2
  Lao Python Learning Platform
3
  Web-based Python learning for Lao secondary students
4
+ Now with PostgreSQL database backend
5
  """
6
 
7
+ import os
8
  import gradio as gr
9
  from markdown_it import MarkdownIt
10
+ import psycopg2
11
+ from psycopg2.extras import RealDictCursor
12
 
13
+ # Pre-render markdown to HTML using markdown-it
14
  md = MarkdownIt("commonmark", {"html": True})
15
 
16
  def render_md(text):
 
18
  html = md.render(text)
19
  return f'<div class="lesson-html">{html}</div>'
20
 
21
+ # Database connection
22
+ def get_db_connection():
23
+ """Get database connection from environment variables."""
24
+ return psycopg2.connect(
25
+ host=os.environ.get("DB_HOST", "localhost"),
26
+ port=os.environ.get("DB_PORT", "5432"),
27
+ database=os.environ.get("DB_NAME", "lao_python_learn"),
28
+ user=os.environ.get("DB_USER", "lao_user"),
29
+ password=os.environ.get("DB_PASSWORD", "lao_learn_2026"),
30
+ cursor_factory=RealDictCursor
31
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ def load_lessons_from_db():
34
+ """Load lessons from PostgreSQL database."""
35
+ try:
36
+ conn = get_db_connection()
37
+ cur = conn.cursor()
38
+ cur.execute("SELECT id, title, slug, content_markdown, example_code, \"order\" FROM lessons ORDER BY \"order\"")
39
+ rows = cur.fetchall()
40
+ cur.close()
41
+ conn.close()
42
+
43
+ lessons = {}
44
+ for row in rows:
45
+ lessons[row["title"]] = {
46
+ "content": row["content_markdown"],
47
+ "example": row["example_code"] or ""
48
+ }
49
+ return lessons
50
+ except Exception as e:
51
+ print(f"Database error: {e}")
52
+ # Fallback to empty dict - will use empty state
53
+ return {}
54
 
55
+ # Load lessons from DB at startup
56
+ LESSONS = load_lessons_from_db()
 
 
 
57
 
58
  # Pre-render all lessons to HTML at startup
59
  LESSON_HTML = {name: render_md(data["content"]) for name, data in LESSONS.items()}
60
 
61
+ # Example codes for the code playground (static fallback)
62
  EXAMPLE_CODES = {
63
  "Hello World": 'print("ສະບາຍດີ! ລົດ Python")',
64
  "Calculator": """# ເຄື່ອງຄິດໄລ່ງ່າຍໆ (run once with fixed numbers)
 
110
  return LESSON_HTML[lesson_name]
111
  return ""
112
 
113
+ first_lesson = list(LESSONS.keys())[0] if LESSONS else ""
114
+
115
+ # Custom CSS for Lao script + lesson styling
116
+ custom_css = """
117
+ @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Lao:wght@400;500;600;700&display=swap');
118
+
119
+ body, .gradio-container, textarea, input, button, select, .tab-item, .block {
120
+ font-family: 'Noto Sans Lao', sans-serif !important;
121
+ }
122
+
123
+ .lesson-html {
124
+ font-family: 'Noto Sans Lao', sans-serif !important;
125
+ line-height: 1.8;
126
+ padding: 10px;
127
+ }
128
+ .lesson-html h1 { font-size: 1.8em; font-weight: 700; margin-bottom: 0.5em; color: #1a1a2e; }
129
+ .lesson-html h2 { font-size: 1.4em; font-weight: 600; margin-top: 1.2em; margin-bottom: 0.4em; color: #16213e; }
130
+ .lesson-html p { margin-bottom: 0.8em; }
131
+ .lesson-html ul, .lesson-html ol { margin-left: 1.5em; margin-bottom: 0.8em; }
132
+ .lesson-html li { margin-bottom: 0.3em; }
133
+ .lesson-html code {
134
+ font-family: 'Courier New', monospace !important;
135
+ background: #f0f0f0;
136
+ padding: 2px 6px;
137
+ border-radius: 4px;
138
+ font-size: 0.9em;
139
+ }
140
+ .lesson-html pre {
141
+ background: #1e1e2e;
142
+ color: #cdd6f4;
143
+ padding: 16px;
144
+ border-radius: 8px;
145
+ overflow-x: auto;
146
+ margin-bottom: 1em;
147
+ }
148
+ .lesson-html pre code {
149
+ background: none;
150
+ padding: 0;
151
+ color: inherit;
152
+ }
153
+ """
154
 
155
  with gr.Blocks(title="ຮຽນ Python") as app:
156
  gr.Markdown("# 🐍 ຮຽນ Python")
 
163
  )
164
  # Use gr.HTML instead of gr.Markdown to bypass Gradio 6.x lazy hydration bug
165
  lesson_content = gr.HTML(
166
+ value=LESSON_HTML.get(first_lesson, ""),
167
  show_label=False
168
  )
169
 
 
213
  """)
214
 
215
  if __name__ == "__main__":
 
216
  import argparse
217
  parser = argparse.ArgumentParser()
218
  parser.add_argument("--share", action="store_true", help="Create a temporary Gradio public link")
 
225
  server_port=port,
226
  share=args.share,
227
  css=custom_css,
228
+ )
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
- gradio==4.44.0
2
  huggingface_hub==0.23.2
3
  markdown-it-py==3.0.0
4
- python-dotenv==1.0.0
 
 
1
+ gradio>=4.0.0
2
  huggingface_hub==0.23.2
3
  markdown-it-py==3.0.0
4
+ python-dotenv>=1.0.0
5
+ psycopg2-binary>=2.9.0