Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
SQLAlchemy Set Up | Database and Models
Professional Web API with Flask
course content

Зміст курсу

Professional Web API with Flask

Professional Web API with Flask

1. Introduction to the Course
2. Database and Models
3. Flask-Smorest and Schemas
4. Endpoints with Blueprints and MethodView
5. Authentication with JWT
6. Deployment

SQLAlchemy Set Up

In our previous project and course, we have gained proficiency in working with SQLAlchemy, storing our recipes in a SQLite database. SQLite is a convenient database for the development process. The transition between databases is seamless in the code, as SQLAlchemy is a highly versatile library that facilitates the connection between the application and many well-known SQL databases.

For those who prefer an interactive approach to database management and wish to see changes in real time, a desktop version of SQLite can be downloaded. If you have the Pro version of PyCharm, you can view changes in a special database window.

To download and set up SQLite on your PC, follow the link provided and select the version compatible with your operating system: SQLite Studio.

With the application installed, you can now explore the structure of your database in detail.

Connecting and Configuring the Database to Your Application

To integrate and configure the database with our application, we first need to install Flask-SQLAlchemy by running:

Next, create a new Python file named 'db.py', where we will initiate our database:

Proceed to the 'app.py' file, which contains all our configurations. After the command that initializes the application, insert several commands related to our database:

Flask Application Factory Pattern

Until now, we created the app variable (which is the Flask application) directly in app.py. Using the application factory pattern, we write a function that returns app. This allows us to pass configuration values to the function, enabling us to customize the application before it's returned. This approach is especially useful for testing and scenarios such as staging and production environments.

To implement the application factory, encapsulate the application creation code inside a function named create_app():

Add new database-related parameters:

We introduced the DATABASE_URI parameter, allowing us to configure the application with a specific database URL obtained from the environment variables. The default value is a local SQLite file if not specified in the environment. We also added two SQLAlchemy configurations to app.config: one for the database URL (or URI) and another to enhance performance by not tracking modifications. Upon application creation, we instruct SQLAlchemy to create all necessary database tables.

Currently, no tables will appear until the application is restarted, as we have not yet defined any models.

Все було зрозуміло?

Секція 2. Розділ 1
We're sorry to hear that something went wrong. What happened?
some-alt