Python助力:在线食谱制作指南

在这个数字化时代,Python不仅是一门强大的编程语言,更是我们生活中的得力助手。今天,我们将用Python编写一个在线食谱制作指南,让美食制作变得更加简单、有趣。

Python助力:在线食谱制作指南

项目背景

你是否曾在厨房里手忙脚乱,试图回忆某个食谱的步骤?是否曾因为配料比例不当而毁了美味佳肴?别担心,我们的在线食谱制作指南将为你解决这些问题。

所需工具

  1. Python环境:确保你的电脑已安装Python。
  2. Flask框架:用于构建Web应用。
  3. SQLite数据库:存储食谱数据。

步骤一:环境搭建

首先,我们需要安装Flask和SQLite。打开终端或命令提示符,输入以下命令:

pip install Flask

步骤二:创建项目结构

在项目文件夹中,创建以下文件和目录:

/recipe_guide
    /templates
        index.html
        recipe.html
    app.py
    schema.sql

步骤三:设计数据库

在schema.sql中,定义食谱数据库的结构:

CREATE TABLE recipes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    ingredients TEXT NOT NULL,
    steps TEXT NOT NULL
);

步骤四:编写后端代码

在app.py中,编写Flask应用的核心代码:

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

app = Flask(__name__)

def get_db_connection():
    conn = sqlite3.connect('database.db')
    conn.row_factory = sqlite3.Row
    return conn

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

@app.route('/recipe/<int:recipe_id>')
def recipe(recipe_id):
    conn = get_db_connection()
    recipe = conn.execute('SELECT * FROM recipes WHERE id = ?', (recipe_id,)).fetchone()
    conn.close()
    return render_template('recipe.html', recipe=recipe)

@app.route('/create', methods=('GET', 'POST'))
def create():
    if request.method == 'POST':
        title = request.form['title']
        ingredients = request.form['ingredients']
        steps = request.form['steps']
        conn = get_db_connection()
        conn.execute('INSERT INTO recipes (title, ingredients, steps) VALUES (?, ?, ?)',
                     (title, ingredients, steps))
        conn.commit()
        conn.close()
        return redirect(url_for('index'))
    return render_template('create.html')

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

步骤五:编写前端代码

在templates/index.html中,展示食谱列表:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>食谱指南</title>
</head>
<body>
    <h1>食谱列表</h1>
    <a href="{{ url_for('create') }}">添加新食谱</a>
    <ul>
        {% for recipe in recipes %}
            <li><a href="{{ url_for('recipe', recipe_id=recipe.id) }}">{{ recipe.title }}</a></li>
        {% endfor %}
    </ul>
</body>
</html>

在templates/recipe.html中,展示单个食谱的详细信息:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ recipe.title }}</title>
</head>
<body>
    <h1>{{ recipe.title }}</h1>
    <h2>配料:</h2>
    <p>{{ recipe.ingredients }}</p>
    <h2>步骤:</h2>
    <p>{{ recipe.steps }}</p>
    <a href="{{ url_for('index') }}">返回列表</a>
</body>
</html>

步骤六:运行应用

在终端中运行以下命令启动应用:

python app.py

打开浏览器,访问http://127.0.0.1:5000/,你将看到一个简洁的在线食谱制作指南。

通过Python和Flask,我们轻松搭建了一个在线食谱制作指南。这不仅是一个实用的工具,更是编程与生活结合的完美示例。希望这个项目能激发你更多的创意,让编程成为你生活中的得力助手。快去尝试制作你的第一个食谱吧!

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

相关推荐

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

2026年09月02日

31阅读

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

2026年09月02日

25阅读

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

2026年09月02日

21阅读

python库包教程,python 包

2026年09月02日

26阅读

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

2026年09月02日

26阅读

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

2026年09月02日

22阅读