Connect with us

Git

How to Deploy a Flask App on GitHub?

Spread the love

GitHub is a powerful platform for version control, collaboration, and even hosting. For Python developers working with Flask, GitHub offers an easy and reliable way to deploy and manage applications. In this post, we’ll walk through the process of deploying a Flask application to GitHub, so you can share it with others, collaborate effectively, and even integrate with other tools.


Why Deploy a Flask App on GitHub?

Deploying a Flask app on GitHub can offer multiple benefits:

  1. Version Control: GitHub allows you to track changes, manage versions, and collaborate seamlessly.
  2. Easy Collaboration: Team members can contribute, review code, and track progress in real-time.
  3. Continuous Deployment: GitHub Actions enable automated workflows, including testing and deployment.
  4. Project Management Tools: GitHub offers issues, project boards, and milestones to help manage development.

Prerequisites

Before starting, make sure you have:

  • A GitHub Account: Sign up at GitHub if you don’t have an account yet.
  • Git Installed: Download and install Git from git-scm.com.
  • A Flask Application: This guide assumes you have a working Flask app. If not, create one by following the Flask documentation.

Step 1: Set Up Your Flask Application

Let’s assume you have a basic Flask application set up as follows:

my_flask_app/
├── app/
│   ├── __init__.py
│   └── routes.py
├── requirements.txt
└── app.py
  • app.py: The main entry point of your Flask application.
  • app/: A directory with additional modules and routes.
  • requirements.txt: A file listing all dependencies of your Flask app.

Example app.py Code

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(debug=True)

Step 2: Create a requirements.txt File

To make sure others can install the necessary dependencies, create a requirements.txt file.

pip freeze > requirements.txt

This command captures your current environment’s dependencies and writes them to requirements.txt. Anyone who clones your repository can use this file to recreate the environment by running:

pip install -r requirements.txt

Step 3: Initialize a Git Repository

  1. Open a terminal in your Flask app directory.
  2. Initialize a Git repository:
   git init
  1. Add all files to the repository:
   git add .
  1. Commit the files:
   git commit -m "Initial commit of Flask app"

Step 4: Create a GitHub Repository

  1. Log in to GitHub and go to https://github.com/new.
  2. Enter a name for your repository (e.g., my-flask-app).
  3. Optionally, add a description and choose to make it public or private.
  4. Click Create repository.

Step 5: Push Your Flask App to GitHub

  1. Go back to your terminal and add the remote repository URL:
   git remote add origin https://github.com/your-username/my-flask-app.git
  1. Push your local code to GitHub:
   git push -u origin main

Step 6: Configure GitHub Actions for Deployment (Optional)

GitHub Actions can automate the deployment process. Let’s set up a simple workflow to run the app’s tests every time you push code.

  1. In your repository on GitHub, go to the Actions tab.
  2. Select Set up a workflow yourself.
  3. Create a .github/workflows/main.yml file with the following content:
   name: Flask CI

   on: [push]

   jobs:
     build:
       runs-on: ubuntu-latest
       steps:
         - uses: actions/checkout@v2
         - name: Set up Python
           uses: actions/setup-python@v2
           with:
             python-version: '3.x'
         - name: Install dependencies
           run: |
             python -m pip install --upgrade pip
             pip install -r requirements.txt
         - name: Run Tests
           run: |
             pytest  # Assuming you have tests set up for your Flask app
  1. Commit and push the workflow file. GitHub will now automatically run tests each time you push a change.

Step 7: Host Your Flask App on GitHub Pages or an External Service

Since GitHub Pages only supports static sites, you’ll need to use an external provider for live hosting. Here are some popular options:

  • Heroku: Easy-to-use platform with free hosting for basic applications.
  • Render: Another popular hosting platform with straightforward deployment for Python apps.
  • Vercel: Primarily for frontend applications, but can also handle API backends.

For example, deploying to Heroku is straightforward:

  1. Install the Heroku CLI.
  2. Run heroku login in your terminal.
  3. In the terminal, create a Heroku app:
   heroku create my-flask-app
  1. Push your code to Heroku:
   git push heroku main

Heroku will build and deploy your app automatically. You can view it by visiting the URL provided by Heroku.


Final Thoughts

By deploying your Flask application to GitHub, you enable easy collaboration, effective version control, and streamlined project management. Integrating GitHub Actions can automate testing and deployment, keeping your codebase healthy and ready for production. Although GitHub Pages doesn’t support live Flask applications, using services like Heroku or Render enables you to make your app accessible to others online.

With these steps, your Flask application is ready to be shared, collaborated on, and even deployed live for users to experience.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *