**TL;DR To create a basic "Hello World" application using Flask, install Flask via pip and then create a new file named app.py with the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Run the application by executing python app.py, then open a web browser and navigate to http://localhost:5000/ to see "Hello, World!" displayed on the page.**
Creating Your First Flask Application: A Step-by-Step Guide
As we venture into the world of web development, it's essential to understand that building a robust application from scratch can be an overwhelming task for beginners. That's where frameworks like Flask come in – they provide a foundation upon which you can build your web applications with ease.
In this article, we'll explore how to create a simple "Hello World" application using Flask, and take it a step further by demonstrating basic routing concepts.
Installing Flask
Before diving into the code, let's ensure that we have Flask installed in our Python environment. You can install it via pip:
pip install flask
With Flask up and running, we're ready to build our first application!
Creating Your First Application
Create a new file named app.py and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Let's break down what we've just created:
- We imported Flask from its package.
- An instance of the Flask application was created and stored in
app. - The
@app.route('/')decorator is used to associate a function with a URL path. In this case, it maps/(the root URL) to thehello_world()function. - The
if __name__ == '__main__':block ensures that our application only runs when the script is executed directly (not if it's imported as a module in another script).
Running Your Application
Save the file and run your Flask application by executing:
python app.py
Open up a web browser and navigate to http://localhost:5000/. You should see "Hello, World!" displayed on the page.
Basic Routing Demonstration
Now that we have our first "Hello World" application up and running, let's explore some basic routing concepts. Update your app.py file with the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/about')
def about_us():
return 'This is a short description of our company.'
if __name__ == '__main__':
app.run()
Here's what changed:
- We've added another route
/aboutthat maps to theabout_us()function. - This new function returns a string describing your company.
Run the application again by executing python app.py. Now, when you navigate to http://localhost:5000/, you should see "Hello, World!". Visit http://localhost:5000/about to view the about page description.
Conclusion
Congratulations! You've just taken your first steps in building a Flask application. We started by installing Flask and creating our first "Hello World" application using basic routing concepts. Remember that this is just the beginning of your journey as a web developer, and there's much more to explore with Flask!
In the next article, we'll dive deeper into advanced routing features, templates, and database integration – but for now, enjoy building simple applications with Flask!
