Lerndatenbank/app.py

37 lines
1.5 KiB
Python
Raw Normal View History

2024-08-05 16:36:41 +02:00
from flask import Flask, render_template, request, jsonify
from flask_sqlalchemy import SQLAlchemy
2024-08-05 17:48:09 +02:00
# Importing db from models to ensure it exists and is initialized properly
from models import db
2024-08-05 16:36:41 +02:00
2024-08-05 17:48:09 +02:00
# 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
2024-08-05 16:36:41 +02:00
def create_app():
2024-08-05 17:48:09 +02:00
# Create a new Flask application instance with the name of the current module (__name__)
2024-08-05 16:36:41 +02:00
app = Flask(__name__)
2024-08-05 17:48:09 +02:00
# 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
2024-08-05 16:36:41 +02:00
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
2024-08-05 17:48:09 +02:00
# Call the initialize_db function with the newly created Flask app instance
initialize_db(app)
2024-08-05 16:36:41 +02:00
return app
2024-08-05 17:48:09 +02:00
# Entry point for running the Flask application when this script is executed directly
2024-08-05 16:36:41 +02:00
if __name__ == '__main__':
# Create and run the Flask application instance with debugging enabled
app = create_app()
app.run(debug=True)