用文心,终于一句话就写完整应用程序了。
安装vscode以后,再安装comate插件,就可以使用了。
详细链接: https://comate.baidu.com/zh

在这里插入图片描述

主程序

from flask import Flask, render_template, jsonify
import json
import os

app = Flask(__name__)

# 加载单词数据
def load_words():
    if os.path.exists('words.json'):
        with open('words.json', 'r', encoding='utf-8') as f:
            return json.load(f)
    return []

# 获取单词列表
WORDS = load_words()
current_index = 0

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/get_word')
def get_word():
    global current_index
    if not WORDS:
        return jsonify({"error": "No words available"})
    
    word = WORDS[current_index]
    current_index = (current_index + 1) % len(WORDS)
    return jsonify(word)

if __name__ == '__main__':
    app.run(debug=True)

word.json

[
    {
        "word": "apple",
        "meaning": "苹果"
    },
    {
        "word": "banana",
        "meaning": "香蕉"
    },
    {
        "word": "orange",
        "meaning": "橙子"
    },
    {
        "word": "grape",
        "meaning": "葡萄"
    },
    {
        "word": "watermelon",
        "meaning": "西瓜"
    }
]

index.html

<!DOCTYPE html>
<html>
<head>
    <title>单词学习</title>
    <meta charset="utf-8">
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        .container {
            text-align: center;
            padding: 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        #word {
            font-size: 24px;
            margin-bottom: 10px;
        }
        #meaning {
            font-size: 18px;
            color: #666;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="word">准备开始</div>
        <div id="meaning"></div>
        <button onclick="nextWord()">下一个</button>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function nextWord() {
            $.getJSON('/get_word', function(data) {
                if (data.error) {
                    $('#word').text('没有可用的单词');
                    $('#meaning').text('');
                    return;
                }
                $('#word').text(data.word);
                $('#meaning').text(data.meaning);
            });
        }

        // 页面加载时获取第一个单词
        $(document).ready(function() {
            nextWord();
        });
    </script>
</body>
</html>

运行

% python app.py      
 * Serving Flask app 'app'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 719-496-366
127.0.0.1 - - [11/Mar/2025 17:24:51] "GET /project13 HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2025 17:24:52] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2025 17:24:57] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:57] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:58] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:59] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:59] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:59] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:59] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:24:59] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:25:00] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:25:00] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:25:00] "GET /get_word HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2025 17:25:00] "GET /get_word HTTP/1.1" 200 -

在这里插入图片描述

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐