Flash
Flash
Install Flask
Install Flask, which is a micro web framework for Python:
1.
2. Create the Application Code
Create a Python file called app.py to write the application code.
Code Structure
This file contains the basic structure of a Flask application with routes for handling HTTP requests.
app = Flask(__name__)
@app.route('/')
def home():
@app.route('/about')
def about():
if __name__ == '__main__':
app.run(debug=True)
● Routes (@app.route()):
The @app.route() decorator is used to map URLs (routes) to Python functions (view functions).
For example, the root URL (/) is mapped to the home() function, which returns a greeting
message.
● View Functions:
View functions (like home() and about()) are the functions that Flask calls when the
corresponding URL route is requested. They return a response that is sent back to the client (in this
case, a simple string).
● if __name__ == '__main__'::
This ensures that the Flask application runs only if the script is executed directly (and not imported
as a module). The app.run(debug=True) starts the development server with debugging enabled.
To run the Flask app, execute the following command in the terminal:
python app.py
By default, Flask runs the application on http://127.0.0.1:5000/. Visit the following URLs in your
browser:
1. Flask Framework:
Flask is a lightweight web framework for Python, designed to be simple and easy to use. It’s
especially useful for building small-to-medium-sized web applications and APIs.
2. Routes:
Routes are URLs that the Flask application listens for. They are defined using the @app.route()
decorator. When a user navigates to a route, the associated view function is called, and the
response is returned.
This is a basic Flask setup for serving dynamic content on a website. Let me know if you want to explore
specific functionality or add new features!