Skip to content

Instantly share code, notes, and snippets.

@ndamulelonemakh
Created May 11, 2024 16:35
Show Gist options
  • Save ndamulelonemakh/fbf10dbbf22731c7375fb7ddad0aa6cb to your computer and use it in GitHub Desktop.
Save ndamulelonemakh/fbf10dbbf22731c7375fb7ddad0aa6cb to your computer and use it in GitHub Desktop.
Flask server html
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
todos = []
@app.route('/')
def index():
return render_template('index.html', todos=todos)
@app.route('/add', methods=['POST'])
def add():
todo = request.form['todo']
todos.append(todo)
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)
<!-- save in ./templates/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1 class="text-center mt-3">To-Do List</h1>
<ul class="list-group">
{% for todo in todos %}
<li class="list-group-item">{{ todo }}</li>
{% endfor %}
</ul>
<form method="post" action="/add" class="form-inline mt-3">
<div class="form-group mx-sm-3">
<input type="text" name="todo" class="form-control" placeholder="Add a new to-do item">
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment