Lerndatenbank/app.py

37 lines
1.5 KiB
Python

from flask import Flask, render_template, request, jsonify
from flask_sqlalchemy import SQLAlchemy
# Importing db from models to ensure it exists and is initialized properly
from models import db
# Function to initialize the database with the app context
def initialize_db(app):
# Push the application context to make it available within this function
app.app_context().push()
# Initialize the SQLAlchemy database with the Flask app configuration
db.init_app(app)
# Create all tables defined in the models (if they don't already exist)
db.create_all()
# Commit any changes to the database session
db.session.commit()
# Function to create and configure the Flask application
def create_app():
# Create a new Flask application instance with the name of the current module (__name__)
app = Flask(__name__)
# Configure the SQLAlchemy database URI for SQLite, using 'learn.db' as the file location
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///learn.db'
# Disable tracking modifications of objects in the session to improve performance
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Call the initialize_db function with the newly created Flask app instance
initialize_db(app)
return app
# Entry point for running the Flask application when this script is executed directly
if __name__ == '__main__':
# Create and run the Flask application instance with debugging enabled
app = create_app()
app.run(debug=True)