提示信息

构建记账小工具的实战教程:Python + Flask + Jinja2

小魔女 已关注

构建一个简单的记账小工具是一个很好的学习项目,特别是如果你想熟悉Python、Flask和Jinja2的基本使用。下面是一个简化的实战教程,帮助你入门。

需求分析

  1. 用户界面:允许用户输入和查看记账信息。
  2. 功能
    • 添加新账目。
    • 查看所有账目。
    • 删除账目。
  3. 持久化:简单实现可以使用Python的内置数据结构(例如列表)来保存数据;更复杂的实现可以使用数据库。

准备环境

首先,确保你已安装Flask,你可以用以下命令安装它:

  1. pip install Flask

项目结构

  1. expense_tracker/
  2. ├── app.py
  3. ├── templates/
  4. │ ├── layout.html
  5. │ ├── index.html
  6. └── static/
  7. └── styles.css

1. 创建Flask应用(app.py)

  1. from flask import Flask, render_template, request, redirect, url_for
  2. app = Flask(__name__)
  3. # 内存中保存账目条目
  4. entries = []
  5. @app.route('/', methods=['GET', 'POST'])
  6. def index():
  7. if request.method == 'POST':
  8. # 获取表单数据
  9. description = request.form.get('description')
  10. amount = request.form.get('amount')
  11. # 添加到账目列表
  12. if description and amount:
  13. entries.append({'description': description, 'amount': float(amount)})
  14. return redirect(url_for('index'))
  15. return render_template('index.html', entries=entries)
  16. if __name__ == '__main__':
  17. app.run(debug=True)

2. 创建模板(templates/layout.html)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
  7. <title>Simple Expense Tracker</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h1>Expense Tracker</h1>
  12. {% block content %}{% endblock %}
  13. </div>
  14. </body>
  15. </html>

3. 创建首页模板(templates/index.html)

  1. {% extends 'layout.html' %}
  2. {% block content %}
  3. <form method="POST">
  4. <input type="text" name="description" placeholder="Description" required>
  5. <input type="number" step="0.01" name="amount" placeholder="Amount" required>
  6. <button type="submit">Add</button>
  7. </form>
  8. <h2>Entries</h2>
  9. <ul>
  10. {% for entry in entries %}
  11. <li>{{ entry.description }} - ${{ entry.amount }}</li>
  12. {% endfor %}
  13. </ul>
  14. {% endblock %}

4. 创建样式(static/styles.css)

  1. body {
  2. font-family: Arial, sans-serif;
  3. margin: 0;
  4. padding: 20px;
  5. background: #f4f4f9;
  6. }
  7. .container {
  8. max-width: 800px;
  9. margin: auto;
  10. background: white;
  11. padding: 20px;
  12. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  13. }
  14. h1, h2 {
  15. color: #333;
  16. }
  17. form {
  18. margin-bottom: 20px;
  19. }
  20. input, button {
  21. padding: 10px;
  22. margin: 5px 0;
  23. }

启动应用

使用命令行进入项目所在目录,运行以下命令启动Flask应用:

  1. flask run

应用将默认在http://127.0.0.1:5000/运行。打开浏览器访问该地址,即可看到记账应用。

后续步骤

  1. 数据持久化:可以选择将数据存储在SQLite数据库中。
  2. 改进UI:使用更高级的前端框架如Bootstrap。
  3. 支持删除:添加删除账目的功能。
  4. 用户身份验证:为应用增添用户登录功能以支持多用户使用。

通过这个简单的项目,你会更好地理解Flask应用的基本构建流程,以及如何使用Jinja2模板渲染动态网页。

    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
小魔女 关注 已关注

最近一次登录:2024-11-20 16:12:13   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图