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.