app.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from flask import Flask, render_template, request, redirect, url_for
  2. import json
  3. import os
  4. app = Flask(__name__)
  5. # File to store checklist items
  6. CHECKLIST_FILE = 'checklist.json'
  7. # Load checklist items
  8. def load_checklist():
  9. if os.path.exists(CHECKLIST_FILE):
  10. with open(CHECKLIST_FILE, 'r') as file:
  11. return json.load(file)
  12. return []
  13. # Save checklist items
  14. def save_checklist(items):
  15. with open(CHECKLIST_FILE, 'w') as file:
  16. json.dump(items, file)
  17. # List of checklist items
  18. checklist_items = load_checklist()
  19. @app.route('/', methods=['GET', 'POST'])
  20. def index():
  21. if request.method == 'POST':
  22. item = request.form.get('item')
  23. if item:
  24. checklist_items.append({'name': item, 'checked': False})
  25. save_checklist(checklist_items) # Save to file after adding
  26. return redirect(url_for('index'))
  27. return render_template('index.html', items=checklist_items)
  28. @app.route('/check/<int:item_id>')
  29. def check(item_id):
  30. if 0 <= item_id < len(checklist_items):
  31. checklist_items[item_id]['checked'] = not checklist_items[item_id]['checked']
  32. save_checklist(checklist_items) # Save to file after checking
  33. return redirect(url_for('index'))
  34. @app.route('/clear')
  35. def clear():
  36. global checklist_items
  37. checklist_items = [] # Clear the list
  38. save_checklist(checklist_items) # Save the empty list
  39. return redirect(url_for('index'))
  40. @app.route('/list')
  41. def list():
  42. return render_template('list.html', items=checklist_items)
  43. if __name__ == '__main__':
  44. app.run(debug=True)