Flask Integration
How to use Bootstrap 5 with Flask - free starter
This guide will provide you with a free template for a Flask application, with MySQL database and Bootstrap 5 front-end.
Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Flask application
Let's create a fresh Flask application so that we can go through all the steps together. For this tutorial we'll be using MySQL database.
Note: Depending on your OS you may need to use different commands. Copy the commands appropriate for your operating system.
Step 1
First, we need to create a project directory. We'll use a virtual environment to manage the dependencies for your project.
Step 2
Before you work on your project, activate the corresponding environment. In case you are working on Windows
and the command is not working, try this: source ./venv/Scripts/activate
You should see venv
folder added to your project directory, and
in your terminal, the (venv)
badge will be added before the path.
Step 3
Use the following command to install Flask.
Step 4
Open your project directory in the code editor. Create folder toDoApp
, there create
__init__.py
file. Paste the content of the snippet below as its content.
Step 5
Install Flask SQLAlchemy
library for handling MySQL database.
Step 6
Creating a MySQL database.
In order to create a new database you need to run the following command.
Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli
.
Now log in with your MDB account, type: mdb login
.
If you don't have account yet you can create one using mdb register
command.
- Create a new user
- Provide username, password, database name and description.
CLI will display your username, password, database name and connections string. You will need this data in the next step. Now you can go to phpMyAdmin where you will be able to handle the administration of the MySQL database.
Note: the password must contain at least one uppercase letter, one lowercase letter, one number, one special symbol and have minimum length of 8.
Step 7
Edit __init__.py
file by adding MySQL database configuration. Change the value between brackets
for the database URL you got after creating a new MySQL Database from our starter.
Step 8
Inside the toDoApp
directory create folder templates
, and there create an index.html
file. There paste example content from the snippet below to see if the formatting is correct after you
link CSS files.
Step 9
Run your app with a command from the snippet below. The first launch of the application will create tables associated with your model in your database.
Now head over to http://127.0.0.1:5000/ and you should see your example page in the browser.
Install MDB
In this section, we will add MDB styles and JavaScript files to the example page.
Step 1
From base the project directory type the following command in the terminal.
It will change the directory to toDoApp/static
and initialize MDB CLI. From the list of starters select Standard
.
Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli
.
Now log in with your MDB account, type: mdb login
.
If you don't have account yet you can create one using mdb register
command.
Step 2
In the head
of the index.html
file import static CSS files.
Step 3
At the end of the body
tag in the index.html
file import static JS files.
Now you're done
With MDB's CSS and JS fully loaded, after restarting the server your example page should work and you should see MDB styles applied to its content.
To Do App with MDB
In this section, we will present the capabilities of Ruby on Rails used with the MDB package by creating an example To Do App.
Step 1
Import request
, redirect
and url_for
packages from Flask. Change a route assigned to the index
and add routes
to add
, delete
, and update
.
Step 2
Change the content of the body
tag in the index.html
file to the content of the snippet below but don't remove the script
tag that imports JavaScript files.
Step 3
At the end of the body
tag in the index.html
add code from the snippet below.
To access your application visit http://localhost:3000. There you should see a simple To Do app connected to MySQL database.
Optimization
If you want to further optimize your application please visit:
Backend features
Flask:
This example was created with the use of Flask. Our app is connected to MySQL database and is ready to receive get, post, put
and delete
requests.
MDB CLI:
The database in this example was created using MDB CLI with Graphical User Interface available under phpmyadmin.mdbgo.com
Frontend features
MDB UI KIT:
We have successfully added MDB UI Kit to the created app, with which we can build basic views very quickly.
Views and Layouts:
We have successfully integrated the backend with the MDB package and can send basic requests to the backend Flask application.