Spaces:
Sleeping
Sleeping
Commit ·
ca86369
1
Parent(s): d3c5c3e
Update snippets to include Chinese comments and improve UI localization
Browse files
app.py
CHANGED
|
@@ -4,62 +4,117 @@ from flask import Flask, render_template, jsonify
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
#
|
|
|
|
| 8 |
SNIPPETS = [
|
| 9 |
{
|
| 10 |
"language": "Python",
|
| 11 |
-
"code": """
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
},
|
| 19 |
{
|
| 20 |
"language": "JavaScript",
|
| 21 |
-
"code": """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
let timeout;
|
| 23 |
return function(...args) {
|
| 24 |
const context = this;
|
| 25 |
clearTimeout(timeout);
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
};
|
| 28 |
}"""
|
| 29 |
},
|
| 30 |
{
|
| 31 |
"language": "Python",
|
| 32 |
-
"code": """
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
},
|
| 41 |
{
|
| 42 |
"language": "Go",
|
| 43 |
-
"code": """
|
|
|
|
| 44 |
|
| 45 |
-
import
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
func main() {
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}"""
|
| 54 |
},
|
| 55 |
{
|
| 56 |
"language": "SQL",
|
| 57 |
-
"code": """
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
]
|
| 65 |
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# 示例代码片段 (Python, JavaScript, Go, SQL)
|
| 8 |
+
# 包含中文注释,模拟真实开发场景
|
| 9 |
SNIPPETS = [
|
| 10 |
{
|
| 11 |
"language": "Python",
|
| 12 |
+
"code": """# 二分查找算法实现
|
| 13 |
+
def binary_search(arr, target):
|
| 14 |
+
left, right = 0, len(arr) - 1
|
| 15 |
+
|
| 16 |
+
while left <= right:
|
| 17 |
+
mid = (left + right) // 2
|
| 18 |
+
if arr[mid] == target:
|
| 19 |
+
return mid # 找到目标值
|
| 20 |
+
elif arr[mid] < target:
|
| 21 |
+
left = mid + 1
|
| 22 |
+
else:
|
| 23 |
+
right = mid - 1
|
| 24 |
+
|
| 25 |
+
return -1 # 未找到"""
|
| 26 |
},
|
| 27 |
{
|
| 28 |
"language": "JavaScript",
|
| 29 |
+
"code": """/**
|
| 30 |
+
* 防抖函数 (Debounce)
|
| 31 |
+
* 用于限制函数执行频率,常用于搜索框输入
|
| 32 |
+
*/
|
| 33 |
+
function debounce(func, wait) {
|
| 34 |
let timeout;
|
| 35 |
return function(...args) {
|
| 36 |
const context = this;
|
| 37 |
clearTimeout(timeout);
|
| 38 |
+
|
| 39 |
+
// 延迟执行
|
| 40 |
+
timeout = setTimeout(() => {
|
| 41 |
+
func.apply(context, args);
|
| 42 |
+
}, wait);
|
| 43 |
};
|
| 44 |
}"""
|
| 45 |
},
|
| 46 |
{
|
| 47 |
"language": "Python",
|
| 48 |
+
"code": """# Flask 路由示例
|
| 49 |
+
@app.route('/api/users', methods=['GET'])
|
| 50 |
+
def get_users():
|
| 51 |
+
# 从数据库获取用户列表
|
| 52 |
+
users = db.session.query(User).all()
|
| 53 |
+
return jsonify({
|
| 54 |
+
"count": len(users),
|
| 55 |
+
"data": [u.to_dict() for u in users]
|
| 56 |
+
})"""
|
| 57 |
},
|
| 58 |
{
|
| 59 |
"language": "Go",
|
| 60 |
+
"code": """// 并发处理示例
|
| 61 |
+
package main
|
| 62 |
|
| 63 |
+
import (
|
| 64 |
+
"fmt"
|
| 65 |
+
"sync"
|
| 66 |
+
)
|
| 67 |
|
| 68 |
func main() {
|
| 69 |
+
var wg sync.WaitGroup
|
| 70 |
+
|
| 71 |
+
// 启动 5 个协程
|
| 72 |
+
for i := 0; i < 5; i++ {
|
| 73 |
+
wg.Add(1)
|
| 74 |
+
go func(id int) {
|
| 75 |
+
defer wg.Done()
|
| 76 |
+
fmt.Printf("Worker %d starting\\n", id)
|
| 77 |
+
}(i)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
wg.Wait() // 等待所有协程完成
|
| 81 |
+
fmt.Println("All workers done")
|
| 82 |
}"""
|
| 83 |
},
|
| 84 |
{
|
| 85 |
"language": "SQL",
|
| 86 |
+
"code": """-- 查询高价值用户
|
| 87 |
+
SELECT
|
| 88 |
+
u.username,
|
| 89 |
+
COUNT(o.id) as order_count,
|
| 90 |
+
SUM(o.amount) as total_spent
|
| 91 |
+
FROM users u
|
| 92 |
+
JOIN orders o ON u.id = o.user_id
|
| 93 |
+
WHERE o.created_at >= '2024-01-01'
|
| 94 |
+
GROUP BY u.id
|
| 95 |
+
HAVING total_spent > 1000
|
| 96 |
+
ORDER BY total_spent DESC;"""
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"language": "React (JSX)",
|
| 100 |
+
"code": """// 简单的计数器组件
|
| 101 |
+
import React, { useState } from 'react';
|
| 102 |
+
|
| 103 |
+
export default function Counter() {
|
| 104 |
+
const [count, setCount] = useState(0);
|
| 105 |
+
|
| 106 |
+
return (
|
| 107 |
+
<div className="p-4 border rounded">
|
| 108 |
+
<h2 className="text-xl">当前计数: {count}</h2>
|
| 109 |
+
<button
|
| 110 |
+
onClick={() => setCount(count + 1)}
|
| 111 |
+
className="bg-blue-500 text-white px-4 py-2 mt-2"
|
| 112 |
+
>
|
| 113 |
+
增加 (+1)
|
| 114 |
+
</button>
|
| 115 |
+
</div>
|
| 116 |
+
);
|
| 117 |
+
}"""
|
| 118 |
}
|
| 119 |
]
|
| 120 |
|