0% found this document useful (0 votes)
6 views3 pages

Flash

Uploaded by

Amith G Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Flash

Uploaded by

Amith G Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Here’s a draft for a simple Flask application with an explanation:

Steps to Create a Simple Flask Application

Install Flask
Install Flask, which is a micro web framework for Python:

pip install flask

1.
2. Create the Application Code
Create a Python file called app.py to write the application code.

Code Structure

1. app.py (Main Application File)

This file contains the basic structure of a Flask application with routes for handling HTTP requests.

# Import the Flask class from the flask module

from flask import Flask

# Create an instance of the Flask class

app = Flask(__name__)

# Define a route for the home page

@app.route('/')

def home():

return 'Hello, World! Welcome to my Flask app.'

# Define another route for the about page

@app.route('/about')

def about():

return 'This is the about page of the Flask app.'


# Run the application on the local development server

if __name__ == '__main__':

app.run(debug=True)

2. Explanation of Key Components

● Flask Object (app):


The Flask object is the core of the application. It’s responsible for routing requests to the
appropriate view function and running the web server.

● 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.

3. Run the Application

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:

● http://127.0.0.1:5000/ → Displays the greeting from the home() function.


● http://127.0.0.1:5000/about → Displays the content from the about() function.

Explanation of Key Concepts

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.

3. Running the Server:


When you run app.run(), Flask starts a built-in development server to handle incoming requests.
The debug=True option enables detailed error messages and auto-reloading of the app when code
changes are made.

Extending the Application

You can extend this basic application by:

● Adding more routes for different pages.


● Rendering HTML templates using render_template().
● Handling form submissions and data from users.
● Connecting to a database to store and retrieve data.

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!

You might also like