deploy flask on heroku

To deploy a Flask application on Heroku, follow these steps:

  1. Create a new Heroku app: Go to the Heroku website and log in to your account. Create a new app by clicking on the "New" button and selecting "Create new app". Give your app a unique name and choose the region where you want to deploy it.

  2. Set up a Git repository: In your local development environment, navigate to the root directory of your Flask application. Initialize a new Git repository by running the command "git init". Add all your project files to the repository by running "git add .". Commit the changes by running "git commit -m 'Initial commit'".

  3. Connect your local repository to Heroku: On the Heroku app dashboard, go to the "Deploy" tab. Under the "Deployment method" section, choose "Connect to GitHub" or "Connect to GitLab" depending on your version control platform. Follow the prompts to authorize Heroku to access your repository.

  4. Configure Heroku: Once your repository is connected, you can customize the deployment settings. In the "Automatic deploys" section, choose the branch you want to deploy. In the "Manual deploy" section, click on the "Deploy Branch" button to manually trigger a deployment.

  5. Specify the Python runtime: To let Heroku know that you're deploying a Flask application, you need to specify the Python runtime. Create a file named "runtime.txt" in your project's root directory. Inside the file, write "python-3.9.1" (or the version of Python you are using).

  6. Create a requirements file: Heroku needs a file that lists all the Python packages required for your Flask application. Create a file named "requirements.txt" in your project's root directory. Inside the file, list all the packages needed, one package per line. For example:

Flask==2.1.0
Gunicorn==20.1.0
  1. Create a Procfile: Heroku uses a Procfile to determine how to start your Flask application. Create a file named "Procfile" in your project's root directory. Inside the file, write the following line:
web: gunicorn app:app

This tells Heroku to use Gunicorn as the web server and to run the Flask app object named "app" in the file named "app.py" (or whatever your Flask app file is named).

  1. Push your changes to the repository: In your local development environment, run the command "git push heroku master" (or "main" if you're using the main branch). This will push your code to the Heroku app and trigger a new deployment.

  2. Monitor the deployment: After pushing your changes, you can monitor the deployment process on the Heroku app dashboard. Once the deployment is complete, you should see a success message.

  3. Test your application: Access your Flask application by visiting the URL provided by Heroku. You should see your Flask app running successfully.

That's it! Your Flask application is now deployed on Heroku.