Python打造个性化在线食谱收藏夹

在这个快节奏的时代,美食成为了人们生活中不可或缺的一部分。然而,面对琳琅满目的食谱,如何高效地管理和收藏心仪的菜肴却成了一大难题。今天,我们将用Python语言,打造一个个性化的在线食谱收藏夹,让你轻松管理美食秘籍。

Python打造个性化在线食谱收藏夹

项目背景

你是否曾在网上浏览到一道令人垂涎的菜肴,却因为无法及时保存而遗憾错过?或者收藏了无数食谱,却在需要时难以找到?这些问题,我们的在线食谱收藏夹都能一一解决。

技术选型

为了实现这一功能,我们将使用以下技术栈:

  • Flask:轻量级的Web框架,用于搭建后端服务。
  • SQLite:轻便的数据库,用于存储食谱数据。
  • HTML/CSS/JavaScript:前端技术,用于构建用户界面。

功能设计

1. 食谱添加

用户可以通过表单提交新的食谱,包括菜名、食材、做法等信息。

2. 食谱展示

首页展示所有收藏的食谱,用户可以浏览和搜索。

3. 食谱编辑

用户可以修改已收藏的食谱信息。

4. 食谱删除

用户可以删除不再需要的食谱。

实现步骤

Step 1: 环境搭建

首先,确保你已经安装了Python和pip。然后,创建一个新的项目目录,并安装Flask和SQLite:

pip install Flask

Step 2: 后端开发

在项目目录下创建app.py,编写后端代码:

from flask import Flask, request, render_template, redirect, url_for
import sqlite3

app = Flask(__name__)

# 数据库初始化
def init_db():
    conn = sqlite3.connect('recipes.db')
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS recipes
                 (id INTEGER PRIMARY KEY, name TEXT, ingredients TEXT, method TEXT)''')
    conn.commit()
    conn.close()

@app.route('/')
def index():
    conn = sqlite3.connect('recipes.db')
    c = conn.cursor()
    c.execute('SELECT * FROM recipes')
    recipes = c.fetchall()
    conn.close()
    return render_template('index.html', recipes=recipes)

@app.route('/add', methods=['GET', 'POST'])
def add():
    if request.method == 'POST':
        name = request.form['name']
        ingredients = request.form['ingredients']
        method = request.form['method']
        conn = sqlite3.connect('recipes.db')
        c = conn.cursor()
        c.execute('INSERT INTO recipes (name, ingredients, method) VALUES (?, ?, ?)', (name, ingredients, method))
        conn.commit()
        conn.close()
        return redirect(url_for('index'))
    return render_template('add.html')

@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit(id):
    conn = sqlite3.connect('recipes.db')
    c = conn.cursor()
    if request.method == 'POST':
        name = request.form['name']
        ingredients = request.form['ingredients']
        method = request.form['method']
        c.execute('UPDATE recipes SET name=?, ingredients=?, method=? WHERE id=?', (name, ingredients, method, id))
        conn.commit()
        return redirect(url_for('index'))
    c.execute('SELECT * FROM recipes WHERE id=?', (id,))
    recipe = c.fetchone()
    conn.close()
    return render_template('edit.html', recipe=recipe)

@app.route('/delete/<int:id>')
def delete(id):
    conn = sqlite3.connect('recipes.db')
    c = conn.cursor()
    c.execute('DELETE FROM recipes WHERE id=?', (id,))
    conn.commit()
    conn.close()
    return redirect(url_for('index'))

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

Step 3: 前端开发

在项目目录下创建templates文件夹,并添加index.html、add.html和edit.html等模板文件。

index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>食谱收藏夹</title>
</head>
<body>
    <h1>我的食谱收藏</h1>
    <a href="/add">添加新食谱</a>
    <ul>
        {% for recipe in recipes %}
        <li>
            <h2>{{ recipe[1] }}</h2>
            <p>食材:{{ recipe[2] }}</p>
            <p>做法:{{ recipe[3] }}</p>
            <a href="/edit/{{ recipe[0] }}">编辑</a>
            <a href="/delete/{{ recipe[0] }}">删除</a>
        </li>

你可能想看:
« 上一篇
下一篇 »

相关推荐

python爬虫教程PDF,Python爬虫书PDF

2026年09月02日

35阅读

外国教程python,python编程教学视频

2026年09月02日

29阅读

python简明教程沈洁元,python详细基础教程

2026年09月02日

24阅读

python库包教程,python 包

2026年09月02日

30阅读

python教程大全集,python教程600集

2026年09月02日

29阅读

python入门教程完整版,python零基础入门全套教程视频

2026年09月02日

26阅读