{"id":22358236,"url":"https://github.com/marincervinschi/flask-mysql-template","last_synced_at":"2026-04-08T16:02:05.631Z","repository":{"id":266303622,"uuid":"897970584","full_name":"MarinCervinschi/flask-mysql-template","owner":"MarinCervinschi","description":"This repository provides a robust template for building Flask applications with MySQL integration. It includes Docker support for database containerization, automated testing with GitHub Actions, and a modular structure for scalability and maintainability.","archived":false,"fork":false,"pushed_at":"2024-12-03T17:58:33.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-24T07:49:45.494Z","etag":null,"topics":["actions","coverage","coverage-report","docker","docker-compose","flask","flask-api","flask-backend","flask-restful","flask-web","github-actions","mysql","mysql-database","mysql-server","pytest","python","python3","test-automation","testing","workflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarinCervinschi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-03T15:02:31.000Z","updated_at":"2024-12-03T17:58:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"5170c4cf-37e3-4a6e-b6ea-0c00b208b882","html_url":"https://github.com/MarinCervinschi/flask-mysql-template","commit_stats":null,"previous_names":["marincervinschi/flask-mysql-template"],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/MarinCervinschi/flask-mysql-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarinCervinschi%2Fflask-mysql-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarinCervinschi%2Fflask-mysql-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarinCervinschi%2Fflask-mysql-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarinCervinschi%2Fflask-mysql-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarinCervinschi","download_url":"https://codeload.github.com/MarinCervinschi/flask-mysql-template/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarinCervinschi%2Fflask-mysql-template/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31562697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actions","coverage","coverage-report","docker","docker-compose","flask","flask-api","flask-backend","flask-restful","flask-web","github-actions","mysql","mysql-database","mysql-server","pytest","python","python3","test-automation","testing","workflow"],"created_at":"2024-12-04T15:13:16.547Z","updated_at":"2026-04-08T16:02:05.614Z","avatar_url":"https://github.com/MarinCervinschi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n[![GitHub license](https://img.shields.io/github/license/MarinCervinschi/flask-mysql-template)](LICENSE)\n[![GitHub stars](https://img.shields.io/github/stars/MarinCervinschi/flask-mysql-template)]()\n[![GitHub forks](https://img.shields.io/github/forks/MarinCervinschi/flask-mysql-template)]()\n[![GitHub issues](https://img.shields.io/github/issues/MarinCervinschi/flask-mysql-template)]()\n[![GitHub visitors](https://visitor-badge.laobi.icu/badge?page_id=MarinCervinschi.flask-mysql-template\u0026)]()\n\n# Flask MySQL Template 🐍🐬\n## Overview\n\nThis repository provides a Flask application template integrated with MySQL. It leverages modular design principles, Docker for database containerization, and a robust testing suite to ensure ease of use and extensibility. Additionally, it uses GitHub Actions for automated testing to maintain code quality.\n\n\u003e **‼️ Use as a template:**\n\u003e \n\u003e To use it as a template for your project, you can click on the \"Use this template\" button on the repository page. This will create a new repository with the same structure and files as this one.\n\n### Technologies\n\n[![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge\u0026logo=python\u0026logoColor=ffdd54)](https://www.python.org/)\n[![Flask](https://img.shields.io/badge/flask-%23000.svg?style=for-the-badge\u0026logo=flask\u0026logoColor=white)](https://flask.palletsprojects.com/en/stable/)\n[![MySQL](https://img.shields.io/badge/mysql-4479A1.svg?style=for-the-badge\u0026logo=mysql\u0026logoColor=white)](https://www.mysql.com/)\n[![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge\u0026logo=docker\u0026logoColor=white)](https://www.docker.com/)\n[![Pytest](https://img.shields.io/badge/pytest-%23ffffff.svg?style=for-the-badge\u0026logo=pytest\u0026logoColor=2f9fe3)](https://docs.pytest.org/en/6.2.x/)\n[![GitHub Actions](https://img.shields.io/badge/github%20actions-%232671E5.svg?style=for-the-badge\u0026logo=githubactions\u0026logoColor=white)](.github/workflows/python-app.yml)\n\n- [Flask-MySQLdb](https://pypi.org/project/Flask-MySQLdb/): A Flask extension that provides MySQL database connectivity.\n- [Coverage](https://coverage.readthedocs.io/en/6.2/): A tool for measuring code coverage of Python programs.\n\n### Features\n\n- [Project Structure](#repository-structure): Modular design for easy extensibility.\n- [Getting Started](#getting-started): Instructions to set up and run the application.\n- [Database Setup](#database-setup): Steps to initialize the MySQL database schema.\n- [CRUD Operations](#crud-operations-in-db_routepy): Demonstrates CRUD operations using Flask and MySQL.\n- [Running Tests and Coverage](#running-tests-and-coverage): Instructions to run tests and generate coverage reports.\n\n## Continuous Integration with GitHub Actions\n\nThis repository is configured to automatically run tests on every commit and pull request using **GitHub Actions**. The CI pipeline ensures that new changes do not introduce errors or break existing features.\n\n### GitHub Actions Workflow\n\n•\t**Location**: `.github/workflows/python-app.yml`\n\n•\t**Workflow Description**:\n\n1.\tInstalls the application requirements.\n\n2.\tSets up the testing environment using Python and MySQL.\n\n3.\tRuns the test suite to validate the application.\n\n**Key Features:**\n\n1.\t**Automated Tests**:\n\n•\tEnsures all unit tests in the tests/ directory are executed on every commit.\n\n•\tReports errors if any test fails, preventing the merge of faulty code.\n\n2.\t**Dependency Installation**:\n\n•\tInstalls dependencies specified in requirements.txt to replicate the application environment.\n\n3.\t**Fast Feedback**:\n\n•\tDevelopers are notified of issues immediately after pushing code, enabling faster debugging and resolution.\n\n\n### Repository Structure\n\n```\n├── README.md                  # Documentation for the repository\n├── LICENSE                    # License information\n├── app/                       # Core application logic\n│   ├── __init__.py            # App initialization\n│   ├── config.py              # Configuration settings\n│   ├── db.py                  # Database connection logic\n│   ├── factory.py             # Application factory pattern implementation\n│   └── routes/                # Application routes\n│       ├── __init__.py        # Routes initialization\n│       ├── db_route.py        # Database-related routes (CRUD operations)\n│       └── main.py            # Main application routes\n├── docker-compose.yml         # Docker configuration for MySQL\n├── requirements.txt           # Python dependencies\n├── run.py                     # Entry point to run the Flask application\n├── schema.sql                 # MySQL database schema\n└── tests/                     # Testing suite\n    ├── conftest.py            # Pytest fixtures and configurations\n    ├── data.sql               # Sample data for tests\n    ├── test_db.py             # Unit tests for `db.py`\n    ├── test_db_route.py       # Unit tests for `db_route.py`\n    ├── test_factory.py        # Unit tests for `factory.py`\n    └── test_main.py           # Unit tests for `main.py`\n```\n\n## Getting Started\n\n### Prerequisites\n\n•\t[Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/)\n\n•\tPython 3.9+ installed locally\n\n•\tMySQL client installed locally (optional for local database interactions)\n\n### Running the Application\n\n**With Docker and Flask**\n\n1.\t**Set up the** .env **File**:\n\nCreate a `.env` file in the root directory with the following content to configure the MySQL database for both Docker and Flask:\n```bash\nMYSQL_ROOT_PASSWORD=password # Change this to a secure password\n\n#MYSQL_USER=root # You don't need for docker-compose\n\nMYSQL_PASSWORD=password # Change this to a secure password (same as MYSQL_ROOT_PASSWORD)\n\nMYSQL_HOST=127.0.0.1 # flask dosn't like localhost\n\nMYSQL_PORT=3306 # default port\n\nMYSQL_DATABASE=mysql_db # Change this to the desired database name\n```\n\n2.\t**Start the MySQL Container**:\n\n```bash\ndocker-compose up --build -d\n```\n\n3.\t**Run Flask Locally**:\n\nCreate a virtual environment and install the dependencies:\n\n```bash\npython3 -m venv .venv\nsource .venv/bin/activate\n```\n\n```bash\npip install -r requirements.txt\n```\n```bash\npython run.py\n```\n\nThe application will be available at: [http://127.0.0.1:5000](http://127.0.0.1:5000/)\n\n## Database Setup\n\n**Schema Initialization**\n\nWith the MySQL container build, you also initialize the database schema.sql file.\nIf you change it, to apply the changes you can run the following command:\n    \n```bash\ndocker exec -i \u003cmysql-container-name\u003e mysql -u root -p \u003cyour-db-name\u003e \u003c schema.sql\n```\nThen you need to enter the password\n```bash\nEnter password: ...\n```\n\nTo access the MySQL CLI, run the following command:\n\n```bash\ndocker exec -it \u003cmysql-container-name\u003e mysql -u root -p \u003cyour-db-name\u003e\n```\n## CRUD Operations in [db_route.py](app/routes/db_route.py)\n\nThe db_route.py implements CRUD (Create, Read, Update, Delete) operations using Flask and MySQL. It uses the [db.py](app/db.py) module that i created to interact with the database. It includes personal methods to handle the database connection, execute queries, and fetch results.\n\nIn particular:\n- `get_db()` is a helper function that creates a database connection.\n- `json_data()` is a helper function that converts the MySQL query results into JSON format.\n- `query_db()` is a helper function that executes a query and returns the results.\n    - It accepts the query and a tuple of parameters.\n    - It has also two optional parameters: `one` and `commit` that are set to False by default.\n        - `one` is used to fetch a single result.\n            - By default, it is set to False and returns all results.\n        - `commit` is used to commit the transaction.\n            - It is set to True when the query is an INSERT, UPDATE, or DELETE operation.\n    - It returns the query results as a list of dictionaries.\n- `init_db()` is a helper function that initializes the database schema.\n    - It reads the schema.sql file and executes the queries to create the tables.\n    - It's a command line that you can run to initialize the database schema.\n    \n        ```bash\n        flask init-db\n        ```\n\nThese routes demonstrate how to use db.py functions to interact with the database effectively. Refer to [db_route.py](app/routes/db_route.py) for implementation details.\n\n## Running Tests and Coverage\n\n1.\t**Run Tests**:\n```bash\npython -m pytest tests/\n```\n2.\t**Run Specific Test**:\n\n```bash\npython -m pytest tests/\u003ctest_file_name\u003e.py\n```\n\n3. Generate a coverage report:\n```bash\ncoverage run -m pytest\ncoverage report\n```\n\n4. To generate an HTML coverage report:\n```bash\ncoverage html\n```\nOpen `htmlcov/index.html` in your web browser to view the report.\n\n---\n### Contributing\n\nCheck out the [contributing guidelines](.github/CONTRIBUTING.md) to get involved in the project.\n\n### Issues\n\n- Bug issues: [Report a bug](.github/ISSUE_TEMPLATE/bug_report.md)\n- Custom issues: [Custom issue](.github/ISSUE_TEMPLATE/custom.md)\n- Feature requests: [Request a feature](.github/ISSUE_TEMPLATE/feature_request.md)\n\n# Happy Coding! 😊","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarincervinschi%2Fflask-mysql-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarincervinschi%2Fflask-mysql-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarincervinschi%2Fflask-mysql-template/lists"}