Building a Python Flask Web Application from Scratch: Part 1 – Starting Out

Posted by


In this tutorial, we will be learning how to create a web application using Python Flask from scratch. Flask is a lightweight web application framework for Python that allows you to easily build web applications.

In this first part of the tutorial, we will be focusing on getting started with Flask, setting up the necessary environment, and creating a basic web application.

Step 1: Install Flask
To get started with Flask, you will need to have Python installed on your system. You can download Python from the official website (https://www.python.org/).

Once you have Python installed, you can install Flask using pip, which is Python’s package installer. Open your command prompt or terminal and run the following command:

pip install Flask

This will install Flask on your system. You can verify that Flask has been installed by running the following command:

flask --version

Step 2: Setting up the Flask environment
Now that you have Flask installed, you can create a new directory for your Flask project. Navigate to the directory where you want to create your project and run the following command to create a new virtual environment:

python -m venv venv

This will create a new virtual environment in a directory called venv. Next, activate the virtual environment by running the following command:

On Windows:

venvScriptsactivate

On Mac or Linux:

source venv/bin/activate

Step 3: Creating a basic Flask application
Now that we have set up our Flask environment, we can create a basic Flask application. Create a new Python file in your project directory and name it app.py.

In app.py, we will create a simple Flask application that displays a message when you visit the home page. Here is the code for the basic Flask application:

from flask import Flask

app = Flask(__name__)

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

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

This code creates a new Flask application that responds with the message ‘Hello, Flask!’ when you visit the home page.

Step 4: Running the Flask application
To run the Flask application, simply navigate to your project directory in the command prompt or terminal and run the following command:

python app.py

This will start the Flask development server and you will see a message that the server is running. Open your web browser and go to http://127.0.0.1:5000/ to see your Flask application in action.

Congratulations! You have successfully created a basic Flask application from scratch. In the next part of the tutorial, we will learn how to create more complex web applications using Flask. Stay tuned for Part 2!

0 0 votes
Article Rating
42 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@TraversyMedia
1 month ago

For those of you that do not care for Python, don't worry, this was a one shot series from start to finish. It is not taking away from the HTML, JavaScript, Node.js, PHP, etc

@johnmahugu
1 month ago

More Please 😀

@bharatht9928
1 month ago

I want to learn flask this is enough for start to learn basic to advance

@khalidelgazzar
1 month ago

Super great series. Thank you!

@lekenorbert4973
1 month ago

I can't find the video where he installed and implemented the mySQL. anyhelp?

@Repost-updated
1 month ago

24:00 it's not Ninja, it's Jinja 👋, very tuts, thanks buddy 👐

@sarath_sajan
1 month ago

Hey thanks to you (especially this flask tutorial) and Sentdex (another yt channel) i was able to land a job in the software industry. I can't thank you enough for all the free expertise that channels like yours give to students like me.

@WHWrooolz
1 month ago

note: had to use a different navbar template because the one used in video isn't up anymore.

<nav class="navbar navbar-expand navbar-dark bg-dark" aria-label="Second navbar example">

<div class="container-fluid">

<a class="navbar-brand" href="/">MyFlaskAPP</a>

<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarsExample02" aria-controls="navbarsExample02" aria-expanded="false" aria-label="Toggle navigation">

<span class="navbar-toggler-icon"></span>

</button>

<div class="collapse navbar-collapse" id="navbarsExample02">

<ul class="navbar-nav me-auto">

<li class="nav-item">

<a class="nav-link active" aria-current="page" href="/home">Home</a>

</li>

<li class="nav-item">

<a class="nav-link" href="/about">about</a>

</li>

<li class="nav-item">

<a class="nav-link" href="/articles">Articles</a>

</li>

</ul>

</div>

</div>

</nav>

@kyomdonalddogo5775
1 month ago

hey brad. I love the vid. Please I need help with the bootstrap at 17:12, because the result I am getting is different from yours on the browser. Please help me sir. Thanks

@tomdevisser8150
1 month ago

Wouldn't it be better to create a virtual environment before installing Flask? Or is there a particular reason you skipped this step that most tutorials mention?

@marcmaura8396
1 month ago

thank you

@Willschmitt17
1 month ago

You are a freaking legend dude. I have been struggling with bad tutorials for months and this one finally helped me. Plus its the first tutorial where anyone has created an "actual app"

@johnyhomson9223
1 month ago

Hello, nice tutorial ! Thanks
I've got one question : is someone know how can I have Ubuntu and Windows at the same time ? Just like in the tutorial

@mostmojo
1 month ago

20:10 – why do you have the function – def Articles(): written with a capital "A" ? I thought python functions were all snake_case. I tried to make a snake_case function name but it says function is not iterable

@LibanMedia
1 month ago

thanks alot such a great tutorial ,love it

@GurdeepSingh-jz3lb
1 month ago

hello brother where is the cheat sheet of this series?

@Nearco1000
1 month ago

Wondering if there is pytest/unittest series for this? 🙂

@ermacheton
1 month ago

My man, why is it that I still get the NOT FOUND even after I create the route??

@chaitrakulkarni7704
1 month ago

Even the ads on your channel are very useful I take pictures of the ad. I really appreciate your effort. u teach in a way even a kid can understand. I am a beginner in web development but I am getting good at it day by day.

@ajayks9330
1 month ago

Am I the only one who is facing an issue … still In localhost:5000 "This site can't be reached" .. what should I do … Pls reply anyone 🙁