Adventure aus DB
This commit is contained in:
parent
431613f9b3
commit
7c3fdb8321
1 changed files with 22 additions and 24 deletions
46
app/app.py
46
app/app.py
|
|
@ -1,34 +1,32 @@
|
|||
from flask import Flask, request, redirect
|
||||
from flask import Flask
|
||||
import sqlite3
|
||||
|
||||
app = Flask(__name__)
|
||||
eintraege = [] # speichert die Nachrichten (im Arbeitsspeicher)
|
||||
|
||||
@app.route("/pinnwand", methods=["GET", "POST"])
|
||||
def pinnwand():
|
||||
if request.method == "POST":
|
||||
eintraege.append(request.form["nachricht"])
|
||||
return redirect("/app/pinnwand")
|
||||
liste = ""
|
||||
for e in eintraege:
|
||||
liste = liste + "<li>" + e + "</li>"
|
||||
return '<h1>Pinnwand</h1>' \
|
||||
'<form method="post"><input name="nachricht">' \
|
||||
'<button>Senden</button></form><ul>' + liste + '</ul>'
|
||||
|
||||
def get_db():
|
||||
db = sqlite3.connect("adventure.db")
|
||||
db.row_factory = sqlite3.Row # Spalten per Name ansprechen
|
||||
return db
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return "Hallo aus meiner Flask-App!"
|
||||
def start():
|
||||
return '<h1>Verlies</h1><a href="/app/raum/1">Abenteuer starten</a>'
|
||||
|
||||
@app.route("/frage", methods=["GET", "POST"])
|
||||
def frage():
|
||||
if request.method == "POST":
|
||||
if request.form["antwort"] == "8":
|
||||
return "Richtig!"
|
||||
return "Leider falsch."
|
||||
return '<form method="post">Was ist 3 + 5? ' \
|
||||
'<input name="antwort"><button>OK</button></form>'
|
||||
@app.route("/raum/<int:id>")
|
||||
def raum(id):
|
||||
db = get_db()
|
||||
r = db.execute("SELECT name, beschreibung FROM raeume WHERE id=?", (id,)).fetchone()
|
||||
ausgaenge = db.execute(
|
||||
"SELECT richtung, nach_raum FROM ausgaenge WHERE von_raum=?", (id,)
|
||||
).fetchall()
|
||||
db.close()
|
||||
|
||||
html = "<h1>" + r["name"] + "</h1>"
|
||||
html = html + "<p>" + r["beschreibung"] + "</p>"
|
||||
for a in ausgaenge:
|
||||
html = html + '<a href="/app/raum/' + str(a["nach_raum"]) + '">' \
|
||||
+ a["richtung"] + '</a> '
|
||||
return html
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=9008)
|
||||
Loading…
Reference in a new issue