Step 1: Set Up Your Development Environment
- Install Python: Make sure you have Python installed on your system. You can download it from the official Python website.
- Install Flask: Use pip to install Flask. Open your terminal or command prompt and run:bash
pip install Flask
Step 2: Create a Basic Flask Application
- Create a Project Directory: Create a new directory for your project and navigate into it.bash
mkdir my_flask_app cd my_flask_app
- Create the Application File: Create a new file named
app.py
and open it in your text editor.
Step 3: Write Your Flask Application Code
- Import Flask: Import the Flask class from the
flask
module. - Create an Instance of Flask: Create an instance of the Flask class.
- Define Routes: Use the
@app.route
decorator to define the routes and their corresponding handler functions. - Run the App: Use the
app.run
method to run the app.
Here's the complete code for a basic Flask application:
pythonfrom flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)
Step 4: Run Your Flask Application
- Run the Application: In your terminal, navigate to the directory containing
app.py
and run:bashpython app.py
- Open Your Browser: Open your web browser and go to
http://127.0.0.1:5000/
. You should see the message "Hello, Flask!".
Step 5: Add More Functionality
You can add more routes and functionality to your web app. Here's an example of how to add more routes and use HTML templates:
Create Templates Directory: Create a directory named
templates
in your project directory.bashmkdir templates
Create an HTML Template: Create a new HTML file named
index.html
inside thetemplates
directory.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Flask App</title> </head> <body> <h1>Welcome to My Flask App</h1> <p>{{ message }}</p> </body> </html>
Update
app.py
to Render the Template:pythonfrom flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('index.html', message="Hello, Flask with Templates!") @app.route('/about') def about(): return "This is the about page." if __name__ == '__main__': app.run(debug=True)
Step 6: Add More Routes and Templates
You can continue to add more routes and corresponding templates to expand your web app's functionality. Here's an example of adding an "About" page with a template:
Create a New Template: Create an
about.html
file in thetemplates
directory.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>About</title> </head> <body> <h1>About My Flask App</h1> <p>This is a simple web app built with Flask.</p> </body> </html>
Update
app.py
to Render the New Template:python@app.route('/about') def about(): return render_template('about.html')
Step 7: Run and Test Your Application
- Run the Application: Run your Flask application again using:bash
python app.py
- Test the Routes: Open your browser and navigate to
http://127.0.0.1:5000/
for the home page andhttp://127.0.0.1:5000/about
for the about page.
0 Comments