Hey, future web developer superstar! 🌟
Are you excited? Because today is the day you’ll build your first-ever web application using Flask! Remember, we’ve already set up our Python environment and installed Flask. Now, it’s time to put those tools to good use.
Ready? Let’s do this!
What You’ll Need
To make sure everyone is on the same page, here’s what you should have ready:
Python installed: This is our building material.
Flask installed in a virtual environment: These are our special tools.
If you’re scratching your head, wondering how to get these, check out our previous post.
The Anatomy of a Basic Flask App
Imagine you’re making a sandwich. You need bread, some fillings, and maybe some sauce, right? A Flask app is kind of like that sandwich, and it has three main ingredients:
- Import Flask: This is the bread of our sandwich.
- Initialize App: Think of this as the fillings.
- Define Route: And finally, the sauce that adds flavor.
Creating Your First Flask App
Open up your text editor or IDE (this is like our kitchen for today), and let’s create a new file. Name it `app.py`.
Now, let’s add the following lines of code to this file:
# Import Flask (The Bread) from flask import Flask # Initialize the App (The Fillings) app = Flask(__name__) # Define a Route (The Sauce) @app.route("/") def hello(): Â Â Â Â return "Hello, World!" # Run the App if __name__ == "__main__": Â Â Â Â app.run()
Let’s dissect this sandwich—I mean, code:
- from flask import Flask: This is like saying, “Hey, I need a slice of bread to start my sandwich.” We’re bringing in the Flask library to use in our app.
- app = Flask(__name__): Now, we’re laying out the fillings on our bread. In code terms, we’re creating an instance of the Flask class. The __name__ part is a special variable that tells Flask where to look for resources, like images or templates.
- @app.route(“/”): Time to add the sauce! This line tells Flask what to do when someone visits the home page of our app. The “/” represents the homepage.
- def hello():: This is our sauce recipe. When someone visits our homepage, this function runs, sending back the message “Hello, World!”
- if __name__ == “__main__”:: This line is like saying, “If I’m making this sandwich for myself (and not for a restaurant), I’ll eat it.” In other words, this line checks if you’re running this script directly (not importing it from another script).
- app.run(): Finally, it’s time to eat the sandwich! Or in our case, run the Flask app.
Running the App
Now that our sandwich—umm, I mean, Flask app—is ready, let’s see it in action.
- Open your terminal: This is like our serving tray.
- Navigate to the folder where app.py is stored: This is where our sandwich is waiting.
- Type `
python app.py
` and hit Enter: And voila! Your sandwich is served, or rather, your web app is running!
You should see some text on your terminal, telling you the app is running. Now, open your web browser and go to http://localhost:5000. You’ll see your “Hello, World!” message!
Conclusion
Wow, look at you! You just made your first web app, and it’s delicious—I mean, amazing! 🎉
Next time, we’ll add more layers to our sandwich, like cheese and lettuce. In coding terms, we’ll learn more about Flask routes and how to make our web app do even cooler things.