Introduction
In this project, I’ve created a basic Flask app that connects to a MySQL database using SQLAlchemy, retrieves data from it, and renders it in an HTML template. It contains two tables through which the data is dynamically displayed on the webpage. It’s essentially a replica of any small to mid-level app.
This project primarily uses Flask, MySQL, and Gunicorn:
- Flask: A lightweight Python framework used to create web applications.
- MySQL: An open-source relational database management system used for storing, managing, and retrieving structured data efficiently.
- Gunicorn: A lightweight Python-based WSGI HTTP server compatible with deploying production-ready Flask or Django applications.
Project Overview
This project demonstrates the process of setting up a web application, packaging it into a reusable module, and deploying it to a Gunicorn server. It serves as a hands-on learning experience for understanding application preparation and deployment workflows.
The major steps involved include:
1. Downloading and setting Up the Code
- To set up the code, we clone the repository to a local folder.
# Clone the repository git clone <repository-url> cd <repository-folder>
- As we used a virtual environment, installing the required dependencies was a challenge.
# Create and activate virtual environment python -m venv venv source venv/bin/activate # For Linux/macOS venv\Scripts\activate # For Windows
- This was overcome by using a requirements file, which contained the necessary dependencies with compatible versions.
2. Customizing the Application
Before we package and deploy the app, we need to make some customizations. These modifications will tailor the app to your specific needs:
- Adding Custom Routes: Introduce custom routes in the Flask app to offer additional functionality, such as handling new HTTP requests or serving different pages.
# Example: Adding a custom route in Flask @app.route('/custom') def custom_route(): return "Custom Route Added!"
- Fetching Data Dynamically: Alter the app to fetch data dynamically from a database and display it on the webpage. This ensures that your app remains interactive and always shows up-to-date information.
# Example: Fetching dynamic data @app.route('/data') def fetch_data(): data = fetch_from_database() # Define this function to fetch from your DB return render_template('data.html', data=data)
These customizations enhance the user experience and prepare the app for the production environment.
3. Packaging the Application
Packaging your Flask app is an essential step before deployment. This step involves creating a script that will automate the process of installing the app on a server or another machine. This script ensures that all required files and dependencies are properly set up. It also makes the app easier to distribute, as it can be installed using pip.
- When packaging the app, you'll want to include:
- All your app files and dependencies
- A
setup.py
or similar script to automate the installation
# Create a setup.py file
echo "from setuptools import setup, find_packages
setup(
name='flask_app',
version='1.0',
packages=find_packages(),
include_package_data=True,
install_requires=open('requirements.txt').readlines(),
)" > setup.py
# Package the app
python setup.py sdist
This step is crucial for ensuring your app is portable and that its dependencies are correctly maintained across different environments.
4. Deploying to the Gunicorn Server
Once your app is packaged, it's time to deploy it to a server. One popular choice for deploying Flask apps is Gunicorn (Green Unicorn). Gunicorn is a WSGI server that efficiently runs your Flask application in production by handling incoming requests and managing multiple workers.
- When deploying to Gunicorn, you’ll need to:
- Start the Gunicorn server using your packaged application.
- Specify parameters like the number of workers and the app module to serve. This ensures the app runs efficiently and can handle multiple simultaneous requests.
# Run the Gunicorn server
gunicorn -w 4 -b 0.0.0.0:8000 <app_module>:<app_instance>
Gunicorn ensures that your app is production-ready, scalable, and can handle heavy traffic.
Challenges Faced and How I Overcame Them
Working on this project presented several challenges, each providing valuable lessons about deployment workflows. Some of the errors were:
-
Dependency Management Issues
- Error: Some dependencies were outdated or mismatched with the project's requirements, causing compatibility issues.
- Solution: Used a virtual environment to isolate and manage dependencies and updated the requirements file.
-
Database Connectivity Errors
- Error: Flask couldn't establish a connection to the MySQL database due to incorrect credentials or host settings.
- Solution: Checked the database credentials and configuration file and tested the connection using standalone MySQL queries.
-
Gunicorn Deployment Errors
-
Error: Gunicorn failed to locate the application's entry point, throwing a
ModuleNotFoundError
. -
Solution: Specified the application instance explicitly in the Gunicorn command (e.g.,
gunicorn app:app
) and tested locally before deploying.
-
Error: Gunicorn failed to locate the application's entry point, throwing a
-
Credential Security Risks
- Error: Sensitive credentials (e.g., database passwords) were exposed or misconfigured.
-
Solution: Used a
.env
file to securely store credentials and loaded them into the app usingpython-dotenv
.
Outcomes
This project focused on gaining a fundamental understanding of packaging and deploying an app. These concepts have strengthened my foundation for real-world integration and development technologies. Although this process was manual, automation tools can improve it significantly (as most developers do).
Conclusion
This project was a great way to learn how real-world apps are prepared and deployed. While the manual deployment process was insightful, it highlighted areas where automation could improve efficiency. Tools like Jenkins could be used to automate the process of packaging, testing, and deploying an application, saving time and reducing errors. Next, I plan to use Jenkins to automate the entire workflow, enabling continuous integration and continuous deployment (CI/CD).
If you're just getting started with deploying apps, try this approach and consider adding automation tools like Jenkins as you go. It’ll save you time and help you handle larger projects with ease.
Have you automated your deployment process yet? Drop a comment and share your experience!
Top comments (0)