Flask is a simple tool for creating websites using Python. Think of it as a helpful kit that handles basic tasks needed for building web pages, like linking web addresses to your content, inserting data into pages dynamically, and managing interactions with visitors.
It's called a "micro" framework because it sticks to the essentials. While it covers the basics like routing (which is how it figures out what content to show for different web addresses), it lets you pick and choose other tools for more complex tasks, such as user logins or working with databases.
In contrast, more comprehensive frameworks like Django come with a lot of built-in features, including their own tools for databases and managing user accounts.
1 - Setting things up
Start off by download flask. If you are on replit, you can just do import flask.
pip install flask
Next, we're going to set up a simple web page and use Flask's own server to show it on our own computer. Basically, we're turning our computer into a little server just for us to test things out. It's great for trying things out while you're building a website, but it's not strong enough for a live website that everyone can visit.
Later, we'll go over how to set up your Flask website on a more powerful server using Apache, which is what professionals use to handle a lot of visitors.
2 - Your first flask app
We're going to build our app using just one Python file. First, make a new folder in your home directory and name it hello_world. Inside that folder, create a file called hello.py. In this file, we'll write some code that displays a simple webpage saying "Hello, World!" The code will look like this:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(port=5000)
3 - Code Breakdown
Let's go over what each part of our code does. Starting off, the first line imports Flask from the flask package, which is something you might have seen before. The next line creates a new Flask app using the name of our module, which helps Flask figure out where everything is. Usually, we just use __name__ for this.
The third line is where we use a decorator, a handy Python feature that helps with web addresses. Here, it's saying, "When someone visits the home page (just the slash '/'), run the function right below." If decorators are new to you, think of them as a way to add extra features to functions without changing the functions themselves.
The next two lines define a simple function that just sends back a "Hello World" message when someone visits our home page.
Line 6 checks if the script is run directly by you (not imported somewhere else) before executing the code. This is a common Python trick to make sure your script runs only when you want it to.
Finally, the last line starts up Flask's server right on your computer. We set it to use port 5000 for now (we'll switch to the standard port 80 in a more professional setup), and turn on debugging mode to help us see errors more clearly right in the browser.
4 - Running the Code
To start our test web server, just open up your terminal and run the hello.py file. If you followed the setup we talked about before, here's what you'll type in:
cd hello_world
python hello.py
You should get an output similar to this:
Once you run the command, you'll notice that the process keeps going. This means our web server is up and ready to handle requests. So let's test it out!
Open your web browser—like Firefox, if you're using Ubuntu—and type in localhost:5000 in the address bar.
The "localhost" part is just a quick way to tell your computer, "Hey, send this request to yourself." It's also known as the loopback address, usually 127.0.0.1. The "5000" after the colon is the port number, which is just where the computer listens for specific types of activities.
Normally, websites use port 80, but we're using 5000 here to avoid messing with any other programs that might be using port 80. When we move to a real setup, we'll switch to port 80 so everything works smoothly without the extra numbers.
If everything's set up right, you should see a page that simply says "Hello, World!" This means you've successfully created your first web application with Flask
And with that, I’ll say, welcome to Flask!