In this tutorial, we will walk through the process of creating a simple web app with Flask and deploying it to Heroku. Flask is a lightweight web application framework for Python, and Heroku is a cloud platform that allows you to deploy and run your web applications.
To get started, make sure you have Python installed on your computer. You can install Python from the official website (https://www.python.org/downloads/). Once you have Python installed, you can proceed with the following steps.
Step 1: Install Flask
First, we need to install Flask. You can do this by running the following command in your terminal or command prompt:
pip install Flask
Step 2: Create a new Python file
Create a new Python file in your desired directory. You can name this file app.py
or any other name you prefer. To create the file, you can use a text editor or an Integrated Development Environment (IDE) such as VS Code or PyCharm.
Step 3: Write the Flask web app code
In your app.py
file, write the following code to create a simple Flask web app:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
This code creates a basic Flask web app that displays "Hello, World!" when you visit the homepage.
Step 4: Run the Flask web app
To run the Flask web app, navigate to the directory where your app.py
file is located using the terminal or command prompt. Then, run the following command:
python app.py
This command will start a local development server, and you can access the web app by visiting http://127.0.0.1:5000
in your web browser.
Step 5: Sign up for Heroku
If you don’t already have a Heroku account, you can sign up for free at https://www.heroku.com/. Once you have signed up and logged in to your Heroku account, you can proceed with deploying your Flask web app.
Step 6: Configure the Heroku app
Before deploying your Flask web app to Heroku, you need to create a requirements.txt
file and a Procfile
in your project directory.
To create the requirements.txt
file, run the following command in your terminal or command prompt:
pip freeze > requirements.txt
This command will generate a list of all Python packages installed in your virtual environment and write them to the requirements.txt
file.
Next, create a Procfile
in your project directory (the same directory as your app.py
file) and add the following line to it:
web: python app.py
Step 7: Deploy the Flask web app to Heroku
To deploy your Flask web app to Heroku, you first need to install the Heroku Command Line Interface (CLI). You can download and install the Heroku CLI from https://devcenter.heroku.com/articles/heroku-cli.
After installing the Heroku CLI, log in to your Heroku account by running the following command in your terminal or command prompt:
heroku login
Next, navigate to your project directory in the terminal or command prompt and create a new Heroku app by running the following command:
heroku create
This command will create a new Heroku app and provide you with a URL where your web app will be deployed.
Finally, deploy your Flask web app to Heroku by running the following command:
git push heroku master
This command will push your code to the Heroku remote repository and deploy your Flask web app to the Heroku platform.
Step 8: Access your Flask web app on Heroku
Once the deployment process is complete, you can access your Flask web app by visiting the URL provided by Heroku after running the heroku create
command.
Congratulations! You have successfully created a simple web app with Flask and deployed it to Heroku. This tutorial provides a basic introduction to building web applications with Flask and deploying them to a cloud platform such as Heroku. You can further customize and enhance your Flask web app by adding additional routes, templates, and functionality. Happy coding!
Note: html and css are not programming languages they’re mark up languages, as there is nothing in html or css that makes them Turing complete
Flask is also great when blended with React. You can use CORS and axios for this
Clear and helpful as always. Thanks!
thank you so much!!!!!! i able to create my own website now!!!
Make a video using the Django Framework too
Thanks!
i like the way you teach it makes it so easy to under stand as a beginner
thanks lovely you do it in the most simplest way
How I can found your have a problem in flask run
Holy cow the amount of makeup in your face can cover the whole victoria's secret parade
Thanks
Hello Maria, am failing to get the url, what can I do
So I am stuck on the "flash" part. Whenever I tried to add the flash module it breaks the application. Soon as I comment it out, It works no problem. Everything else works fine. Any suggestions?
thanks
Hot Girl
Sadly Heroku is not free anymore, great video tho im going to deploy with vercel
Awesome video! Thank you so much. Спасибо 🙂
How am i just finding your channel now 😊
Great stuff
I have created a Html file, but nothing appears i need help.
Thank you so much for this explanation!!