Python
48 ~40 dk

Flask ile Web Uygulaması

Flask ile Web

C:\> pip install flask
Successfully installed flask-3.0.2
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

ogrenciler = [
    {"ad": "Ali", "not": 85},
    {"ad": "Veli", "not": 92},
]

@app.route("/")
def anasayfa():
    return \"\"\"
    <h1>Öğrenci Listesi</h1>
    <ul>
    \"\"\" + "".join(f"<li>{o['ad']}: {o['not']}</li>" for o in ogrenciler) + \"\"\"
    </ul>
    <a href="/api/ogrenciler">API</a>
    \"\"\"

@app.route("/api/ogrenciler")
def api_ogrenciler():
    return jsonify(ogrenciler)

@app.route("/api/ogrenci", methods=["POST"])
def api_ekle():
    veri = request.json
    ogrenciler.append(veri)
    return jsonify({"mesaj": "Eklendi!", "toplam": len(ogrenciler)})

if __name__ == "__main__":
    app.run(debug=True)
C:\> python app.py
* Running on http://127.0.0.1:5000
* Debug mode: on

🎯 Alıştırmalar

  1. Flask ile kişisel blog sitesi
  2. REST API ile todo uygulaması

Yorumlar 0

Giriş yapın — Yorumlarınız hemen yayınlansın
Henüz yorum yapılmamış. İlk yorumu siz yapın!