Flask integration

How to use Vue with Flask - free starter

This guide will provide you with a free template for a Flask application, with MySQL database and Vue + 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.

Step 1

Creating MySQL database.

In order to create a new database you need to run the following command:

  • Create a new user
  • Provide username, password, database name and description.

CLI will display your username, password, database name and connections string. 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.

Important: Do not close your terminal window until you save your credentials somewhere. This is the only time we will show you your database password. If you won't save it you'll loose it.

Step 2

Create a table.

Go to phpMyAdmin, log in, and add a table. For example on purpose of this tutoria with name tasks. Then create a three columns: id (type: int), name (type: text) and description (type: text).

Step 3

Create Flask project by creating an Python envrionment. Apply these commands into terminal:

Step 4

Now activate created environment. In case that you are working on Windows and command is not working, try this: source ./venv/Scripts/activate. After this you should see venv catalogue and in your terminal will be visible (venv) badge.

Step 5

Install Flask.

Step 6

Create a flaskr folder (in your root directory) and then put inside a __init__.py file with this basic structure.

Step 7

Install Flask MySQL library for handling MySQL database.

Step 8

Edit __init__.py file by adding MySQL database configurations - insert it into create_app function.

Step 9

Install Flask CORS library for handling CORS.

Step 10

Edit __init__.py file by adding these lines preventing CORS errors - insert it into create_app function.

Step 11

Import jsonify and request packages from Flask and add routes for handling requests coming from your frontend part.

Step 12

Start your Flask API.


Creating MDB Vue application

Note: Don't forget to go back to your root folder before next step. Folders flaskr, venv and mdb5-free-vue should be in the same directory.

Step 1

Create a new vite project with our MDB starter. Run the command below and select MDB5 Free Vue starter.

Your folder structure should look like this

  flask/ ├── flaskr ├── mdb5-free-vue └── venv  

Step 2

Let's make some changes to the created vue app. First we need to install axios inside our mdb5-free-vue directory.

Remove style.css file (don't forget to delete it from main.ts file) and remove HelloWorld file from components directory.

Step 3

Let's create a .env file inside a mdb5-free-vue directory. We have to add VITE_ before the name of your variable, because it's the only way to expose them to Vite-processed code. Don't forget to change URL to the one you created earlier.

Step 4

Add new content to Home.vue file inside the views directory.

Since our starter database contains some sample models and routes, let's use them. We will create an application that will show a list of tasks. We also intend to create a functonality for adding new tasks, changing their content and removing them.

We have already prepared the code for you, so go ahead and copy and paste it into App.vue.

Ok, so what's actually going on there. We use MDB components, MDBBtn, MDBModal, MDBListGroup, MDBInputs and few other. The modal will be responsible to show inputs that will allow you to add, edit and send tasks to the database. The Manage tasks button, gives possibilty to modify or remove tasks. At the end, the list group will display our data.

If you haven't done it already, run npm start in your terminal. The app should be fully functional and should work correctly with backend.


Optimization

If you want to further optimize your application please visit: