In this tutorial you will learn about the Flask First Application and its application with practical example.
In this tutorial, we will create a simple hello world website using the Python Flask framework. Please follow the step by step guide to create your first flask application.
Create a Flask Application
Now, let’s create a directory for your project and create a Python file for your Flask application. You can name this file, for example, app.py
.
Write Your First Flask Application
In your app.py
file, write the following code to create a simple Flask application:
1 2 3 4 5 6 7 |
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Flask!' |
This code does the following:
- It imports the Flask class from the Flask library.
- It creates an instance of the Flask class called
app
. - It defines a route (“/”) and a function (
hello_world
) to handle requests to that route. In this case, it returns the text “Hello, Flask!” as a response.
Run Your Flask Application
To run your Flask application, open your terminal or command prompt, navigate to the directory containing app.py
, and run the following command:
1 |
flask run |
You should see output indicating that your Flask app is running, and it will provide a URL (usually http://127.0.0.1:5000/
) where your app is accessible.
Access Your Flask Application
Open a web browser and enter the URL provided in the terminal (e.g., http://127.0.0.1:5000/
).
You should see the message “Hello, Flask!” displayed in your browser. Congratulations! You’ve created and run your first Flask web application.
Conclusion:
In this tutorial, you’ve taken your first steps into the world of Flask web development. You’ve learned how to set up Flask, create a simple web application, and run it locally.