A simple and flexible starter template for building web applications with Flask. Also, demonstrates how to host a Flask application on PythonAnywhere with CI/CD implementation using GitHub Actions and Webhooks.
- Lightweight & Flexible: Start building web applications quickly with a minimal and adaptable Flask template.
- Structured Setup: Comes with a well-organized project structure to help you maintain and scale your Flask application.
- PythonAnywhere Integration: Includes step-by-step instructions for deploying your Flask app on PythonAnywhere, making it easy to host and manage your application.
- CI/CD with GitHub Actions:
- Automated Testing: Configure GitHub Actions to run automated tests on your code with every push, ensuring that only tested and verified code is deployed.
- Continuous Deployment: Automatically deploy your application to PythonAnywhere when changes are pushed to the repository. This setup uses webhooks to trigger deployments, reducing manual intervention.
- Deployment Pipeline: Set up a streamlined pipeline that includes building, testing, and deploying your application, making the deployment process seamless and efficient.
- Webhook Integration:
- Trigger Deployments: Use webhooks to automatically trigger deployments to PythonAnywhere whenever code is pushed to the repository.
- Post-Merge Hook: Includes a
post-mergescript to reload your application on PythonAnywhere after code updates, ensuring that the latest changes are always live.
- Configuration Management: Easily manage and update your web app’s configurations and dependencies directly through PythonAnywhere’s interface and virtual environment.
- Customizable: The template is designed to be easily customizable, allowing you to adapt it to fit various project requirements and deployment environments.
- Documentation: Comprehensive setup and configuration instructions provided, including details for integrating with PythonAnywhere and configuring CI/CD pipelines.
With these features, you can efficiently build, deploy, and manage your Flask application, while leveraging modern CI/CD practices to streamline your development workflow.
Before you begin, ensure you have the following installed:
-
Clone the Repository
git clone https://github.com/Rahulkumar010/FlaskWebSync.git cd FlaskWebSync -
Create a Virtual Environment
python -m venv venv
-
Activate the Virtual Environment
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install Dependencies
pip install -r requirements.txt
-
Run the Application
python server.py
The application will start on
http://127.0.0.1:5000/by default. -
Access the Application
Open your web browser and navigate to
http://127.0.0.1:5000/.
- Run with
waitress(production-like):
waitress-serve --host=0.0.0.0 --port=5000 server:appProduction (Waitress) — notes
server.pycurrently starts the Flask development server (app.run()) when run directly. For production deployments, usewaitress-serve(as shown above) or another WSGI server. The project'sDockerfileuseswaitress-serveby default.- Ensure
waitressis present inrequirements.txt(it is listed in this repo).
Pre-push checklist
- Environment: Verify
.env/ instance config does not contain secrets committed to the repo. - Tests: Run
pytest -qand resolve failures before pushing. - Build: Optionally build the Docker image locally to confirm the container starts:
- Files added:
Dockerfile,compose.yaml,.dockerignore,README.Docker.md. - Build & run (Docker Compose):
docker compose up --build- Build image and run (Docker):
docker build -t flaskwebsync .
docker run --rm -p 5000:5000 flaskwebsync-
The container serves the app on port
5000(host) mapped to5000in the container. -
See
README.Docker.mdfor additional deployment and platform notes. -
Push Docker image to GCP Artifact Registry
gcloud auth configure-docker
docker build -t {REGION}-docker.pkg.dev/{PROJECT-ID}/{Repository-Name}/flaskwebsync:latest .
docker push {REGION}-docker.pkg.dev/{PROJECT-ID}/{Repository-Name}/flaskwebsync:latestFlaskWebSync/
│
├── app # Main application
├── .env # Environment variables
├── requirements.txt # Project dependencies
├── static/ # Static files (CSS, JS, images)
└── templates/ # HTML templates
-
Create and Push the Flask Application
- Develop your Flask application, generate the
requirements.txtfile with the following command and push it to GitHub:pipreqs ..\sample-flask-app --savepath requirements.txt
- Develop your Flask application, generate the
-
Create a PythonAnywhere Account
- Sign up for a PythonAnywhere account here.
-
Clone the Repository
- Open the Bash terminal in PythonAnywhere and clone your GitHub repository.
-
Set Up the Web App
- Follow the instructions provided by PythonAnywhere to set up your web application.
-
Configure GitHub Webhook
- Add the
./git_updatescript to your main script (e.g.,server.py) and create a webhook with the endpoint like:https://{username|custom_domain}.pythonanywhere.com/git_update
- Add the
-
Create a Post-Merge Hook
- Create a file named
post-mergewith the following content under the./git/hooksdirectory of the cloned Git repository in PythonAnywhere:#!/bin/sh touch {path to WSGI configuration file}- The path to the WSGI configuration file typically looks like
/var/www/{your web application domain}_wsgi.py. - You can find the WSGI configuration file here.
- The path to the WSGI configuration file typically looks like
- Create a file named
-
Set Execution Permissions
- Provide execution permissions to the
post-mergescript with the following command:chmod +x post-merge
- Provide execution permissions to the
-
Install Additional Dependencies
- Follow the guide here to install additional dependencies for your web app and update the configuration accordingly.
-
Continuous Deployment
- With the setup complete, any code updates committed to your GitHub repository will trigger the webhook, which updates the repository on PythonAnywhere. The
post-mergescript will execute to reload the web application, ensuring continuous deployment.
- With the setup complete, any code updates committed to your GitHub repository will trigger the webhook, which updates the repository on PythonAnywhere. The
-
Fork the Repository
Click on the “Fork” button at the top right of this page.
-
Create a New Branch
git checkout -b feature/your-feature
-
Commit Your Changes
git add . git commit -m "Add a meaningful commit message"
-
Push to Your Fork
git push origin feature/your-feature
-
Create a Pull Request
Go to the original repository and click “New Pull Request.”
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or feedback, please reach out to rahul01110100@gmail.com.
- Env Configuration such as
SECRET_KEY, and any specifics in the directory structure with actual details from your project. - Add more detailed sections if your app has additional features or complex setup steps.
Feel free to tweak the structure and content to better match your project’s needs!
Happy coding!