Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
January 5, 2024

Flask and SQL application on Ubuntu

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Flask:

Flask is a lightweight web framework for Python. It is easy to learn and allows you to quickly build web applications. Flask follows the WSGI (Web Server Gateway Interface) standard and provides tools, libraries, and templates to simplify web development.

MySQL:

MySQL is a popular open-source relational database management system. It is widely used for managing and organizing data in a structured manner. MySQL supports SQL (Structured Query Language), making it easy to interact with databases.

Python:

Python is a popular choice for back-end development due to its readability, versatility, and the availability of various frameworks. Flask and Django are two commonly used Python frameworks for building web applications.

Why Flask:

  1. Lightweight and Simple: Flask is a lightweight micro-framework that is simple to understand and easy to get started with. It doesn’t come with unnecessary components, allowing developers to choose and integrate only what they need for their specific project.
  2. Flexibility: Flask provides a flexible structure, allowing developers to choose their own tools and libraries for various components like databases, authentication, and templating. This flexibility is beneficial when tailoring the application to specific requirements.
  3. Extensive Documentation: Flask has extensive and well-documented resources, making it easy for developers to find solutions to common problems and learn how to use various features. This is especially valuable for beginners.
  4. Large Community: Flask has a large and active community of developers. This means there are plenty of tutorials, plugins, and extensions available, and developers can seek help from the community when facing challenges.
  5. Widely Used: Flask is widely used in the industry, and many companies and startups choose it for building web applications. This popularity ensures that there is a wealth of knowledge and experience available for developers.

Why SQL:

  1. Relational Data Structure: SQL (Structured Query Language) is designed for managing and querying relational databases. Many real-world applications deal with structured data, and SQL provides a powerful and standardized way to interact with relational databases.
  2. Data Integrity: SQL databases enforce data integrity through features like constraints, foreign keys, and transactions. This ensures that the data remains accurate and consistent, which is crucial for applications that rely on reliable and structured information.
  3. Scalability: SQL databases are scalable, allowing for the efficient management of large datasets and high transaction volumes. This scalability is essential for applications that need to handle a growing user base and increasing amounts of data.
  4. ACID Properties: SQL databases adhere to ACID (Atomicity, Consistency, Isolation, Durability) properties, providing a high level of reliability and ensuring that database transactions are processed reliably even in the face of errors or system failures.
  5. Standardization: SQL is an industry-standard language for interacting with databases. This standardization means that knowledge and skills acquired while working with one SQL database (e.g., MySQL, PostgreSQL) are often transferable to others.

In summary, Flask is chosen for its simplicity, flexibility, and community support, while SQL is chosen for its suitability in managing structured data, ensuring data integrity, and providing a standardized way to interact with databases. The combination of Flask and SQL is a popular choice for building web applications with a back-end that involves data storage and retrieval.

Installation of Flask

Ubuntu 20.04 ships with Python 3.8. You can verify that Python is installed on your system by typing:

python3 -V

The recommended way to create a virtual environment is by using the venv module, which is provided by the python3-venv package. Run the following command to install the package:

sudo apt install python3-venv

Now create a folder for your flask application

mkdir flask_app && cd flask_app

Create a virtual environment named venv:

python3 -m venv venv

Start the environment:

source venv/bin/activate

Install FLASK:

pip install Flask

Check the installation:

python3 -m flask --version

Output should look like this:

Python 3.8.5   Flask 1.1.2   Werkzeug 1.0.1

Installation of SQL:

To install MySQL on Ubuntu, you can use the package manager apt. Here are the steps to install MySQL on Ubuntu:

1.Update Package Lists:

Before installing any new software, it’s a good practice to update the package lists to get the latest information about available packages. Open a terminal and run:

sudo apt update

2. Install MySQL Server:

Now you can install the MySQL server package. You will be prompted to set a password for the MySQL root user during the installation process.

sudo apt install mysql-server

3. Secure MySQL Installation (Optional but Recommended):

MySQL comes with a script that can help you secure the installation. It will guide you through various security-related configurations.

sudo mysql_secure_installation

Follow the on-screen instructions to set a password for the MySQL root user, remove anonymous users, disallow remote root login, and remove the test database.

4. Start and Enable MySQL Service:

MySQL should start automatically after installation. If not, you can start it manually:

sudo systemctl start mysql

To ensure that MySQL starts on boot, you can enable it:

sudo systemctl enable mysql

5. Check MySQL Status:

You can check the status of the MySQL service to ensure that it’s running:

sudo systemctl status mysql

6. Access MySQL:

You can access the MySQL shell using the following command. You will be prompted to enter the MySQL root password.

mysql -u root -p

Now you have MySQL installed and can start creating databases, tables, and managing users using the MySQL shell or a database management tool.

Remember that securing your MySQL installation is crucial for the safety of your data, so make sure to follow the best practices for database security.

Connect SQL with Python:

To connect to a MySQL database in Python, you can use the mysql-connector-python library, which is an official MySQL driver for Python provided by the MySQL team. Here's a simple example of how to connect to a MySQL database using this library:

If you haven’t installed the mysql-connector-python library yet, you can do so using:

pip install mysql-connector-python

SQL CLI:

Creating a database and creating a Table
SQL connection and executing a command

A Simple Flask Application Program:

main.py

login.html

dashboard.html

Running Flask Application:

Exporting the file name with environment variable and running flask app

Output:

Table for users — username and password.

Table for records — student name, roll number and marks

Login Page UI

Display Page UI

CodeStax.Ai
Profile
January 5, 2024
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles