Flaskとは
Flask(フラスコ/フラスク)とは、PythonのWebアプリケーションフレームワークで、小規模向けの簡単なWebアプリケーションを作るのに適しています。
Webフレームワークとは、ウェブサイトやウェブアプリケーションを作るための機能を提供し、
ウェブフレームワークを使わない時よりもより容易にWebアプリケーションを作ることができるものです。
引用元
Flaskの特徴
Flask(フラスク)は、Pythonで使える軽量なWebフレームワークです。とてもシンプルで、必要な機能だけを自由に追加できるのが特徴です。
他のフレームワークに比べて、コード量が少なくて済むため、学びながらすぐに動かして試すことができます。
また、Flaskはコミュニティが活発で、ドキュメントも豊富なので、つまずいても情報を見つけやすいのも魅力です。
Pythonの標準ライブラリや他の外部パッケージとの相性も良いため、カスタマイズ性が高く、自由に設計できる点も大きなメリットです。
引用元
ToDoアプリ(Flaskコード)
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
todos = []
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
task = request.form.get("task")
if task:
todos.append({"task": task, "done": False})
return redirect(url_for("index"))
return render_template("index.html", todos=todos)
@app.route("/toggle/<int:index>")
def toggle(index):
todos[index]["done"] = not todos[index]["done"]
return redirect(url_for("index"))
@app.route("/delete/<int:index>")
def delete(index):
todos.pop(index)
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=True)
ToDoアプリ(HTMLテンプレート)
<form method="POST">
<input type="text" name="task" placeholder="やることを入力">
<button type="submit">追加</button>
</form>
<ul>
{% for todo in todos %}
<li>
<input type="checkbox"
onclick="location.href='/toggle/{{ loop.index0 }}'"
{% if todo.done %}checked{% endif %}>
<span class="{% if todo.done %}done{% endif %}">
{{ todo.task }}
</span>
<a href="/delete/{{ loop.index0 }}">削除</a>
</li>
{% endfor %}
</ul>