12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- from flask import Flask, render_template, Blueprint, request, redirect, url_for
- from datetime import timezone
- import os
- clbp = Blueprint('checklist', __name__)
- def save_checklist(list_items): # Save checklist items
- new_list = json.dumps(list_items, indent=4)
- list_file = os.path.join(current_app.instance_path, 'list.py')
- try:
- with open(list_file, 'w') as f:
- f.write(f'LIST = [{new_list}]')
- f.close
- current_app.config.from_pyfile('list.py', silent=True) # Reread the list into the app
- except Exception as sclerror:
- current_app.logger.error(f'Couldn\'t save list.py: {sclerror.args[0]}')
- @clbp.route('/', methods=['GET', 'POST'])
- def index():
- checklist_items = current_app.list['LIST']
- 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)
- @clbp.route('/check/<int:item_id>')
- def check(item_id):
- checklist_items = current_app.list['LIST']
- 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'))
- @clbp.route('/clear')
- def clear():
- checklist_items = current_app.list['LIST']
- checklist_items = [] # Clear the list
- save_checklist(checklist_items) # Save the empty list
- return redirect(url_for('index'))
- @clbp.route('/list')
- def list():
- checklist_items = current_app.list['LIST']
- return render_template('list.html', items=checklist_items)
|