我的项目 2:使用 Python + Streamlit 构建简易备忘录应用

发布: (2025年11月30日 GMT+8 05:57)
3 min read
原文: Dev.to

Source: Dev.to

项目概述

备忘录应用可以让您:

  • 添加新备忘录
  • 将其存储在 memos.json 文件中
  • 查看所有已保存的备忘录

它可以通过控制台或 Streamlit Web UI 使用,演示文件处理、JSON 存储和 UI 转换。

项目结构

memo_app/

├── memo_app.py               # Console version
├── memo_app_streamlit.py     # Streamlit version
└── memos.json                # Memo storage file

控制台版本 (memo_app.py)

代码

import json
import os
from datetime import datetime

now = datetime.now().strftime("%Y-%m-%d")
FILE_NAME = "memos.json"

# Ensure the storage file exists
if not os.path.exists(FILE_NAME):
    with open(FILE_NAME, "w") as f:
        json.dump([], f)

def load_memos():
    with open(FILE_NAME, "r") as f:
        return json.load(f)

def save_memos(memos):
    with open(FILE_NAME, "w") as f:
        json.dump(memos, f, indent=2)

print("=== Sabin's Memo App ===")
print("1) Add Memo")
print("2) Show Memos")

choice = input("Select option: ")

if choice == "1":
    memo = input("Write memo: ")
    memos = load_memos()
    memos.append({"text": memo, "date": now})
    save_memos(memos)
    print("Memo saved!")

elif choice == "2":
    memos = load_memos()
    print("=== Memo List ===")
    for i, m in enumerate(memos, start=1):
        print(f"{i}. {m['text']}  ({m['date']})")

else:
    print("Invalid option.")

Streamlit 版本 (memo_app_streamlit.py)

代码

import streamlit as st
import json
import os
from datetime import datetime

FILE_NAME = "memos.json"

# Ensure the storage file exists
if not os.path.exists(FILE_NAME):
    with open(FILE_NAME, "w") as f:
        json.dump([], f)

def load_memos():
    with open(FILE_NAME, "r") as f:
        return json.load(f)

def save_memos(memos):
    with open(FILE_NAME, "w") as f:
        json.dump(memos, f, indent=2)

st.title("📝 Sabin's Memo App (Streamlit Version)")

menu = st.radio("Select an option:", ["Add Memo", "Show Memos"])

if menu == "Add Memo":
    text = st.text_input("Write your memo:")

    if st.button("Save Memo"):
        if text.strip() == "":
            st.warning("Memo cannot be empty.")
        else:
            now = datetime.now().strftime("%Y-%m-%d")
            memos = load_memos()
            memos.append({"text": text, "date": now})
            save_memos(memos)
            st.success("Memo saved!")

elif menu == "Show Memos":
    memos = load_memos()
    if not memos:
        st.info("No memos yet.")
    else:
        st.subheader("📄 Memo List")
        for i, memo in enumerate(memos, start=1):
            st.write(f"**{i}. {memo['text']}**  ({memo['date']})")

如何运行

控制台版本

python3 memo_app.py

Streamlit 版本

pip install streamlit
streamlit run memo_app_streamlit.py

Streamlit 命令将在默认浏览器中启动 Web UI。

我的收获

  • 使用 JSON 文件存储数据
  • 本地存储的安全读写
  • 使用 Streamlit 添加 UI 层
  • 将 CLI 工具转换为 Web 应用
  • 小而专注的项目对构建坚实基础的价值

未来改进

  • 添加备忘录删除功能
  • 添加备忘录编辑功能
  • 将存储切换为 SQLite
  • 实现搜索/过滤功能
  • 在 Streamlit 中提供暗/亮模式切换
Back to Blog

相关文章

阅读更多 »

如何在 Python 中生成 QR 码

面向初学者的教程,探索 Python 的 “qrcode” 包 文章《How to Generate QR Codes in Python》首次发表于 Towards Data Science....