Let’s create a basic example Flask project to get you started. Flask is a lightweight and flexible web framework for Python. We’ll set up a simple Flask application with a single route that returns “Hello, world!” when accessed.
Personal Software/Tools Recommendation
I personally use and recommend you for use the bellow tools while developing Flask Projects:
- Git Bash Command Line Interface for Windows
- Visual Studio Code IDE for writing code
Create a Basic Flask Web Framework Project
Step 1: Create a Virtual Environment
If you haven’t already set up a virtual environment, you can create and activate one by following these steps:
# On Windows
python -m venv venv
venv\Scripts\activate
# On macOS/Linux
python3 -m venv venv
source venv/bin/activate
Step 2: Install Flask
Install Flask within the activated virtual environment:
pip install Flask
Step 3: Create the Flask App
Now, let’s create the Flask app. In your desired project directory, create a Python file named app.py
:
# app.py
from flask import Flask
# Create the Flask app instance
app = Flask(__name__)
# Define a route for the root URL
@app.route('/')
def hello_world():
return "Hello, world!"
# Run the app if this script is executed directly
if __name__ == '__main__':
app.run(debug=True)
Step 4: Run the Flask App
To run the Flask app, execute the following command in the terminal:
python app.py
The development server will start, and you should see the following output:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Step 5: Access the App in Your Browser
Open your web browser and go to http://127.0.0.1:5000/. You should see the text “Hello, world!” displayed on the page.
Congratulations! You’ve created a simple Flask project with a basic route. This project can serve as a starting point for building more complex Flask applications. Flask offers many features like routing, templates, request handling, and more, making it a versatile choice for web development with Python. Happy coding!
Find this Project on Github
Please read our blog on Flask Best Pratices for Building High Quality Web Projects.