Last active
April 16, 2021 19:06
-
-
Save cbscribe/89226d94be90d600ecb8b62b61976e9e to your computer and use it in GitHub Desktop.
Flask Todo list app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layout.html" %} | |
{% block body %} | |
<form action="/add" method="post"> | |
<input type="text" name="task"> | |
<input type="submit"> | |
</form> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, redirect, session | |
app = Flask(__name__) | |
app.secret_key = "abcd" | |
username = "bradfield" | |
password = "1234" | |
def save_tasks(): | |
with open("todos.save", "w") as f: | |
for task in todos: | |
f.write(f"{task}\n") | |
def load_tasks(): | |
with open("todos.save", "r") as f: | |
for line in f: | |
todos.append(line) | |
todos = [] | |
load_tasks() | |
@app.route("/") | |
def index(): | |
if not logged_in(): | |
return redirect("/login") | |
return render_template("tasks.html", title="Tasks", todos=todos) | |
@app.route("/add", methods=["GET", "POST"]) | |
def add(): | |
if not logged_in(): | |
return redirect("/login") | |
if request.method == "GET": | |
return render_template("add.html", title="Add a task") | |
else: | |
task = request.form.get("task") | |
if task != "": | |
todos.append(task) | |
save_tasks() | |
return redirect("/") | |
@app.route("/del") | |
def delete(): | |
if not logged_in(): | |
return redirect("/login") | |
n = int(request.args.get("id")) | |
del todos[n] | |
save_tasks() | |
return redirect("/") | |
@app.route("/login", methods=["GET", "POST"]) | |
def login(): | |
if request.method == "POST": | |
user = request.form.get("username") | |
passwd = request.form.get("password") | |
if user == username and passwd == password: | |
session["username"] = username | |
return redirect("/") | |
else: | |
return redirect("/login") | |
return render_template("login.html", title="Login") | |
def logged_in(): | |
return "username" in session | |
@app.route("/logout") | |
def logout(): | |
session.pop("username") | |
return redirect("/login") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>{{ title }}</title> | |
<link rel="stylesheet" href="/static/style.css"> | |
</head> | |
<body> | |
{% block body %} | |
{% endblock %} | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layout.html" %} | |
{% block body %} | |
<h1>Login</h1> | |
<form action="/login" method="post"> | |
<p>Login: <input type="text" name="username"></p> | |
<p>Password <input type="password" name="password"></p> | |
<input type="submit"> | |
</form> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layout.html" %} | |
{% block body %} | |
<h1>Tasks</h1> | |
<ul> | |
{% for task in todos: %} | |
<li>{{ task }} <a href="/del?id={{loop.index0}}">x</a></li> | |
{% endfor %} | |
</ul> | |
<a href="/add">Add a task</a> | |
<a href="/logout">Logout</a> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment