1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- from flask import Flask, render_template, request, redirect, url_for
- import json
- import os
- app = Flask(__name__)
- # File to store checklist items
- CHECKLIST_FILE = 'checklist.json'
- # Load checklist items
- def load_checklist():
- if os.path.exists(CHECKLIST_FILE):
- with open(CHECKLIST_FILE, 'r') as file:
- return json.load(file)
- return []
- # Save checklist items
- def save_checklist(items):
- with open(CHECKLIST_FILE, 'w') as file:
- json.dump(items, file)
- # List of checklist items
- checklist_items = load_checklist()
- @app.route('/', methods=['GET', 'POST'])
- def index():
- if request.method == 'POST':
- item = request.form.get('item')
- if item:
- checklist_items.append({'name': item, 'checked': False})
- save_checklist(checklist_items) # Save to file after adding
- return redirect(url_for('index'))
- return render_template('index.html', items=checklist_items)
- @app.route('/check/<int:item_id>')
- def check(item_id):
- if 0 <= item_id < len(checklist_items):
- checklist_items[item_id]['checked'] = not checklist_items[item_id]['checked']
- save_checklist(checklist_items) # Save to file after checking
- return redirect(url_for('index'))
- @app.route('/clear')
- def clear():
- global checklist_items
- checklist_items = [] # Clear the list
- save_checklist(checklist_items) # Save the empty list
- return redirect(url_for('index'))
- @app.route('/list')
- def list():
- return render_template('list.html', items=checklist_items)
- if __name__ == '__main__':
- app.run(debug=True)
|