fix: Behandle JSON-Lade-Fehler in app.py und füge Fallback für leere Liste hinzu
Co-authored-by: aider (ollama_chat/qwen3-coder:30b) <aider@aider.chat>
This commit is contained in:
parent
70e3155636
commit
6994b462c4
1 changed files with 16 additions and 11 deletions
27
app.py
27
app.py
|
|
@ -8,13 +8,15 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///typewriter.db'
|
|||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db.init_app(app)
|
||||
|
||||
# Laden der Lektionen aus der JSON-Datei mit Escape-Zeichen-Handling
|
||||
# Laden der Lektionen aus der JSON-Datei
|
||||
DATA_PATH = os.path.join(app.root_path, "data", "lessons.json")
|
||||
with open(DATA_PATH, "r", encoding="utf-8") as f:
|
||||
data = f.read()
|
||||
# Alle einfachen Backslashes verdoppeln
|
||||
data = data.replace("\\", "\\\\")
|
||||
lessons = json.loads(data)
|
||||
try:
|
||||
with open(DATA_PATH, "r", encoding="utf-8") as f:
|
||||
lessons = json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Fehler beim Laden der JSON-Datei: {e}")
|
||||
# Fallback: leere Liste, falls die Datei beschädigt ist
|
||||
lessons = []
|
||||
|
||||
@app.before_request
|
||||
def create_tables():
|
||||
|
|
@ -27,12 +29,15 @@ def index():
|
|||
|
||||
if not progress:
|
||||
# Falls keine Daten vorhanden sind, initialisiere mit ersten Text
|
||||
progress = Progress(current_text_index=0, current_position=0, last_text=lessons[0]['text'])
|
||||
if lessons:
|
||||
progress = Progress(current_text_index=0, current_position=0, last_text=lessons[0]['text'])
|
||||
else:
|
||||
progress = Progress(current_text_index=0, current_position=0, last_text="")
|
||||
db.session.add(progress)
|
||||
db.session.commit()
|
||||
|
||||
# Hole den aktuellen Text
|
||||
current_text = lessons[progress.current_text_index]['text']
|
||||
current_text = lessons[progress.current_text_index]['text'] if lessons else ""
|
||||
|
||||
return render_template('index.html',
|
||||
current_text=current_text,
|
||||
|
|
@ -125,19 +130,19 @@ def next_text():
|
|||
progress = Progress.query.first()
|
||||
|
||||
# Wechsel zum nächsten Text
|
||||
if progress.current_text_index < len(lessons) - 1:
|
||||
if lessons and progress.current_text_index < len(lessons) - 1:
|
||||
progress.current_text_index += 1
|
||||
else:
|
||||
# Wenn wir am Ende sind, starten wir von vorne
|
||||
progress.current_text_index = 0
|
||||
|
||||
progress.current_position = 0
|
||||
progress.last_text = lessons[progress.current_text_index]['text']
|
||||
progress.last_text = lessons[progress.current_text_index]['text'] if lessons else ""
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({
|
||||
'text': lessons[progress.current_text_index]['text']
|
||||
'text': lessons[progress.current_text_index]['text'] if lessons else ""
|
||||
})
|
||||
|
||||
@app.route('/end_session', methods=['POST'])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue