-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (51 loc) · 1.92 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# To-Do-List-Python-Flask-and-MySQL
import os
from flask import Flask, request, render_template
from datetime import date
# Defining Flask App
app = Flask(__name__)
# Saving Date today in 2 different formats
datetoday = date.today().strftime("%m_%d_%y")
datetoday2 = date.today().strftime("%d-%B-%Y")
# If this file doesn't exist, create it
if 'tasks.txt' not in os.listdir('.'):
with open('tasks.txt', 'w') as f:
f.write('')
def gettasklist():
with open('tasks.txt', 'r') as f:
tasklist = f.readlines()
return tasklist
def createnewtasklist():
os.remove('tasks.txt')
with open('tasks.txt', 'w') as f:
f.write('')
def updatetasklist(tasklist):
os.remove('tasks.txt')
with open('tasks.txt', 'w') as f:
f.writelines(tasklist)
# ROUTING FUNCTIONS
@app.route('/')
def home():
return render_template('home.html', datetoday2=datetoday2, tasklist=gettasklist(), l=len(gettasklist()))
@app.route('/clear')
def clear_list():
createnewtasklist()
return render_template('home.html', datetoday2=datetoday2, tasklist=gettasklist(), l=len(gettasklist()))
@app.route('/addtask', methods=['POST'])
def add_task():
task = request.form.get('newtask')
with open('tasks.txt', 'a') as f:
f.writelines(task + '\n')
return render_template('home.html', datetoday2=datetoday2, tasklist=gettasklist(), l=len(gettasklist()))
@app.route('/deltask', methods=['GET'])
def remove_task():
task_index = int(request.args.get('deltaskid'))
tasklist = gettasklist()
if task_index < 0 or task_index >= len(tasklist):
return render_template('home.html', datetoday2=datetoday2, tasklist=tasklist, l=len(tasklist), mess='Invalid Index...')
else:
removed_task = tasklist.pop(task_index)
updatetasklist(tasklist)
return render_template('home.html', datetoday2=datetoday2, tasklist=tasklist, l=len(tasklist))
if __name__ == '__main__':
app.run(debug=True)