Flask is a lightweight Python web framework perfect for building simple web applications and APIs. Once your app is up and running locally, the next logical step is to share your project with othersβand GitHub is the ideal place to do that.
In this blog post, you’ll learn how to deploy a Flask app to GitHub so others can view, clone, and contribute to your project.
πΉ Note: This guide covers deployment to GitHub (code hosting)βnot deploying your app to a live server. For that, you can use platforms like Heroku, Vercel, or Render (covered in a separate post).
β Why Deploy to GitHub?
Publishing your Flask app to GitHub allows you to:
- Showcase your project in a portfolio
- Collaborate with other developers
- Use version control for better code management
- Set up CI/CD pipelines and hosting
π§° Prerequisites
Before you begin:
- Python and Flask installed locally
- A GitHub account
- Git installed on your machine
- A functional Flask app
π Step-by-Step: Deploying a Flask App to GitHub
1. π Organize Your Flask App
Ensure your project has a clean structure, for example:
/my-flask-app
β
βββ app.py
βββ requirements.txt
βββ templates/
β βββ index.html
βββ static/
β βββ style.css
βββ README.md
app.py
: Your main Flask scriptrequirements.txt
: Lists Python dependenciesREADME.md
: Optional, but recommended for documentation
Run this to generate
requirements.txt
:
pip freeze > requirements.txt
2. π§ Initialize Git Locally
Open your terminal and navigate to your project folder:
cd my-flask-app
git init
Then add and commit your files:
git add .
git commit -m "Initial commit of Flask app"
3. π Create a Repository on GitHub
- Go to github.com and click New Repository.
- Name it (e.g.,
my-flask-app
) and optionally add a description. - Leave it empty (donβt add README, .gitignore, or licenseβyou already have them).
- Click Create Repository.
4. π Push to GitHub
Once the GitHub repo is created, follow the instructions shown. Typically:
git remote add origin https://github.com/your-username/my-flask-app.git
git branch -M main
git push -u origin main
Now your Flask app is live on GitHub!
π¦ Optional: Add a .gitignore
File
To avoid uploading unnecessary files (like __pycache__
or virtual environments), create a .gitignore
file:
__pycache__/
*.pyc
*.pyo
*.pyd
.env
venv/
Commit it:
git add .gitignore
git commit -m "Add .gitignore"
git push
π Whatβs Next?
After publishing your Flask app on GitHub, consider:
- Adding a README with usage instructions
- Including screenshots or a live demo link
- Setting up Heroku, Render, or Fly.io for live deployment
- Creating GitHub issues and pull requests for collaboration
π§ Final Thoughts
Deploying a Flask app to GitHub is a great way to organize, share, and grow your project. Whether youβre building a personal portfolio or collaborating with others, GitHub is a valuable tool in your web development workflow.