9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python, MySQL, and Flask together to create a web application?


To create a web application using Python, MySQL, and Flask, the following steps should be taken:

  1. Install all the required packages for the project:
pip install flask
pip install mysql-connector-python
  1. Create a MySQL database and create a table for the web application:
CREATE DATABASE mydb;

USE mydb;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL
);
  1. Create a Python file containing the code for the web application. This file should include code to connect to the MySQL database, code to execute queries and retrieve data from the database, and code to render the web page:
from flask import Flask
import mysql.connector

app = Flask(__name__)

@app.route('/')
def index():
    # Connect to the database
    conn = mysql.connector.connect(
        host='localhost',
        user='root',
        password='',
        database='mydb'
    )
    # Execute a query
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM users')
    # Retrieve the results
    users = cursor.fetchall()
    # Render the page
    return render_template('index.html', users=users)
  1. Create a template file for the web page. This file should contain HTML code and Jinja2 template code to render the data retrieved from the database:
<html>
    <head>
        <title>Users</title>
    </head>
    <body>
        <h1>Users</h1>
        <ul>
            {% for user in users %}
            <li>{{ user.name }}</li>
            {% endfor %}
        </ul>
    </body>
</html>
  1. Run the Python file to start the web application:
python app.py

Finally, the web application should be accessible at http://localhost:5000.

Helpful links

Edit this code on GitHub