Setting Up a Flask Server: A Comprehensive Guide
Flask, a popular Python web framework, is renowned for its simplicity and flexibility. It's an excellent choice for building small applications or APIs, and it's easy to get started with. In this guide, we'll walk you through setting up a simple Flask server, step by step.
Prerequisites
Before we begin, ensure you have Python (3.6 or later) and pip installed on your system. You can verify this by running the following commands in your terminal:
python --version
pip --version
Installing Flask
If you haven't already, install Flask using pip:

pip install flask
Creating Your First Flask Application
Create a new directory for your project and navigate to it in your terminal. Then, create a new file named app.py and open it in your favorite text editor or IDE.
Hello, World!
Let's start by creating a simple "Hello, World!" application. Add the following code to your app.py file:
```python from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, World!" if __name__ == '__main__': app.run(debug=True) ```
The Flask class is our WSGI application, and we've created a single route ('/') that responds with "Hello, World!". The if __name__ == '__main__': block ensures that our server only runs when the script is executed directly (not imported as a module).

Running Your Flask Server
Now, run your Flask server by executing the following command in your terminal:
python app.py
You should see output similar to this:
``` * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) ```
Open your web browser and navigate to http://127.0.0.1:5000/. You should see "Hello, World!" displayed.

Adding More Routes
Let's add a new route to our application. In your app.py file, add the following code below the existing route:
```python @app.route('/about') def about(): return "This is the about page." ```
Now, if you navigate to http://127.0.0.1:5000/about in your browser, you should see "This is the about page." displayed.
Using Templates
While returning plain text is fine for simple applications, Flask also supports templates for rendering dynamic web pages. Install the flask-bootstrap and flask-wtf packages using pip:
pip install flask-bootstrap flask-wtf
Create a new folder named templates in your project directory, and inside it, create a new file named base.html. Add the following code to base.html:
```html
Now, update your app.py file to use this template:
```python from flask import Flask, render_template app = Flask(__name__) @app.route('/') def hello(): return render_template('base.html', title='Home', content='Hello, World!') @app.route('/about') def about(): return render_template('base.html', title='About', content='This is the about page.') if __name__ == '__main__': app.run(debug=True) ```
Now, when you navigate to http://127.0.0.1:5000/ or http://127.0.0.1:5000/about, you should see your content displayed within a basic HTML structure.
Conclusion
In this guide, we've created a simple Flask server, added routes, and used templates to render dynamic content. Flask's simplicity makes it an excellent choice for building small applications and APIs. As you become more comfortable with Flask, you can explore its extensive ecosystem of extensions and libraries to add functionality to your applications.









![[32oz] Sunnies Flask In Ube Pandan](https://i.pinimg.com/originals/23/28/54/23285470acc6c331f6cc758ea9ce2691.jpg)










