(代码开源)基于 Flask 的笔记应用,并且生成一个.exe运行工具,用户可以创建、编辑、删除笔记

目录

1. 安装 Flask

2. 创建 Flask 应用并实现基本功能

3. 创建 HTML 页面

4. 运行 Flask 应用

5. 生成可执行文件


1. 安装 Flask

可以使用 pip 命令安装 Flask:



pip install Flask

2. 创建 Flask 应用并实现基本功能

可以创建一个 Python 文件,例如 app.py,来实现基于 Flask 的笔记应用:



import os

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



app = Flask(__name__)



# 设定笔记保存的文件名

NOTES_FILE = 'notes.txt'



# 如果没有笔记文件,创建一个空的

if not os.path.exists(NOTES_FILE):

    with open(NOTES_FILE, 'w') as f:

        pass



# 加载笔记数据

def load_notes():

    notes = []

    with open(NOTES_FILE, 'r') as f:

        for line in f:

            title, content = line.strip().split('\t')

            notes.append({'title': title, 'content': content})

    return notes



# 保存笔记数据

def save_notes(notes):

    with open(NOTES_FILE, 'w') as f:

        for note in notes:

            f.write('{}\t{}\n'.format(note['title'], note['content']))



# 首页:列出所有笔记

@app.route('/')

def index():

    notes = load_notes()

    return render_template('index.html', notes=notes)



# 笔记详情页:查看、编辑和删除笔记

@app.route('/note/detail/<title>')

def note_detail(title):

    notes = load_notes()

    for note in notes:

        if note['title'] == title:

            return render_template('note_detail.html', note=note)

    else:

        return render_template('error.html', message='未找到该笔记。')



# 新建笔记页:创建新笔记

@app.route('/note/create', methods=['GET', 'POST'])

def note_create():

    if request.method == 'POST':

        title = request.form['title']

        content = request.form['content']

        notes = load_notes()

        notes.append({'title': title, 'content': content})

        save_notes(notes)

        return redirect(url_for('index'))

    else:

        return render_template('note_create.html')



# 编辑笔记页:编辑已有笔记

@app.route('/note/edit/<title>', methods=['GET', 'POST'])

def note_edit(title):

    notes = load_notes()

    for i, note in enumerate(notes):

        if note['title'] == title:

            if request.method == 'POST':

                content = request.form['content']

                notes[i]['content'] = content

                save_notes(notes)

                return redirect(url_for('note_detail', title=title))

            else:

                return render_template('note_edit.html', note=note)

    else:

        return render_template('error.html', message='未找到该笔记。')



# 删除笔记页:删除已有笔记

@app.route('/note/delete/<title>')

def note_delete(title):

    notes = load_notes()

    for i, note in enumerate(notes):

        if note['title'] == title:

            del notes[i]

            save_notes(notes)

            return redirect(url_for('index'))

    else:

        return render_template('error.html', message='未找到该笔记。')



# 搜索笔记页:根据关键字搜索笔记

@app.route('/note/search', methods=['GET', 'POST'])

def note_search():

    if request.method == 'POST':

        keyword = request.form['keyword']

        notes = load_notes()

        results = []

        for note in notes:

            if keyword in note['title'] or keyword in note['content']:

                results.append(note)

        return render_template('note_search.html', keyword=keyword, notes=results)

    else:

        return render_template('note_search.html')



if __name__ == '__main__':

    app.run()

上述代码实现了基本的笔记应用功能,通过 Flask 框架提供的路由功能,实现了首页、笔记详情页、新建笔记页、编辑笔记页、删除笔记页和搜索笔记页等页面。

3. 创建 HTML 页面

在项目根目录下创建 templates 目录,并在该目录下创建以下 HTML 模板:

index.html

```html

<!DOCTYPE html>
<html>
<head>
    <title>笔记应用 - 首页</title>
</head>
<body>
    <h1>笔记应用</h1>
    <hr>
    <h2>笔记列表</h2>
    <ul>
        {% for note in notes %}
        <li><a href="{{ url_for('note_detail', title=note['title']) }}">{{ note['title'] }}</a></li>
        {% endfor %}
    </ul>
    <hr>
    <a href="{{ url_for('note_create') }}">新建笔记</a>
    <a href="{{ url_for('note_search') }}">搜索笔记</a>
</body>
</html>

```

note_detail.html

```html

<!DOCTYPE html>

<html>

<head>

    <title>笔记应用 - {{ note['title'] }}</title>

</head>

<body>

    <h1>{{ note['title'] }}</h1>

    <hr>

    <p>{{ note['content'] }}</p>

    <hr>

    <a href="{{ url_for('note_edit', title=note['title']) }}">编辑笔记</a>

    <a href="{{ url_for('note_delete', title=note['title']) }}">删除笔记</a>

    <a href="{{ url_for('index') }}">返回首页</a>

</body>

</html>

```

note_create.html

```html

<!DOCTYPE html>

<html>

<head>

    <title>笔记应用 - 新建笔记</title>

</head>

<body>

    <h1>新建笔记</h1>

    <hr>

    <form method="post">

        <div>

            <label for="title">标题:</label>

            <input type="text" id="title" name="title" required>

        </div>

        <br>

        <div>

            <label for="content">内容:</label>

            <textarea id="content" name="content" required></textarea>

        </div>

        <br>

        <div>

            <input type="submit" value="创建笔记">

        </div>

    </form>

    <hr>

    <a href="{{ url_for('index') }}">返回首页</a>

</body>

</html>

```

note_edit.html

```html

<!DOCTYPE html>

<html>

<head>

    <title>笔记应用 - 编辑 {{ note['title'] }}</title>

</head>

<body>

    <h1>编辑 {{ note['title'] }}</h1>

    <hr>

    <form method="post">

        <div>

            <label for="content">内容:</label>

            <textarea id="content" name="content" required>{{ note['content'] }}</textarea>

        </div>

        <br>

        <div>

            <input type="submit" value="保存更改">

        </div>

    </form>

    <hr>

    <a href="{{ url_for('index') }}">返回首页</a>

</body>

</html>

```

error.html

```html

<!DOCTYPE html>

<html>

<head>

    <title>笔记应用 - 错误</title>

</head>

<body>

    <h1>错误</h1>

    <hr>

    <p>{{ message }}</p>

    <hr>

    <a href="{{ url_for('index') }}">返回首页</a>

</body>

</html>

```

note_search.html

```html

<!DOCTYPE html>

<html>

<head>

    <title>笔记应用 - 搜索:{{ keyword }}</title>

</head>

<body>

    <h1>搜索:{{ keyword }}</h1>

    <hr>

    {% if notes %}

        <ul>

            {% for note in notes %}

            <li><a href="{{ url_for('note_detail', title=note['title']) }}">{{ note['title'] }}</a></li>

            {% endfor %}

        </ul>

    {% else %}

        <p>没有找到匹配的笔记。</p>

    {% endif %}

    <hr>

    <a href="{{ url_for('index') }}">返回首页</a>

</body>

</html>

```

4. 运行 Flask 应用

在控制台运行以下命令启动 Flask 应用:

```

python app.py

```

Flask 应用将在本地启动,并监听默认端口 5000。在本地浏览器中打开 `http://localhost:5000` 即可访问笔记应用。

5. 生成可执行文件

使用 PyInstaller 工具可以将 Python 应用程序打包成可执行文件。可以使用以下命令安装 PyInstaller:

```

pip install pyinstaller

```

然后,在命令行中进入项目目录,使用以下命令将应用程序打包成单个可执行文件:

```

pyinstaller --onefile app.py

```

PyInstaller 将在 dist 目录下生成一个可执行文件。这个文件可以直接运行,无需安装 Python 环境。

大家觉得有帮助的话还请大家给个收藏关注鼓励一下

有什么问题评论区留言,看到会恢复哒~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理Flask简单整理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值